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 /core/src | |
| parent | b0a7b7a70cafc7988a33e1856147622b0ea92559 (diff) | |
add socket.sendto and socket.recvfrom
Diffstat (limited to 'core/src')
| -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 |
4 files changed, 188 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 \ |
