summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-15 00:54:17 +0800
committerruki <[email protected]>2019-10-14 23:25:43 +0800
commit7366683acf12cef49d82a37a809c2ed6e5b81459 (patch)
tree23b50d4a87e24700e0dbde44a1729ee705cc053f /xmake
parent91bad8e2dbbe495f93cc14b56742d1af55c6d51b (diff)
add socket.accept
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/socket.lua17
-rw-r--r--xmake/core/sandbox/modules/import/core/base/socket.lua31
2 files changed, 47 insertions, 1 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index ddd89c004..bdc79faa7 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -113,6 +113,23 @@ function _instance:listen(backlog)
return result, errors
end
+-- accept socket
+function _instance:accept()
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return -1, errors
+ end
+
+ -- accept it
+ local result, errors = io.socket_accept(self._SOCK)
+ if not result and errors then
+ errors = string.format("%s: %s", self, errors)
+ end
+ return result, errors
+end
+
-- connect socket
function _instance:connect(addr, port)
diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua
index cca8b5227..b68b4a560 100644
--- a/xmake/core/sandbox/modules/import/core/base/socket.lua
+++ b/xmake/core/sandbox/modules/import/core/base/socket.lua
@@ -47,7 +47,7 @@ function sandbox_core_base_socket_instance.bind(sock, addr, port)
end
-- listen socket
-function sandbox_core_base_socket_instance.listen(backlog)
+function sandbox_core_base_socket_instance.listen(sock, backlog)
local result, errors = sock:_listen(backlog)
if not result and errors then
raise(errors)
@@ -55,6 +55,15 @@ function sandbox_core_base_socket_instance.listen(backlog)
return result
end
+-- accept socket
+function sandbox_core_base_socket_instance.accept(sock)
+ local result, errors = sock:_accept()
+ if not result and errors then
+ raise(errors)
+ end
+ return result
+end
+
-- connect socket
function sandbox_core_base_socket_instance.connect(sock, addr, port)
local result, errors = sock:_connect(addr, port)
@@ -127,6 +136,26 @@ function sandbox_core_base_socket.bind(addr, port, opt)
raise("bind %s:%s failed!", addr, port)
end
+-- open and accept tcp socket
+function sandbox_core_base_socket.accept(addr, port, opt)
+ opt = opt or {}
+ local sock = socket.bind(addr, port, opt)
+ sock:listen()
+ local sock_client = nil
+ repeat
+ local ok = sock:wait(socket.EV_ACPT, opt.timeout or -1)
+ if ok == socket.EV_ACPT then
+ sock_client = sock:accept()
+ end
+ until sock_client ~= nil
+ if ok > 0 then
+ return sock
+ else
+ sock:close()
+ raise("connect %s:%s failed!", addr, port)
+ end
+end
+
-- open and connect tcp socket
function sandbox_core_base_socket.connect(addr, port, opt)
opt = opt or {}