diff options
| author | ruki <[email protected]> | 2019-10-15 22:16:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-15 07:39:18 +0800 |
| commit | 194434ebd67911dfd8352bc8c50b463b191e85c1 (patch) | |
| tree | bad56d85dfde8cdba4e65d3b268e71a706177c2d | |
| parent | 3c4ea7584702e760400d5cbd27e8260e3ad8a560 (diff) | |
fix socket.accept
| -rw-r--r-- | xmake/core/base/socket.lua | 3 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 53 |
2 files changed, 22 insertions, 34 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index bdc79faa7..09fd00646 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -127,6 +127,9 @@ function _instance:accept() if not result and errors then errors = string.format("%s: %s", self, errors) end + if result then + result = _instance.new(self:type(), self:family(), result) + end return result, errors end diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index e6d8a1044..b401e47ed 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -43,6 +43,23 @@ sandbox_core_base_socket.EV_SEND = socket.EV_SEND sandbox_core_base_socket.EV_CONN = socket.EV_SEND sandbox_core_base_socket.EV_ACPT = socket.EV_RECV +-- wrap socket +function _socket_wrap(sock) + + -- hook socket interfaces + local hooked = {} + for name, func in pairs(sandbox_core_base_socket_instance) do + if not name:startswith("_") and type(func) == "function" then + hooked["_" .. name] = sock["_" .. name] or sock[name] + hooked[name] = func + end + end + for name, func in pairs(hooked) do + sock[name] = func + end + return sock +end + -- wait socket events function sandbox_core_base_socket_instance.wait(sock, events, timeout) local result, errors = sock:_wait(events, timeout) @@ -76,7 +93,7 @@ function sandbox_core_base_socket_instance.accept(sock) if not result and errors then raise(errors) end - return result + return result and _socket_wrap(result) or nil end -- connect socket @@ -113,19 +130,7 @@ function sandbox_core_base_socket.open(socktype, family) if not sock then raise(errors) end - - -- hook socket interfaces - local hooked = {} - for name, func in pairs(sandbox_core_base_socket_instance) do - if not name:startswith("_") and type(func) == "function" then - hooked["_" .. name] = sock["_" .. name] or sock[name] - hooked[name] = func - end - end - for name, func in pairs(hooked) do - sock[name] = func - end - return sock + return _socket_wrap(sock) end -- open tcp socket @@ -151,26 +156,6 @@ function sandbox_core_base_socket.bind(addr, port, opt) raise("bind %s:%s failed!", addr, port) end --- open and accept tcp socket -function sandbox_core_base_socket.accept(addr, port, opt) - opt = opt or {} - local sock = socket.bind(addr, port, opt) - sock:listen() - local sock_client = nil - repeat - local ok = sock:wait(socket.EV_ACPT, opt.timeout or -1) - if ok == socket.EV_ACPT then - sock_client = sock:accept() - end - until sock_client ~= nil - if ok > 0 then - return sock - else - sock:close() - raise("connect %s:%s failed!", addr, port) - end -end - -- open and connect tcp socket function sandbox_core_base_socket.connect(addr, port, opt) opt = opt or {} |
