diff options
| author | ruki <[email protected]> | 2019-10-14 22:15:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-14 07:30:32 +0800 |
| commit | 4041c5a3a47da126f7a97e09cee7b5b3f3fd8978 (patch) | |
| tree | 03d3b8d20d18e5446a90f6c58af6b7d87bd6b624 | |
| parent | f2a2f516c29eeb5a3d9475903dd3b4ed8c3df45d (diff) | |
improve socket type and family
| -rw-r--r-- | core/src/xmake/io/socket_connect.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_open.c | 27 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 26 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 24 |
4 files changed, 40 insertions, 41 deletions
diff --git a/core/src/xmake/io/socket_connect.c b/core/src/xmake/io/socket_connect.c index b7826019f..5c8f81e5c 100644 --- a/core/src/xmake/io/socket_connect.c +++ b/core/src/xmake/io/socket_connect.c @@ -56,11 +56,11 @@ tb_int_t xm_io_socket_connect(lua_State* lua) tb_size_t port = (tb_size_t)luaL_checknumber(lua, 3); // get family - tb_char_t const* family = lua_tostring(lua, 4); + tb_size_t family = (tb_size_t)luaL_checknumber(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); + tb_ipaddr_set(&addr, address, port, family); // connect socket lua_pushnumber(lua, (tb_int_t)tb_socket_connect(sock, &addr)); diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c index 706295ff3..fbfd7a3c7 100644 --- a/core/src/xmake/io/socket_open.c +++ b/core/src/xmake/io/socket_open.c @@ -43,28 +43,27 @@ 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 = lua_tostring(lua, 1); + tb_size_t socktype = (tb_size_t)luaL_checknumber(lua, 1); // get address family - tb_char_t const* family = lua_tostring(lua, 2); + tb_size_t family = (tb_size_t)luaL_checknumber(lua, 2); // map socket type - tb_size_t t = TB_SOCKET_TYPE_TCP; - if (socktype) + switch (socktype) { - if (!tb_strcmp(socktype, "udp")) - t = TB_SOCKET_TYPE_UDP; - else if (!tb_strcmp(socktype, "icmp")) - t = TB_SOCKET_TYPE_ICMP; + case 2: + socktype = TB_SOCKET_TYPE_UDP; + break; + case 3: + socktype = TB_SOCKET_TYPE_ICMP; + break; + default: + socktype = TB_SOCKET_TYPE_TCP; + break; } - // map address family - tb_size_t f = TB_IPADDR_FAMILY_IPV4; - if (family && !tb_strcmp(family, "ipv6")) - f = TB_IPADDR_FAMILY_IPV6; - // init socket - tb_socket_ref_t sock = tb_socket_init(t, f); + tb_socket_ref_t sock = tb_socket_init(socktype, family); if (sock) lua_pushlightuserdata(lua, (tb_pointer_t)sock); else lua_pushnil(lua); return 1; diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 97a04bb8b..0ff3ab8b0 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -19,22 +19,31 @@ -- -- define module -local io = io or {} local socket = socket or {} local _instance = _instance or {} -- load modules +local io = require("base/io") local table = require("base/table") local string = require("base/string") +-- the socket types +socket.TCP = 1 +socket.UDP = 2 +socket.ICMP = 3 + +-- the socket families +socket.IPV4 = 1 +socket.IPV6 = 2 + -- new a socket function _instance.new(socktype, family, sock) - local socket = table.inherit(_instance) - socket._SOCK = sock - socket._TYPE = socktype or "tcp" - socket._FAMILY = family or "ipv4" - setmetatable(socket, _instance) - return socket + local instance = table.inherit(_instance) + instance._SOCK = sock + instance._TYPE = socktype or socket.TCP + instance._FAMILY = family or socket.IPV4 + setmetatable(instance, _instance) + return instance end -- get socket type @@ -109,7 +118,8 @@ end -- tostring(socket) function _instance:__tostring() local rawfd = self:rawfd() or "closed" - return string.format("<socket: %s%s/%s>", self:type(), self:family() == "ipv6" and "6" or "4", rawfd) + local types = {"tcp", "udp", "icmp"} + return string.format("<socket: %s%s/%s>", types[self:type()], self:family() == socket.IPV6 and "6" or "4", rawfd) end -- gc(socket) diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index e13d55fca..fa9114699 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -77,29 +77,19 @@ function sandbox_core_base_socket.open(socktype, family) return sock end --- open tcp/ipv4 socket -function sandbox_core_base_socket.open_tcp4() - return sandbox_core_base_socket.open("tcp", "ipv4") -end - --- open tcp/ipv6 socket -function sandbox_core_base_socket.open_tcp6() - return sandbox_core_base_socket.open("tcp", "ipv6") -end - -- open udp/ipv4 socket -function sandbox_core_base_socket.open_udp4() - return sandbox_core_base_socket.open("udp", "ipv4") +function sandbox_core_base_socket.udp4() + return sandbox_core_base_socket.open(socket.UDP, socket.IPV4) end -- open udp/ipv6 socket -function sandbox_core_base_socket.open_udp6() - return sandbox_core_base_socket.open("udp", "ipv6") +function sandbox_core_base_socket.udp6() + return sandbox_core_base_socket.open(socket.UDP, socket.IPV6) end -- open and connect tcp/ipv4 socket function sandbox_core_base_socket.connect4(addr, port, timeout) - local sock = sandbox_core_base_socket.open_tcp4() + local sock = sandbox_core_base_socket.open(socket.TCP, socket.IPV4) local ok = 0 repeat ok = sock:connect(addr, port) @@ -114,8 +104,8 @@ function sandbox_core_base_socket.connect4(addr, port, timeout) end -- open and connect tcp/ipv6 socket -function sandbox_core_base_socket.connect6(addr, port) - local sock = sandbox_core_base_socket.open_tcp6() +function sandbox_core_base_socket.connect6(addr, port, timeout) + local sock = sandbox_core_base_socket.open(socket.TCP, socket.IPV6) local ok = 0 repeat ok = sock:connect(addr, port) |
