diff options
| author | ruki <[email protected]> | 2019-11-03 22:19:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-11-03 22:19:55 +0800 |
| commit | 8eddfb0d678d6ea5427ae55e9dff108fde5add42 (patch) | |
| tree | cac98a7120db66447cf48b6b6c1bdf2d05be04a4 | |
| parent | b0a7b7a70cafc7988a33e1856147622b0ea92559 (diff) | |
add socket.sendto and socket.recvfrom
| -rw-r--r-- | core/src/xmake/io/socket_recvfrom.c | 101 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_sendto.c | 81 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 2 | ||||
| -rw-r--r-- | tests/modules/socket/udp/echo_client.lua | 10 | ||||
| -rw-r--r-- | tests/modules/socket/udp/echo_server.lua | 14 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 95 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 18 |
8 files changed, 325 insertions, 0 deletions
diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c new file mode 100644 index 000000000..2933a44f0 --- /dev/null +++ b/core/src/xmake/io/socket_recvfrom.c @@ -0,0 +1,101 @@ +/*!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_recvfrom.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_recvfrom" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// real, data_or_errors, addr, port = io.socket_recvfrom(sock, size) +tb_int_t xm_io_socket_recvfrom(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check socket + if (!lua_isuserdata(lua, 1)) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid socket!"); + return 2; + } + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // get data and size + tb_byte_t buffer[8192]; + tb_byte_t* data = buffer; + tb_long_t size = 0; + if (lua_isnumber(lua, 2)) size = (tb_long_t)lua_tonumber(lua, 2); + if (size < 0) + { + lua_pushnumber(lua, -1); + lua_pushfstring(lua, "invalid size(%ld)!", size); + return 2; + } + if (!size) size = sizeof(data); + else if (size > sizeof(data)) + { + data = tb_malloc_bytes(size); + if (!data) + { + lua_pushnumber(lua, -1); + lua_pushfstring(lua, "malloc(%ld) failed!", size); + return 2; + } + } + + // recv data + tb_ipaddr_t ipaddr; + tb_ipaddr_clear(&ipaddr); + tb_int_t retn = 1; + tb_long_t real = tb_socket_urecv(sock, &ipaddr, data, size); + lua_pushnumber(lua, (tb_int_t)real); + if (real > 0) + { + retn = 2; + lua_pushlstring(lua, (tb_char_t const*)data, real); + if (!tb_ipaddr_is_empty(&ipaddr)) + { + tb_char_t const* ipstr = tb_ipaddr_ip_cstr(&ipaddr, (tb_char_t*)buffer, sizeof(buffer)); + if (ipstr) + { + lua_pushstring(lua, ipstr); + lua_pushnumber(lua, (tb_int_t)tb_ipaddr_port(&ipaddr)); + retn = 4; + } + } + } + if (data != buffer) tb_free(data); + return retn; +} diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c new file mode 100644 index 000000000..ed3e18261 --- /dev/null +++ b/core/src/xmake/io/socket_sendto.c @@ -0,0 +1,81 @@ +/*!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_sendto.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "socket_sendto" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// io.socket_sendto(sock, data, addr, port) +tb_int_t xm_io_socket_sendto(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check socket + if (!lua_isuserdata(lua, 1)) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid socket!"); + return 2; + } + + // 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 address + tb_char_t const* addr = lua_tostring(lua, 3); + tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 4); + if (!addr || !port) + { + lua_pushnumber(lua, -1); + lua_pushliteral(lua, "invalid address!"); + return 2; + } + + // get address family + tb_size_t family = (tb_size_t)luaL_checknumber(lua, 5); + + // init ip address + tb_ipaddr_t ipaddr; + tb_ipaddr_set(&ipaddr, addr, port, family); + + // send data + tb_long_t real = tb_socket_usend(sock, &ipaddr, (tb_byte_t const*)data, (tb_size_t)datasize); + lua_pushnumber(lua, (tb_int_t)real); + return 1; +} diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 4319023cc..835e10b73 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -118,8 +118,10 @@ 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_sendto(lua_State* lua); tb_int_t xm_io_socket_sendfile(lua_State* lua); tb_int_t xm_io_socket_recv(lua_State* lua); +tb_int_t xm_io_socket_recvfrom(lua_State* lua); tb_int_t xm_io_socket_close(lua_State* lua); // the path functions @@ -262,8 +264,10 @@ 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_sendto", xm_io_socket_sendto } , { "socket_sendfile", xm_io_socket_sendfile } , { "socket_recv", xm_io_socket_recv } +, { "socket_recvfrom", xm_io_socket_recvfrom } , { "socket_close", xm_io_socket_close } , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index b3028e984..e2660128a 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -68,8 +68,10 @@ xmake_C_FILES += \ io/socket_accept \ io/socket_connect \ io/socket_send \ + io/socket_sendto \ io/socket_sendfile \ io/socket_recv \ + io/socket_recvfrom \ io/socket_close \ path/relative \ path/absolute \ diff --git a/tests/modules/socket/udp/echo_client.lua b/tests/modules/socket/udp/echo_client.lua new file mode 100644 index 000000000..2c628cc92 --- /dev/null +++ b/tests/modules/socket/udp/echo_client.lua @@ -0,0 +1,10 @@ +import("core.base.socket") + +function main() + local addr = "127.0.0.1" + local port = 9001 + local sock = socket.udp() + local send = sock:sendto("hello world..", addr, port, {block = true}) + print("%s: send to %s:%d %d bytes!", sock, addr, port, send) + sock:close() +end diff --git a/tests/modules/socket/udp/echo_server.lua b/tests/modules/socket/udp/echo_server.lua new file mode 100644 index 000000000..98cf72f7c --- /dev/null +++ b/tests/modules/socket/udp/echo_server.lua @@ -0,0 +1,14 @@ +import("core.base.socket") + +function main() + local sock = socket.udp() + while true do + print("%s: recv ..", sock) + local recv, data, addr, port = sock:recv(8192, {block = true}) + print("%s: recv: %d bytes from: %s:%d", sock, recv, addr, port) + if data then + data:dump() + end + end + sock:close() +end diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 26ca2b2e8..06820522c 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -346,6 +346,101 @@ function _instance:recv(size, opt) return recv, data_or_errors end +-- send udp data to peer +function _instance:sendto(data, addr, port, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- check address + if not addr or not port then + return -1, string.format("%s: sendto empty address!", self) + end + + -- send it + opt = opt or {} + local send = 0 + local wait = false + local errors = nil + if opt.block then + while true do + send, errors = io.socket_sendto(self._SOCK, data, addr, port, self:family()) + if send == 0 and not wait then + local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1) + if events == socket.EV_SEND then + wait = true + else + errors = waiterrs + break + end + else + break + end + end + else + send, errors = io.socket_sendto(self._SOCK, data, addr, port, self:family()) + if send < 0 and errors then + errors = string.format("%s: %s", self, errors) + end + end + return send, errors +end + +-- recv udp data from peer +function _instance:recvfrom(size, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- check size + if size == 0 then + return 0 + elseif size == nil or size < 0 then + return -1, string.format("%s: invalid size(%d)!", self, size) + end + + -- recv it + opt = opt or {} + local recv = 0 + local wait = false + local data_or_errors = nil + if opt.block then + while true do + recv, data_or_errors, addr, port = io.socket_recvfrom(self._SOCK, size) + if recv > 0 then + data_or_errors = bytes(data_or_errors) + break + elseif recv == 0 and not wait then + local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1) + if events == socket.EV_RECV then + wait = true + else + recv = -1 + data_or_errors = waiterrs + break + end + else + break + end + end + else + recv, data_or_errors, addr, port = io.socket_recvfrom(self._SOCK, size) + if recv > 0 then + data_or_errors = bytes(data_or_errors) + end + end + if recv < 0 and data_or_errors then + data_or_errors = string.format("%s: %s", self, data_or_errors) + end + return recv, data_or_errors, addr, port +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 3d679bd52..73aa0994b 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -132,6 +132,24 @@ function sandbox_core_base_socket_instance.recv(sock, size, opt) return real, data_or_errors end +-- send udp data to peer +function sandbox_core_base_socket_instance.sendto(sock, data, addr, port, opt) + local real, errors = sock:_sendto(data, addr, port, opt) + if real < 0 and errors then + raise(errors) + end + return real +end + +-- recv udp data from peer +function sandbox_core_base_socket_instance.recvfrom(sock, size, opt) + local real, data_or_errors, addr, port = sock:_recvfrom(size, opt) + if real < 0 and data_or_errors then + raise(data_or_errors) + end + return real, data_or_errors, addr, port +end + -- get socket rawfd function sandbox_core_base_socket_instance.rawfd(sock) local result, errors = sock:_rawfd() |
