summaryrefslogtreecommitdiff
path: root/xmake/core/base/socket.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-14 22:18:00 +0800
committerruki <[email protected]>2019-10-14 07:48:01 +0800
commit95df8418962d8dae106e52d35405c61bdbf4bada (patch)
treea218e6de7c39c0b81c8e6c5ea16eca61c5c02520 /xmake/core/base/socket.lua
parent4041c5a3a47da126f7a97e09cee7b5b3f3fd8978 (diff)
add socket.wait
Diffstat (limited to 'xmake/core/base/socket.lua')
-rw-r--r--xmake/core/base/socket.lua29
1 files changed, 27 insertions, 2 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 0ff3ab8b0..080cf16d9 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -36,12 +36,18 @@ socket.ICMP = 3
socket.IPV4 = 1
socket.IPV6 = 2
+-- the socket events
+socket.EV_RECV = 1
+socket.EV_SEND = 2
+socket.EV_CONN = socket.EV_SEND
+socket.EV_ACPT = socket.EV_RECV
+
-- new a socket
function _instance.new(socktype, family, sock)
local instance = table.inherit(_instance)
instance._SOCK = sock
- instance._TYPE = socktype or socket.TCP
- instance._FAMILY = family or socket.IPV4
+ instance._TYPE = socktype
+ instance._FAMILY = family
setmetatable(instance, _instance)
return instance
end
@@ -90,6 +96,23 @@ function _instance:connect(addr, port)
return result, errors
end
+-- wait socket events
+function _instance:wait(events, timeout)
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return -1, errors
+ end
+
+ -- wait it
+ local result, errors = io.socket_wait(self._SOCK, events, timeout or -1)
+ if result < 0 and errors then
+ errors = string.format("%s: %s", self, errors)
+ end
+ return result, errors
+end
+
-- close socket
function _instance:close()
@@ -137,6 +160,8 @@ end
-- @return the socket instance
--
function socket.open(socktype, family)
+ socktype = socktype or socket.TCP
+ family = family or socket.IPV4
local sock, errors = io.socket_open(socktype, family)
if sock then
return _instance.new(socktype, family, sock)