diff options
| author | ruki <[email protected]> | 2019-10-27 21:45:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-27 21:45:35 +0800 |
| commit | 433872bafb966f4c5d613f0a65795bf54149fdcd (patch) | |
| tree | 10a3b642fb91ed4be87bfb34b3e524f3b63773b9 /xmake/core/base/socket.lua | |
| parent | ecc9fe93d544669f6ae5c8261c7e3e4601a11a26 (diff) | |
improve socket.send
Diffstat (limited to 'xmake/core/base/socket.lua')
| -rw-r--r-- | xmake/core/base/socket.lua | 52 |
1 files changed, 47 insertions, 5 deletions
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 |
