diff options
| author | ruki <[email protected]> | 2019-10-16 22:15:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-16 07:30:16 +0800 |
| commit | 74fe92ec5fab1725b139567f468f49cab4580888 (patch) | |
| tree | 4cb88fa26fe6580b6441f76b17b41e8cd393a85c | |
| parent | 690dc274a4d5f9f443e6c2004b985b03345874d6 (diff) | |
add socket.recv
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 68 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 18 |
4 files changed, 89 insertions, 0 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c new file mode 100644 index 000000000..849bc832a --- /dev/null +++ b/core/src/xmake/io/socket_recv.c @@ -0,0 +1,68 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file socket_recv.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_recv" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// io.socket_recv(file, data, start, last) +tb_int_t xm_io_socket_recv(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // get data + size_t datasize = 0; + tb_char_t const* data = luaL_checklstring(lua, 2, &datasize); + tb_assert_and_check_return_val(data, 0); + + // get start + tb_size_t start = 1; + if (lua_isnumber(lua, 3)) start = lua_tonumber(lua, 3); + + // get last + tb_size_t last = (tb_size_t)datasize; + if (lua_isnumber(lua, 4)) last = lua_tonumber(lua, 4); + + // recv data + tb_long_t real = tb_socket_recv(sock, (tb_byte_t const*)data + start - 1, last - start + 1); + lua_pushnumber(lua, (tb_int_t)real); + return 1; +} diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 9b9ede041..99a0cf18e 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -118,6 +118,7 @@ tb_int_t xm_io_socket_listen(lua_State* lua); tb_int_t xm_io_socket_accept(lua_State* lua); tb_int_t xm_io_socket_connect(lua_State* lua); tb_int_t xm_io_socket_send(lua_State* lua); +tb_int_t xm_io_socket_recv(lua_State* lua); tb_int_t xm_io_socket_close(lua_State* lua); // the path functions @@ -260,6 +261,7 @@ static luaL_Reg const g_io_functions[] = , { "socket_accept", xm_io_socket_accept } , { "socket_connect", xm_io_socket_connect } , { "socket_send", xm_io_socket_send } +, { "socket_recv", xm_io_socket_recv } , { "socket_close", xm_io_socket_close } , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index c7ebc2644..597001707 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -68,6 +68,7 @@ xmake_C_FILES += \ io/socket_accept \ io/socket_connect \ io/socket_send \ + io/socket_recv \ io/socket_close \ path/relative \ path/absolute \ diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 78bbb9935..e85ec0301 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -167,6 +167,24 @@ function _instance:send(data, start, last) return result, errors end +-- TODO +-- recv data from socket +function _instance:recv(size) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- recv it + local result, errors = io.socket_recv(self._SOCK, data, size) + if not result and errors then + errors = string.format("%s: %s", self, errors) + end + return result, errors +end + -- wait socket events function _instance:wait(events, timeout) |
