socket.onclose() (#1364)

* Add socket.onclose() and close socket immediately when socket channel closed by peer

* Revert read_socket(), Add SOCKET_MORE, See #1358

* remove warning log , because it's not a rare case any more. See #1358
This commit is contained in:
云风
2021-03-19 18:52:09 +08:00
committed by GitHub
parent a39c9b8b10
commit 9228b05dfe
4 changed files with 54 additions and 65 deletions

View File

@@ -123,6 +123,9 @@ socket_message[3] = function(id)
end
s.connected = false
wakeup(s)
if s.on_close then
s.on_close(id)
end
end
-- SKYNET_SOCKET_TYPE_ACCEPT = 4
@@ -485,4 +488,10 @@ function socket.warning(id, callback)
obj.on_warning = callback
end
function socket.onclose(id, callback)
local obj = socket_pool[id]
assert(obj)
obj.on_close = callback
end
return socket

View File

@@ -128,6 +128,16 @@ local function pop_response(self)
end
end
-- on close callback
local function autoclose_cb(self, fd)
local sock = self.__sock
if self.__wait_response and sock and sock[1] == fd then
-- closed by peer
skynet.error("socket closed by peer : ", self.__host, self.__port)
close_channel_socket(self)
end
end
local function push_response(self, response, co)
if self.__response then
-- response is session
@@ -170,7 +180,15 @@ local function dispatch_by_order(self)
wakeup_all(self, "channel_closed")
break
end
local ok, result_ok, result_data = pcall(get_response, func, self.__sock)
local sock = self.__sock
if not sock then
-- closed by peer
self.__result[co] = socket_error
skynet.wakeup(co)
wakeup_all(self)
break
end
local ok, result_ok, result_data = pcall(get_response, func, sock)
if ok then
self.__result[co] = result_ok
if result_ok and self.__result_data[co] then
@@ -197,6 +215,9 @@ local function dispatch_function(self)
if self.__response then
return dispatch_by_session
else
socket.onclose(self.__sock[1], function(fd)
autoclose_cb(self, fd)
end)
return dispatch_by_order
end
end