summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-27 21:45:35 +0800
committerruki <[email protected]>2019-10-27 21:45:35 +0800
commit433872bafb966f4c5d613f0a65795bf54149fdcd (patch)
tree10a3b642fb91ed4be87bfb34b3e524f3b63773b9
parentecc9fe93d544669f6ae5c8261c7e3e4601a11a26 (diff)
improve socket.send
-rw-r--r--tests/modules/socket/tcp/echo_client.lua2
-rw-r--r--xmake/core/base/socket.lua52
-rw-r--r--xmake/core/sandbox/modules/import/core/base/socket.lua4
3 files changed, 50 insertions, 8 deletions
diff --git a/tests/modules/socket/tcp/echo_client.lua b/tests/modules/socket/tcp/echo_client.lua
index d2409a210..7c88a5992 100644
--- a/tests/modules/socket/tcp/echo_client.lua
+++ b/tests/modules/socket/tcp/echo_client.lua
@@ -6,7 +6,7 @@ function main()
print("connect %s:%d ..", addr, port)
local sock = socket.connect(addr, port)
print("%s: connected!", sock)
- local send = sock:send("hello world..")
+ local send = sock:send("hello world..", {block = true})
print("%s: send %d", sock, send)
sock:close()
end
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index edf8c6a44..31f23389d 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -169,7 +169,7 @@ function _instance:connect(addr, port, opt)
end
-- send data to socket
-function _instance:send(data, start, last)
+function _instance:send(data, opt)
-- ensure opened
local ok, errors = self:_ensure_opened()
@@ -177,12 +177,47 @@ function _instance:send(data, start, last)
return -1, errors
end
+ -- init start and last
+ opt = opt or {}
+ local start = opt.start or 1
+ local last = opt.last or #data
+
+ -- 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)
+ end
+
-- send it
- local real, errors = io.socket_send(self._SOCK, data, start, last)
- if real < 0 and errors then
- errors = string.format("%s: %s", self, errors)
+ local send = 0
+ local real = 0
+ local wait = false
+ local errors = nil
+ if opt.block then
+ while start < last do
+ real, errors = io.socket_send(self._SOCK, data, start, last)
+ if real > 0 then
+ send = send + real
+ start = start + real
+ wait = false
+ elseif real == 0 and not wait then
+ local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1)
+ if events == socket.EV_SEND then
+ wait = true
+ else
+ errors = waiterrs
+ break
+ end
+ else
+ break
+ end
+ end
+ else
+ send, errors = io.socket_send(self._SOCK, data, start, last)
+ if send < 0 and errors then
+ errors = string.format("%s: %s", self, errors)
+ end
end
- return real, errors
+ return send, errors
end
-- recv data from socket
@@ -194,6 +229,13 @@ function _instance:recv(size, opt)
return -1, errors
end
+ -- check size
+ if size == 0 then
+ return 0
+ elseif size == nil or size < 0 then
+ return -1, string.format("%s: invalid size(%d)!", self, size)
+ end
+
-- recv it
opt = opt or {}
local recv = 0
diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua
index 3220837e9..bfb5769f5 100644
--- a/xmake/core/sandbox/modules/import/core/base/socket.lua
+++ b/xmake/core/sandbox/modules/import/core/base/socket.lua
@@ -106,8 +106,8 @@ function sandbox_core_base_socket_instance.connect(sock, addr, port, opt)
end
-- send data to socket
-function sandbox_core_base_socket_instance.send(sock, data, start, last)
- local real, errors = sock:_send(data, start, last)
+function sandbox_core_base_socket_instance.send(sock, data, opt)
+ local real, errors = sock:_send(data, opt)
if real < 0 and errors then
raise(errors)
end