add socket.disconnected(), used by socketchannle. see #715

This commit is contained in:
Cloud Wu
2017-08-15 13:44:06 +08:00
parent 90536e4a0d
commit f94ca6f4b3
3 changed files with 24 additions and 20 deletions

View File

@@ -104,17 +104,13 @@ function httpc.request(method, host, url, recvheader, header, content)
if timeout then if timeout then
skynet.timeout(timeout, function() skynet.timeout(timeout, function()
if not finish then if not finish then
local temp = fd socket.shutdown(fd) -- shutdown the socket fd, need close later.
fd = nil
socket.shutdown(temp)
end end
end) end)
end end
local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content) local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content)
finish = true finish = true
if fd then -- may close by skynet.timeout socket.close(fd)
socket.close(fd)
end
if ok then if ok then
return statuscode, body return statuscode, body
else else

View File

@@ -209,22 +209,15 @@ function socket.start(id, func)
return connect(id, func) return connect(id, func)
end end
local function close_fd(id, func) function socket.shutdown(id)
local s = socket_pool[id] local s = socket_pool[id]
if s then if s then
if s.buffer then driver.clear(s.buffer,buffer_pool)
driver.clear(s.buffer,buffer_pool) -- the framework would send SKYNET_SOCKET_TYPE_CLOSE , need close(id) later
end driver.shutdown(id)
if s.connected then
func(id)
end
end end
end end
function socket.shutdown(id)
close_fd(id, driver.shutdown)
end
function socket.close_fd(id) function socket.close_fd(id)
assert(socket_pool[id] == nil,"Use socket.close instead") assert(socket_pool[id] == nil,"Use socket.close instead")
driver.close(id) driver.close(id)
@@ -250,7 +243,7 @@ function socket.close(id)
end end
s.connected = false s.connected = false
end end
close_fd(id) -- clear the buffer (already close fd) driver.clear(s.buffer,buffer_pool)
assert(s.lock == nil or next(s.lock) == nil) assert(s.lock == nil or next(s.lock) == nil)
socket_pool[id] = nil socket_pool[id] = nil
end end
@@ -352,6 +345,13 @@ function socket.invalid(id)
return socket_pool[id] == nil return socket_pool[id] == nil
end end
function socket.disconnected(id)
local s = socket_pool[id]
if s then
return not(s.connected or s.connecting)
end
end
function socket.listen(host, port, backlog) function socket.listen(host, port, backlog)
if port == nil then if port == nil then
host, port = string.match(host, "([^:]+):(.+)$") host, port = string.match(host, "([^:]+):(.+)$")
@@ -392,10 +392,12 @@ end
-- you must call socket.start(id) later in other service -- you must call socket.start(id) later in other service
function socket.abandon(id) function socket.abandon(id)
local s = socket_pool[id] local s = socket_pool[id]
if s and s.buffer then if s then
driver.clear(s.buffer,buffer_pool) driver.clear(s.buffer,buffer_pool)
s.connected = false
wakeup(s)
socket_pool[id] = nil
end end
socket_pool[id] = nil
end end
function socket.limit(id, limit) function socket.limit(id, limit)

View File

@@ -289,6 +289,12 @@ end
local function check_connection(self) local function check_connection(self)
if self.__sock then if self.__sock then
if socket.disconnected(self.__sock[1]) then
-- closed by peer
skynet.error("socket: disconnect detected ", self.__host, self.__port)
close_channel_socket(self)
return
end
local authco = self.__authcoroutine local authco = self.__authcoroutine
if not authco then if not authco then
return true return true