diff options
| author | ruki <[email protected]> | 2019-10-30 00:41:51 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-29 22:11:09 +0800 |
| commit | 5f388f24e9c0b2f62101667490a2032306148a35 (patch) | |
| tree | 8527647e2b1ec8d427088f278f0524bc53712831 | |
| parent | 0ad553e46eb001448cf5acac7ba6d26a11e39eff (diff) | |
fix socket.recv
| -rw-r--r-- | core/src/xmake/io/socket_recv.c | 9 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/file_client.lua | 1 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 17 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 8 |
4 files changed, 22 insertions, 13 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index dce4da277..e1f48cf9b 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -34,7 +34,7 @@ * implementation */ -// real, data_or_errors = io.socket_recv(sock, size) +// real, data, errors = io.socket_recv(sock, size, prev_data) tb_int_t xm_io_socket_recv(lua_State* lua) { // check @@ -44,6 +44,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua) if (!lua_isuserdata(lua, 1)) { lua_pushnumber(lua, -1); + lua_pushnil(lua); lua_pushliteral(lua, "invalid socket!"); return 2; } @@ -59,6 +60,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua) if (size < 0) { lua_pushnumber(lua, -1); + lua_pushnil(lua); lua_pushfstring(lua, "invalid size(%ld)!", size); return 2; } @@ -86,5 +88,10 @@ tb_int_t xm_io_socket_recv(lua_State* lua) luaL_pushresult(&result); return 2; } + else if (prev_data && prev_size) + { + lua_pushlstring(lua, prev_data, prev_size); + return 2; + } return 1; } diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua index 61924b95c..1d2756d77 100644 --- a/tests/modules/socket/tcp/file_client.lua +++ b/tests/modules/socket/tcp/file_client.lua @@ -12,6 +12,7 @@ function main() local wait = false while true do real, data = sock:recv(8192, {prevdata = data}) + print(real, type(data)) if real > 0 then recv = recv + real wait = false diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index e3cd1fa69..6ee878bd8 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -299,10 +299,11 @@ function _instance:recv(size, opt) local recv = 0 local real = 0 local wait = false - local data_or_errors = opt.prevdata + local data = opt.prevdata + local errors = nil if opt.block then while recv < size do - real, data_or_errors = io.socket_recv(self._SOCK, size - recv, data_or_errors) + real, data, errors = io.socket_recv(self._SOCK, size - recv, data) if real > 0 then recv = recv + real wait = false @@ -311,7 +312,7 @@ function _instance:recv(size, opt) if events == socket.EV_RECV then wait = true else - data_or_errors = waiterrs + errors = waiterrs break end else @@ -319,12 +320,12 @@ function _instance:recv(size, opt) end end else - recv, data_or_errors = io.socket_recv(self._SOCK, size, data_or_errors) - if recv < 0 and data_or_errors then - data_or_errors = string.format("%s: %s", self, data_or_errors) - end + recv, data, errors = io.socket_recv(self._SOCK, size, data) + end + if recv < 0 and errors then + errors = string.format("%s: %s", self, errors) end - return recv, data_or_errors + return recv, data, errors end -- wait socket events diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index 3d679bd52..e6a0b88cd 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -125,11 +125,11 @@ end -- recv data from socket function sandbox_core_base_socket_instance.recv(sock, size, opt) - local real, data_or_errors = sock:_recv(size, opt) - if real < 0 and data_or_errors then - raise(data_or_errors) + local real, data, errors = sock:_recv(size, opt) + if real < 0 and errors then + raise(errors) end - return real, data_or_errors + return real, data end -- get socket rawfd |
