diff options
| author | ruki <[email protected]> | 2019-12-18 22:43:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-18 10:22:52 +0800 |
| commit | 9e01c1f9affa69b23a78ee1c04ba7cf8302bdfb2 (patch) | |
| tree | 180a2f86b80f81335df8afb698e29e36d8fd44a7 /core/src | |
| parent | 656f546055712e694754211bbd99ff18d144d856 (diff) | |
improve socket recv
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 5 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_recvfrom.c | 36 |
2 files changed, 17 insertions, 24 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index 0b4b680fe..ce87cffca 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -76,10 +76,5 @@ tb_int_t xm_io_socket_recv(lua_State* lua) // recv data tb_long_t real = tb_socket_recv(sock, data, size); lua_pushinteger(lua, (tb_int_t)real); - if (real > 0) - { - lua_pushlstring(lua, (tb_char_t const*)data, real); - return 2; - } return 1; } diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c index 9bf08c542..7e192f60d 100644 --- a/core/src/xmake/io/socket_recvfrom.c +++ b/core/src/xmake/io/socket_recvfrom.c @@ -52,27 +52,25 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) 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) + // get data + tb_byte_t* data = tb_null; + if (lua_isnumber(lua, 2)) + data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 2); + if (!data) { lua_pushinteger(lua, -1); - lua_pushfstring(lua, "invalid size(%ld)!", size); + lua_pushfstring(lua, "invalid data(%p)!", data); return 2; } - if (!size) size = sizeof(data); - else if (size > sizeof(data)) + + // get size + tb_long_t size = 0; + if (lua_isnumber(lua, 3)) size = (tb_long_t)lua_tonumber(lua, 3); + if (size <= 0) { - data = tb_malloc_bytes(size); - if (!data) - { - lua_pushinteger(lua, -1); - lua_pushfstring(lua, "malloc(%ld) failed!", size); - return 2; - } + lua_pushinteger(lua, -1); + lua_pushfstring(lua, "invalid size(%ld)!", size); + return 2; } // recv data @@ -84,10 +82,11 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) if (real > 0) { retn = 2; - lua_pushlstring(lua, (tb_char_t const*)data, real); + lua_pushnil(lua); if (!tb_ipaddr_is_empty(&ipaddr)) { - tb_char_t const* ipstr = tb_ipaddr_ip_cstr(&ipaddr, (tb_char_t*)buffer, sizeof(buffer)); + tb_char_t buffer[256]; + tb_char_t const* ipstr = tb_ipaddr_ip_cstr(&ipaddr, buffer, sizeof(buffer)); if (ipstr) { lua_pushstring(lua, ipstr); @@ -96,6 +95,5 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) } } } - if (data != buffer) tb_free(data); return retn; } |
