summaryrefslogtreecommitdiff
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
parent577363396ad259128813f52148547fd97f89f95b (diff)
add socket.connect
-rw-r--r--core/src/xmake/io/socket_connect.c69
-rw-r--r--core/src/xmake/io/socket_open.c31
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--xmake/core/base/io/socket.lua21
-rw-r--r--xmake/core/sandbox/modules/io.lua9
6 files changed, 112 insertions, 21 deletions
diff --git a/core/src/xmake/io/socket_connect.c b/core/src/xmake/io/socket_connect.c
new file mode 100644
index 000000000..b7826019f
--- /dev/null
+++ b/core/src/xmake/io/socket_connect.c
@@ -0,0 +1,69 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file socket_connect.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "socket_connect"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * interfaces
+ */
+
+// io.socket_connect(sock, addr, port, family)
+tb_int_t xm_io_socket_connect(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // is user data?
+ if (!lua_isuserdata(lua, 1))
+ return 0;
+
+ // get socket
+ tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1);
+ tb_check_return_val(sock, 0);
+
+ // get address
+ tb_char_t const* address = lua_tostring(lua, 2);
+ tb_assert_and_check_return_val(address, 0);
+
+ // get port
+ tb_size_t port = (tb_size_t)luaL_checknumber(lua, 3);
+
+ // get family
+ tb_char_t const* family = lua_tostring(lua, 4);
+
+ // init address
+ tb_ipaddr_t addr;
+ tb_ipaddr_set(&addr, address, port, (family && !tb_strcmp(family, "ipv6"))? TB_IPADDR_FAMILY_IPV6 : TB_IPADDR_FAMILY_IPV4);
+
+ // connect socket
+ lua_pushnumber(lua, (tb_int_t)tb_socket_connect(sock, &addr));
+ return 1;
+}
+
diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c
index 64cbea80e..706295ff3 100644
--- a/core/src/xmake/io/socket_open.c
+++ b/core/src/xmake/io/socket_open.c
@@ -43,32 +43,25 @@ tb_int_t xm_io_socket_open(lua_State* lua)
tb_assert_and_check_return_val(lua, 0);
// get socket type
- tb_char_t const* socktype = luaL_checkstring(lua, 1);
- tb_assert_and_check_return_val(socktype, 0);
+ tb_char_t const* socktype = lua_tostring(lua, 1);
// get address family
- tb_char_t const* family = luaL_checkstring(lua, 2);
- tb_assert_and_check_return_val(family, 0);
+ tb_char_t const* family = lua_tostring(lua, 2);
// map socket type
- tb_size_t t = TB_SOCKET_TYPE_NONE;
- if (!tb_strcmp(socktype, "tcp"))
- t = TB_SOCKET_TYPE_TCP;
- else if (!tb_strcmp(socktype, "udp"))
- t = TB_SOCKET_TYPE_UDP;
- else if (!tb_strcmp(socktype, "icmp"))
- t = TB_SOCKET_TYPE_ICMP;
- else
- xm_io_return_error(lua, "invalid socket type!");
+ tb_size_t t = TB_SOCKET_TYPE_TCP;
+ if (socktype)
+ {
+ if (!tb_strcmp(socktype, "udp"))
+ t = TB_SOCKET_TYPE_UDP;
+ else if (!tb_strcmp(socktype, "icmp"))
+ t = TB_SOCKET_TYPE_ICMP;
+ }
// map address family
- tb_size_t f = TB_IPADDR_FAMILY_NONE;
- if (!tb_strcmp(family, "ipv4"))
- f = TB_IPADDR_FAMILY_IPV4;
- else if (!tb_strcmp(family, "ipv6"))
+ tb_size_t f = TB_IPADDR_FAMILY_IPV4;
+ if (family && !tb_strcmp(family, "ipv6"))
f = TB_IPADDR_FAMILY_IPV6;
- else
- xm_io_return_error(lua, "invalid address family!");
// init socket
tb_socket_ref_t sock = tb_socket_init(t, f);
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 1cb2b94b6..5887b0477 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -112,6 +112,7 @@ tb_int_t xm_io_filelock_close(lua_State* lua);
// the io/socket functions
tb_int_t xm_io_socket_open(lua_State* lua);
tb_int_t xm_io_socket_rawfd(lua_State* lua);
+tb_int_t xm_io_socket_connect(lua_State* lua);
tb_int_t xm_io_socket_close(lua_State* lua);
// the path functions
@@ -248,6 +249,7 @@ static luaL_Reg const g_io_functions[] =
, { "filelock_close", xm_io_filelock_close }
, { "socket_open", xm_io_socket_open }
, { "socket_rawfd", xm_io_socket_rawfd }
+, { "socket_connect", xm_io_socket_connect }
, { "socket_close", xm_io_socket_close }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index bc9b27ba2..3176a30d7 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -62,6 +62,7 @@ xmake_C_FILES += \
io/filelock_close \
io/socket_open \
io/socket_rawfd \
+ io/socket_connect \
io/socket_close \
path/relative \
path/absolute \
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()