Diff

net/server_select.lua @ 8528:67311cda0625

net.server_select: Better detection of errors for outgoing connections On connection failure, a socket is marked readable and writable. So to detect initial connection failures (connection refused, etc.) we now watch for sockets becoming readable during initial connection, and also read from readable sockets before writing to writable sockets. This should fix 'onconnect' being called for outgoing connections that actually failed.
author Matthew Wild <mwild1@gmail.com>
date Fri, 23 Feb 2018 15:30:00 +0000
parent 7745:6d6a04628954
child 8728:41c959c5c84b
line wrap: on
line diff
--- a/net/server_select.lua	Fri Feb 23 14:07:16 2018 +0100
+++ b/net/server_select.lua	Fri Feb 23 15:30:00 2018 +0000
@@ -905,6 +905,15 @@
 		end
 
 		local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
+		for _, socket in ipairs( read ) do -- receive data
+			local handler = _socketlist[ socket ]
+			if handler then
+				handler.readbuffer( )
+			else
+				closesocket( socket )
+				out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
+			end
+		end
 		for _, socket in ipairs( write ) do -- send data waiting in writequeues
 			local handler = _socketlist[ socket ]
 			if handler then
@@ -914,15 +923,6 @@
 				out_put "server.lua: found no handler and closed socket (writelist)"	-- this should not happen
 			end
 		end
-		for _, socket in ipairs( read ) do -- receive data
-			local handler = _socketlist[ socket ]
-			if handler then
-				handler.readbuffer( )
-			else
-				closesocket( socket )
-				out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
-			end
-		end
 		for handler, err in pairs( _closelist ) do
 			handler.disconnect( )( handler, err )
 			handler:force_close()	 -- forced disconnect
@@ -978,6 +978,7 @@
 	if not handler then return nil, err end
 	_socketlist[ socket ] = handler
 	if not sslctx then
+		_readlistlen = addsocket(_readlist, socket, _readlistlen)
 		_sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
 		if listeners.onconnect then
 			-- When socket is writeable, call onconnect