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 /core/src | |
| parent | f2a2f516c29eeb5a3d9475903dd3b4ed8c3df45d (diff) | |
improve socket type and family
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/socket_connect.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_open.c | 27 |
2 files changed, 15 insertions, 16 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; |
