summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-06 22:51:24 +0800
committerruki <[email protected]>2022-04-06 22:51:24 +0800
commit313fc50dbd586e43c7516dc12202db02e6b94945 (patch)
tree446a2ce9bab6d148974b5fa2624c57722b9936ed
parent3d378500c304db2776ff2969491084ce4d320e58 (diff)
improve pipe write
-rw-r--r--core/src/xmake/io/pipe_write.c46
-rw-r--r--core/src/xmake/io/socket_sendto.c30
-rw-r--r--xmake/core/base/pipe.lua23
-rw-r--r--xmake/core/base/socket.lua24
4 files changed, 40 insertions, 83 deletions
diff --git a/core/src/xmake/io/pipe_write.c b/core/src/xmake/io/pipe_write.c
index c44836783..0c8c2bd12 100644
--- a/core/src/xmake/io/pipe_write.c
+++ b/core/src/xmake/io/pipe_write.c
@@ -52,29 +52,11 @@ tb_int_t xm_io_pipe_write(lua_State* lua)
tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(pipefile, 0);
- // get data
+ // get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
- if (lua_istable(lua, 2))
- {
- // get data address
- lua_pushstring(lua, "data");
- lua_gettable(lua, 2);
- data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, -1);
- lua_pop(lua, 1);
-
- // get data size
- lua_pushstring(lua, "size");
- lua_gettable(lua, 2);
- size = (tb_size_t)lua_tonumber(lua, -1);
- lua_pop(lua, 1);
- }
- else
- {
- size_t datasize = 0;
- data = (tb_byte_t const*)luaL_checklstring(lua, 2, &datasize);
- size = (tb_size_t)datasize;
- }
+ if (lua_isnumber(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 2);
+ if (lua_isnumber(lua, 3)) size = (tb_size_t)lua_tonumber(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
@@ -82,28 +64,8 @@ tb_int_t xm_io_pipe_write(lua_State* lua)
return 2;
}
- // get start
- tb_long_t start = 1;
- if (lua_isnumber(lua, 3)) start = (tb_long_t)lua_tonumber(lua, 3);
- if (start < 1 || start > size)
- {
- lua_pushinteger(lua, -1);
- lua_pushfstring(lua, "invalid start position(%d)!", (tb_int_t)start);
- return 2;
- }
-
- // get last
- tb_long_t last = (tb_long_t)size;
- if (lua_isnumber(lua, 4)) last = (tb_long_t)lua_tonumber(lua, 4);
- if (last < start - 1 || last > size + start - 1)
- {
- lua_pushinteger(lua, -1);
- lua_pushfstring(lua, "invalid last position(%d)!", (tb_int_t)last);
- return 2;
- }
-
// write data
- tb_long_t real = tb_pipe_file_write(pipefile, data + start - 1, last - start + 1);
+ tb_long_t real = tb_pipe_file_write(pipefile, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c
index f203df5b6..de3b39b75 100644
--- a/core/src/xmake/io/socket_sendto.c
+++ b/core/src/xmake/io/socket_sendto.c
@@ -52,29 +52,11 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
- // get data
+ // get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
- if (lua_istable(lua, 2))
- {
- // get data address
- lua_pushstring(lua, "data");
- lua_gettable(lua, 2);
- data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, -1);
- lua_pop(lua, 1);
-
- // get data size
- lua_pushstring(lua, "size");
- lua_gettable(lua, 2);
- size = (tb_size_t)lua_tonumber(lua, -1);
- lua_pop(lua, 1);
- }
- else
- {
- size_t datasize = 0;
- data = (tb_byte_t const*)luaL_checklstring(lua, 2, &datasize);
- size = (tb_size_t)datasize;
- }
+ if (lua_isnumber(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 2);
+ if (lua_isnumber(lua, 3)) size = (tb_size_t)lua_tonumber(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
@@ -83,8 +65,8 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
}
// get address
- tb_char_t const* addr = lua_tostring(lua, 3);
- tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 4);
+ tb_char_t const* addr = lua_tostring(lua, 4);
+ tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 5);
if (!addr || !port)
{
lua_pushinteger(lua, -1);
@@ -93,7 +75,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua)
}
// get address family
- tb_size_t family = (tb_size_t)luaL_checknumber(lua, 5);
+ tb_size_t family = (tb_size_t)luaL_checknumber(lua, 6);
// init ip address
tb_ipaddr_t ipaddr;
diff --git a/xmake/core/base/pipe.lua b/xmake/core/base/pipe.lua
index c204f4f07..ddf62411d 100644
--- a/xmake/core/base/pipe.lua
+++ b/xmake/core/base/pipe.lua
@@ -67,21 +67,22 @@ function _instance:write(data, opt)
return -1, errors
end
- -- data is bytes? table.unpack the raw address
- local datasize = #data
- if bytes.instance_of(data) then
- datasize = data:size()
- data = {data = data:caddr(), size = data:size()}
+ -- get data address and size for bytes and string
+ if type(data) == "string" then
+ data = bytes(data)
end
+ local datasize = data:size()
+ local dataaddr = data:caddr()
-- init start and last
opt = opt or {}
local start = opt.start or 1
local last = opt.last or datasize
-
- -- check start and last
- if start > last or start < 1 then
- return -1, string.format("%s: invalid start(%d) and last(%d)!", self, start, last)
+ if start < 1 or start > datasize then
+ return -1, string.format("%s: invalid start(%d)!", self, start)
+ end
+ if last < start - 1 or last > datasize + start - 1 then
+ return -1, string.format("%s: invalid last(%d)!", self, last)
end
-- write it
@@ -91,7 +92,7 @@ function _instance:write(data, opt)
if opt.block then
local size = last + 1 - start
while start <= last do
- real, errors = io.pipe_write(self:cdata(), data, start, last)
+ real, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
if real > 0 then
write = write + real
start = start + real
@@ -109,7 +110,7 @@ function _instance:write(data, opt)
write = -1
end
else
- write, errors = io.pipe_write(self:cdata(), data, start, last)
+ write, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
if write < 0 and errors then
errors = string.format("%s: %s", self, errors)
end
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 8fe895a67..f2866ccfd 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -471,19 +471,31 @@ function _instance:sendto(data, addr, port, opt)
return -1, string.format("%s: sendto empty address!", self)
end
- -- data is bytes? table.unpack the raw address
- if bytes.instance_of(data) then
- data = {data = data:caddr(), size = data:size()}
+ -- get data address and size for bytes and string
+ if type(data) == "string" then
+ data = bytes(data)
end
+ local datasize = data:size()
+ local dataaddr = data:caddr()
- -- send it
+ -- init start and last
opt = opt or {}
+ local start = opt.start or 1
+ local last = opt.last or datasize
+ if start < 1 or start > datasize then
+ return -1, string.format("%s: invalid start(%d)!", self, start)
+ end
+ if last < start - 1 or last > datasize + start - 1 then
+ return -1, string.format("%s: invalid last(%d)!", self, last)
+ end
+
+ -- send it
local send = 0
local wait = false
local errors = nil
if opt.block then
while true do
- send, errors = io.socket_sendto(self:cdata(), data, addr, port, self:family())
+ send, errors = io.socket_sendto(self:cdata(), dataaddr + start - 1, last + 1 - start, addr, port, self:family())
if send == 0 and not wait then
local events, waiterrs = _instance.wait(self, socket.EV_SEND, opt.timeout or -1)
if events == socket.EV_SEND then
@@ -497,7 +509,7 @@ function _instance:sendto(data, addr, port, opt)
end
end
else
- send, errors = io.socket_sendto(self:cdata(), data, addr, port, self:family())
+ send, errors = io.socket_sendto(self:cdata(), dataaddr + start - 1, last + 1 - start, addr, port, self:family())
if send < 0 and errors then
errors = string.format("%s: %s", self, errors)
end