Skip to content

Commit f94ca6f

Browse files
committedAug 15, 2017
add socket.disconnected(), used by socketchannle. see #715
·
v1.8.0v1.1.0
1 parent 90536e4 commit f94ca6f

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed
 

‎lualib/http/httpc.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,13 @@ function httpc.request(method, host, url, recvheader, header, content)
104104
if timeout then
Code has comments. Press enter to view.
105105
skynet.timeout(timeout, function()
106106
if not finish then
107-
local temp = fd
108-
fd = nil
109-
socket.shutdown(temp)
107+
socket.shutdown(fd) -- shutdown the socket fd, need close later.
110108
end
111109
end)
112110
end
113111
local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content)
114112
finish = true
115-
if fd then -- may close by skynet.timeout
116-
socket.close(fd)
117-
end
113+
socket.close(fd)
118114
if ok then
119115
return statuscode, body
120116
else

‎lualib/skynet/socket.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,15 @@ function socket.start(id, func)
209209
return connect(id, func)
210210
end
211211

212-
local function close_fd(id, func)
212+
function socket.shutdown(id)
213213
local s = socket_pool[id]
214214
if s then
215-
if s.buffer then
216-
driver.clear(s.buffer,buffer_pool)
217-
end
218-
if s.connected then
219-
func(id)
220-
end
215+
driver.clear(s.buffer,buffer_pool)
216+
-- the framework would send SKYNET_SOCKET_TYPE_CLOSE , need close(id) later
217+
driver.shutdown(id)
221218
end
222219
end
223220

224-
function socket.shutdown(id)
225-
close_fd(id, driver.shutdown)
226-
end
227-
228221
function socket.close_fd(id)
229222
assert(socket_pool[id] == nil,"Use socket.close instead")
230223
driver.close(id)
@@ -250,7 +243,7 @@ function socket.close(id)
250243
end
251244
s.connected = false
252245
end
253-
close_fd(id) -- clear the buffer (already close fd)
246+
driver.clear(s.buffer,buffer_pool)
254247
assert(s.lock == nil or next(s.lock) == nil)
255248
socket_pool[id] = nil
256249
end
@@ -352,6 +345,13 @@ function socket.invalid(id)
352345
return socket_pool[id] == nil
353346
end
354347

348+
function socket.disconnected(id)
349+
local s = socket_pool[id]
350+
if s then
351+
return not(s.connected or s.connecting)
352+
end
353+
end
354+
355355
function socket.listen(host, port, backlog)
356356
if port == nil then
357357
host, port = string.match(host, "([^:]+):(.+)$")
@@ -392,10 +392,12 @@ end
392392
-- you must call socket.start(id) later in other service
393393
function socket.abandon(id)
394394
local s = socket_pool[id]
395-
if s and s.buffer then
395+
if s then
396396
driver.clear(s.buffer,buffer_pool)
397+
s.connected = false
398+
wakeup(s)
399+
socket_pool[id] = nil
397400
end
398-
socket_pool[id] = nil
399401
end
400402

401403
function socket.limit(id, limit)

‎lualib/skynet/socketchannel.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ end
289289

290290
local function check_connection(self)
291291
if self.__sock then
292+
if socket.disconnected(self.__sock[1]) then
293+
-- closed by peer
294+
skynet.error("socket: disconnect detected ", self.__host, self.__port)
295+
close_channel_socket(self)
296+
return
297+
end
292298
local authco = self.__authcoroutine
293299
if not authco then
294300
return true

0 commit comments

Comments
 (0)
Please sign in to comment.