diff options
| author | ruki <[email protected]> | 2019-12-17 23:51:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-17 17:08:04 +0800 |
| commit | 656f546055712e694754211bbd99ff18d144d856 (patch) | |
| tree | ee4631b0a90b1ce4f95089f015d5b874b8e484d2 | |
| parent | 8b62b7ced03aa39d79980958cf1d92cef88883f5 (diff) | |
fix socket recv for iocp
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 20 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 9 |
2 files changed, 19 insertions, 10 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index a24eac0cb..0b4b680fe 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -52,18 +52,26 @@ tb_int_t xm_io_socket_recv(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 data[8192]; + // 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 data(%p)!", data); + return 2; + } + + // get size tb_long_t size = 0; - if (lua_isnumber(lua, 2)) size = (tb_long_t)lua_tonumber(lua, 2); - if (size < 0) + if (lua_isnumber(lua, 3)) size = (tb_long_t)lua_tonumber(lua, 3); + if (size <= 0) { lua_pushinteger(lua, -1); lua_pushfstring(lua, "invalid size(%ld)!", size); return 2; } - if (size > sizeof(data)) - size = sizeof(data); // recv data tb_long_t real = tb_socket_recv(sock, data, size); diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index ffae67082..a01dde5dd 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -322,14 +322,15 @@ function _instance:recv(size, opt) local real = 0 local wait = false local data_or_errors = nil + local data = bytes(8192) if opt.block then local results = {} while recv < size do - real, data_or_errors = io.socket_recv(self:csock(), size - recv) + real, data_or_errors = io.socket_recv(self:csock(), data:caddr(), math.min(8192, size - recv)) if real > 0 then recv = recv + real wait = false - table.insert(results, bytes(data_or_errors)) + table.insert(results, bytes(data, 1, real)) elseif real == 0 and not wait then local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1) if events == socket.EV_RECV then @@ -348,9 +349,9 @@ function _instance:recv(size, opt) recv = -1 end else - recv, data_or_errors = io.socket_recv(self:csock(), size) + recv, data_or_errors = io.socket_recv(self:csock(), data:caddr(), math.min(8192, size)) if recv > 0 then - data_or_errors = bytes(data_or_errors) + data_or_errors = bytes(data, 1, recv) end end if recv < 0 and data_or_errors then |
