summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-29 22:42:19 +0800
committerruki <[email protected]>2019-10-29 10:13:54 +0800
commit0ad553e46eb001448cf5acac7ba6d26a11e39eff (patch)
treeb1c8bf9fe95ec38ac7048d74970f90380b7b66d8
parentb2a507bc82fefb561c386f4910ba223abed20e1e (diff)
fix socket_recv
-rw-r--r--core/src/xmake/io/socket_recv.c7
-rw-r--r--xmake/core/base/socket.lua15
2 files changed, 14 insertions, 8 deletions
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c
index 162e36c1d..dce4da277 100644
--- a/core/src/xmake/io/socket_recv.c
+++ b/core/src/xmake/io/socket_recv.c
@@ -65,6 +65,9 @@ tb_int_t xm_io_socket_recv(lua_State* lua)
if (size > sizeof(data))
size = sizeof(data);
+ // get the previous data and size first
+ size_t prev_size = 0;
+ tb_char_t const* prev_data = luaL_optlstring(lua, 3, "", &prev_size);
// recv data
tb_long_t real = tb_socket_recv(sock, data, size);
@@ -75,9 +78,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua)
luaL_Buffer result;
luaL_buffinit(lua, &result);
- // prepend the previous data and size first
- size_t prev_size = 0;
- tb_char_t const* prev_data = luaL_optlstring(lua, 3, "", &prev_size);
+ // prepend the previous data
if (prev_data && prev_size) luaL_addlstring(&result, prev_data, prev_size);
luaL_addlstring(&result, (tb_char_t const*)data, real);
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 47c358599..e3cd1fa69 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -223,12 +223,18 @@ end
-- send file to socket
function _instance:sendfile(file, opt)
- -- ensure opened
+ -- ensure the socket opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
+ -- ensure the file opened
+ local ok, errors = file:_ensure_opened()
+ if not ok then
+ return -1, errors
+ end
+
-- init start and last
opt = opt or {}
local start = opt.start or 1
@@ -246,8 +252,7 @@ function _instance:sendfile(file, opt)
local errors = nil
if opt.block then
while start < last do
- -- TODO pass file/handle, improve block send
- real, errors = io.socket_sendfile(self._SOCK, file, start, last)
+ real, errors = io.socket_sendfile(self._SOCK, file._FILE, start, last)
if real > 0 then
send = send + real
start = start + real
@@ -265,7 +270,7 @@ function _instance:sendfile(file, opt)
end
end
else
- send, errors = io.socket_sendfile(self._SOCK, file, start, last)
+ send, errors = io.socket_sendfile(self._SOCK, file._FILE, start, last)
if send < 0 and errors then
errors = string.format("%s: %s", self, errors)
end
@@ -314,7 +319,7 @@ function _instance:recv(size, opt)
end
end
else
- recv, data_or_errors = io.socket_recv(self._SOCK, size)
+ 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