summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-13 21:47:32 +0800
committerruki <[email protected]>2019-10-13 21:47:32 +0800
commit3d88cf25bc977b9c5b01e5841d1175fc40bef6bd (patch)
treef4fdd639f0e6aab31336a5ed745dcabffbff7e5a /xmake
parent577363396ad259128813f52148547fd97f89f95b (diff)
add socket.connect
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/io/socket.lua21
-rw-r--r--xmake/core/sandbox/modules/io.lua9
2 files changed, 28 insertions, 2 deletions
diff --git a/xmake/core/base/io/socket.lua b/xmake/core/base/io/socket.lua
index a6b445646..13b56ecb0 100644
--- a/xmake/core/base/io/socket.lua
+++ b/xmake/core/base/io/socket.lua
@@ -30,8 +30,8 @@ local string = require("base/string")
function _socket.new(socktype, family, sock)
local socket = table.inherit(_socket)
socket._SOCK = sock
- socket._TYPE = socktype
- socket._FAMILY = family
+ socket._TYPE = socktype or "tcp"
+ socket._FAMILY = family or "ipv4"
setmetatable(socket, _socket)
return socket
end
@@ -63,6 +63,23 @@ function _socket:rawfd()
return result, errors
end
+-- connect socket
+function _socket:connect(addr, port)
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return -1, errors
+ end
+
+ -- connect it
+ local result, errors = io.socket_connect(self._SOCK, addr, port, self:family())
+ if result < 0 and errors then
+ errors = string.format("%s: %s", self, errors)
+ end
+ return result, errors
+end
+
-- close socket
function _socket:close()
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 903d6c3b6..e58b43a98 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -168,6 +168,15 @@ function sandbox_io_filelock.close(lock)
end
end
+-- connect socket
+function sandbox_io_socket.connect(sock, addr, port)
+ local result, errors = sock:_connect(addr, port)
+ if result < 0 and errors then
+ raise(errors)
+ end
+ return result
+end
+
-- get socket rawfd
function sandbox_io_socket.rawfd(sock)
local result, errors = sock:_rawfd()