diff options
| author | ruki <[email protected]> | 2019-11-06 00:40:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-11-05 22:00:30 +0800 |
| commit | fd322749574405435e4332014be8b3fa883c92d9 (patch) | |
| tree | 5ea4209b59122bc2e10c11eac806357e4d7d3b24 /xmake/core/base/socket.lua | |
| parent | 4e96ff73864ea29e5ec7730e5e08a20cf42e9b5f (diff) | |
improve poller
Diffstat (limited to 'xmake/core/base/socket.lua')
| -rw-r--r-- | xmake/core/base/socket.lua | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 6a92d802f..8992995e9 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -520,6 +520,21 @@ function _instance:__gc() end end +-- get socket events from poller +function _poller:events(sock) + return self._CACHE and self._CACHE[sock] or nil +end + +-- set socket events to poller +function _poller:events_set(sock, events) + local cache = self._CACHE + if not cache then + cache = {} + self._CACHE = cache + end + cache[sock] = events +end + -- insert socket events to poller function _poller:insert(sock, events) @@ -530,7 +545,13 @@ function _poller:insert(sock, events) end -- insert it - return io.poller_insert(sock._SOCK, events) + if not io.poller_insert(sock._SOCK, events) then + return false, string.format("insert %s events(%d) to poller failed!", sock, events) + end + + -- save this socket events and save sock/ref for gc + self:events_set(sock, events) + return true end -- modify socket events in poller @@ -543,7 +564,13 @@ function _poller:modify(sock, events) end -- modify it - return io.poller_modify(sock._SOCK, events) + if not io.poller_modify(sock._SOCK, events) then + return false, string.format("modify %s events(%d) to poller failed!", sock, events) + end + + -- update this socket events + self:events_set(sock, events) + return true end -- remove socket from poller @@ -556,7 +583,13 @@ function _poller:remove(sock) end -- remove it - return io.poller_remove(sock._SOCK) + if not io.poller_remove(sock._SOCK) then + return false, string.format("remove %s from poller failed!", sock) + end + + -- remove this socket events + self:events_set(sock, nil) + return true end -- open a socket |
