Skip to content

Commit

Permalink
add socket.disconnected(), used by socketchannle. see #715
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Aug 15, 2017
1 parent 90536e4 commit f94ca6f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lualib/http/httpc.lua
Expand Up @@ -104,17 +104,13 @@ function httpc.request(method, host, url, recvheader, header, content)
if timeout then

This comment has been minimized.

Copy link
@Ws314

Ws314 Jun 21, 2019

按时发放

skynet.timeout(timeout, function()
if not finish then
local temp = fd
fd = nil
socket.shutdown(temp)
socket.shutdown(fd) -- shutdown the socket fd, need close later.
end
end)
end
local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content)
finish = true
if fd then -- may close by skynet.timeout
socket.close(fd)
end
socket.close(fd)
if ok then
return statuscode, body
else
Expand Down
30 changes: 16 additions & 14 deletions lualib/skynet/socket.lua
Expand Up @@ -209,22 +209,15 @@ function socket.start(id, func)
return connect(id, func)
end

local function close_fd(id, func)
function socket.shutdown(id)
local s = socket_pool[id]
if s then
if s.buffer then
driver.clear(s.buffer,buffer_pool)
end
if s.connected then
func(id)
end
driver.clear(s.buffer,buffer_pool)
-- the framework would send SKYNET_SOCKET_TYPE_CLOSE , need close(id) later
driver.shutdown(id)
end
end

function socket.shutdown(id)
close_fd(id, driver.shutdown)
end

function socket.close_fd(id)
assert(socket_pool[id] == nil,"Use socket.close instead")
driver.close(id)
Expand All @@ -250,7 +243,7 @@ function socket.close(id)
end
s.connected = false
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)
socket_pool[id] = nil
end
Expand Down Expand Up @@ -352,6 +345,13 @@ function socket.invalid(id)
return socket_pool[id] == nil
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)
if port == nil then
host, port = string.match(host, "([^:]+):(.+)$")
Expand Down Expand Up @@ -392,10 +392,12 @@ end
-- you must call socket.start(id) later in other service
function socket.abandon(id)
local s = socket_pool[id]
if s and s.buffer then
if s then
driver.clear(s.buffer,buffer_pool)
s.connected = false
wakeup(s)
socket_pool[id] = nil
end
socket_pool[id] = nil
end

function socket.limit(id, limit)
Expand Down
6 changes: 6 additions & 0 deletions lualib/skynet/socketchannel.lua
Expand Up @@ -289,6 +289,12 @@ end

local function check_connection(self)
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
if not authco then
return true
Expand Down

0 comments on commit f94ca6f

Please sign in to comment.