diff options
Diffstat (limited to 'xmake/core')
| -rw-r--r-- | xmake/core/base/socket.lua | 95 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 18 |
2 files changed, 113 insertions, 0 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 26ca2b2e8..06820522c 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -346,6 +346,101 @@ function _instance:recv(size, opt) return recv, data_or_errors end +-- send udp data to peer +function _instance:sendto(data, addr, port, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- check address + if not addr or not port then + return -1, string.format("%s: sendto empty address!", self) + end + + -- send it + opt = opt or {} + local send = 0 + local wait = false + local errors = nil + if opt.block then + while true do + send, errors = io.socket_sendto(self._SOCK, data, addr, port, self:family()) + if send == 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_sendto(self._SOCK, data, addr, port, self:family()) + if send < 0 and errors then + errors = string.format("%s: %s", self, errors) + end + end + return send, errors +end + +-- recv udp data from peer +function _instance:recvfrom(size, opt) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + 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 + local wait = false + local data_or_errors = nil + if opt.block then + while true do + recv, data_or_errors, addr, port = io.socket_recvfrom(self._SOCK, size) + if recv > 0 then + data_or_errors = bytes(data_or_errors) + break + elseif recv == 0 and not wait then + local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1) + if events == socket.EV_RECV then + wait = true + else + recv = -1 + data_or_errors = waiterrs + break + end + else + break + end + end + else + recv, data_or_errors, addr, port = io.socket_recvfrom(self._SOCK, size) + if recv > 0 then + data_or_errors = bytes(data_or_errors) + end + end + if recv < 0 and data_or_errors then + data_or_errors = string.format("%s: %s", self, data_or_errors) + end + return recv, data_or_errors, addr, port +end + -- wait socket events function _instance:wait(events, timeout) diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index 3d679bd52..73aa0994b 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -132,6 +132,24 @@ function sandbox_core_base_socket_instance.recv(sock, size, opt) return real, data_or_errors end +-- send udp data to peer +function sandbox_core_base_socket_instance.sendto(sock, data, addr, port, opt) + local real, errors = sock:_sendto(data, addr, port, opt) + if real < 0 and errors then + raise(errors) + end + return real +end + +-- recv udp data from peer +function sandbox_core_base_socket_instance.recvfrom(sock, size, opt) + local real, data_or_errors, addr, port = sock:_recvfrom(size, opt) + if real < 0 and data_or_errors then + raise(data_or_errors) + end + return real, data_or_errors, addr, port +end + -- get socket rawfd function sandbox_core_base_socket_instance.rawfd(sock) local result, errors = sock:_rawfd() |
