diff options
| author | ruki <[email protected]> | 2022-04-07 00:53:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-06 23:22:21 +0800 |
| commit | c48a7d959049ad14b6ea09baca682dcbaf88639c (patch) | |
| tree | b023420a92e8c74aeea35b3ce02ba55f98395729 /xmake/core/base/socket.lua | |
| parent | 79e14625b3977bf7953c2150e584332a0ea8dd16 (diff) | |
improve bytes
Diffstat (limited to 'xmake/core/base/socket.lua')
| -rw-r--r-- | xmake/core/base/socket.lua | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index f2866ccfd..25bff99d0 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -385,6 +385,7 @@ end -- recv data from socket function _instance:recv(buff, size, opt) + assert(buff) -- ensure opened local ok, errors = self:_ensure_opened() @@ -393,7 +394,8 @@ function _instance:recv(buff, size, opt) end -- check buffer - if not buff and buff:size() < size then + size = size or buff:size() + if buff:size() < size then return -1, string.format("%s: too small buffer!", self) end @@ -436,14 +438,14 @@ function _instance:recv(buff, size, opt) end end if recv == size then - data_or_errors = bytes(buff, start, recv) + data_or_errors = buff:slice(start, recv) else recv = -1 end else recv, data_or_errors = io.socket_recv(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size)) if recv > 0 then - data_or_errors = bytes(buff, start, recv) + data_or_errors = buff:slice(start, recv) end end if recv < 0 and data_or_errors then @@ -519,6 +521,7 @@ end -- recv udp data from peer function _instance:recvfrom(buff, size, opt) + assert(buff) -- ensure opened local ok, errors = self:_ensure_opened() @@ -532,7 +535,8 @@ function _instance:recvfrom(buff, size, opt) end -- check buffer - if not buff and buff:size() < size then + size = size or buff:size() + if buff:size() < size then return -1, string.format("%s: too small buffer!", self) end @@ -559,7 +563,7 @@ function _instance:recvfrom(buff, size, opt) while true do recv, data_or_errors, addr, port = io.socket_recvfrom(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size)) if recv > 0 then - data_or_errors = bytes(buff, start, recv) + data_or_errors = buff:slice(start, recv) break elseif recv == 0 and not wait then local events, waiterrs = _instance.wait(self, socket.EV_RECV, opt.timeout or -1) @@ -577,7 +581,7 @@ function _instance:recvfrom(buff, size, opt) else recv, data_or_errors, addr, port = io.socket_recvfrom(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size)) if recv > 0 then - data_or_errors = bytes(buff, start, recv) + data_or_errors = buff:slice(start, recv) end end if recv < 0 and data_or_errors then |
