diff options
| author | ruki <[email protected]> | 2019-10-16 00:48:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-15 22:53:03 +0800 |
| commit | 690dc274a4d5f9f443e6c2004b985b03345874d6 (patch) | |
| tree | 43b6ba534d331325ca8d4022994bda75e496e5eb | |
| parent | e915ad63a7f1a9fa4736472c60b0550cbd174361 (diff) | |
add socket.send
| -rw-r--r-- | core/src/xmake/io/socket_send.c | 68 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/echo_client.lua | 9 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/echo_server.lua | 7 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 17 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 9 |
7 files changed, 109 insertions, 4 deletions
diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c new file mode 100644 index 000000000..f33e77414 --- /dev/null +++ b/core/src/xmake/io/socket_send.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_send.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_send" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// io.socket_send(file, data, start, last) +tb_int_t xm_io_socket_send(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); + + // send data + tb_long_t real = tb_socket_send(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 0a72b2456..9b9ede041 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -117,6 +117,7 @@ tb_int_t xm_io_socket_bind(lua_State* lua); 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_close(lua_State* lua); // the path functions @@ -258,6 +259,7 @@ static luaL_Reg const g_io_functions[] = , { "socket_listen", xm_io_socket_listen } , { "socket_accept", xm_io_socket_accept } , { "socket_connect", xm_io_socket_connect } +, { "socket_send", xm_io_socket_send } , { "socket_close", xm_io_socket_close } , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 830f83066..c7ebc2644 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -67,6 +67,7 @@ xmake_C_FILES += \ io/socket_listen \ io/socket_accept \ io/socket_connect \ + io/socket_send \ io/socket_close \ path/relative \ path/absolute \ diff --git a/tests/modules/socket/tcp/echo_client.lua b/tests/modules/socket/tcp/echo_client.lua index fce4ffcff..4da40bdc1 100644 --- a/tests/modules/socket/tcp/echo_client.lua +++ b/tests/modules/socket/tcp/echo_client.lua @@ -1,7 +1,12 @@ import("core.base.socket") function main() - local sock = socket.connect("127.0.0.1", 9001) - print(sock) + local addr = "127.0.0.1" + local port = 9001 + print("%s: connect %s:%d ..", sock, addr, port) + local sock = socket.connect(addr, port) + print("%s: connected!", sock) + local send = sock:send("hello") + print("%s: send %d", sock, send) sock:close() end diff --git a/tests/modules/socket/tcp/echo_server.lua b/tests/modules/socket/tcp/echo_server.lua index 76aa9dfb6..a047aa15f 100644 --- a/tests/modules/socket/tcp/echo_server.lua +++ b/tests/modules/socket/tcp/echo_server.lua @@ -1,15 +1,18 @@ import("core.base.socket") function main() - local sock = socket.bind("127.0.0.1", 9001) + local addr = "127.0.0.1" + local port = 9001 + local sock = socket.bind(addr, port) sock:listen(20) local sock_client = nil while true do + print("%s: listening %s:%d ..", sock, addr, port) local ok = sock:wait(socket.EV_ACPT, -1) if ok == socket.EV_ACPT then sock_client = sock:accept() if sock_client then - print(sock_client) + print("%s: accepted", sock_client) sock_client:close() end end diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 09fd00646..78bbb9935 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -150,6 +150,23 @@ function _instance:connect(addr, port) return result, errors end +-- send data to socket +function _instance:send(data, start, last) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- send it + local result, errors = io.socket_send(self._SOCK, data, start, last) + if result < 0 and errors then + errors = string.format("%s: %s", self, errors) + end + return result, errors +end + -- wait socket events function _instance:wait(events, timeout) diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index b401e47ed..082aabe4a 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -105,6 +105,15 @@ function sandbox_core_base_socket_instance.connect(sock, addr, port) return result end +-- send data to socket +function sandbox_core_base_socket_instance.send(sock, data, start, last) + local result, errors = sock:_send(data, start, last) + if result < 0 and errors then + raise(errors) + end + return result +end + -- get socket rawfd function sandbox_core_base_socket_instance.rawfd(sock) local result, errors = sock:_rawfd() |
