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 /xmake/core/base/socket.lua | |
| parent | f2a2f516c29eeb5a3d9475903dd3b4ed8c3df45d (diff) | |
improve socket type and family
Diffstat (limited to 'xmake/core/base/socket.lua')
| -rw-r--r-- | xmake/core/base/socket.lua | 26 |
1 files changed, 18 insertions, 8 deletions
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) |
