Comparison

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
comparison
equal deleted inserted replaced
8527:0a3dced117f7 8528:67311cda0625
903 local t = _timerlist[ i ]( _currenttime ) -- fire timers 903 local t = _timerlist[ i ]( _currenttime ) -- fire timers
904 if t then next_timer_time = math_min(next_timer_time, t); end 904 if t then next_timer_time = math_min(next_timer_time, t); end
905 end 905 end
906 906
907 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) ) 907 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
908 for _, socket in ipairs( read ) do -- receive data
909 local handler = _socketlist[ socket ]
910 if handler then
911 handler.readbuffer( )
912 else
913 closesocket( socket )
914 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
915 end
916 end
908 for _, socket in ipairs( write ) do -- send data waiting in writequeues 917 for _, socket in ipairs( write ) do -- send data waiting in writequeues
909 local handler = _socketlist[ socket ] 918 local handler = _socketlist[ socket ]
910 if handler then 919 if handler then
911 handler.sendbuffer( ) 920 handler.sendbuffer( )
912 else 921 else
913 closesocket( socket ) 922 closesocket( socket )
914 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen 923 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen
915 end
916 end
917 for _, socket in ipairs( read ) do -- receive data
918 local handler = _socketlist[ socket ]
919 if handler then
920 handler.readbuffer( )
921 else
922 closesocket( socket )
923 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
924 end 924 end
925 end 925 end
926 for handler, err in pairs( _closelist ) do 926 for handler, err in pairs( _closelist ) do
927 handler.disconnect( )( handler, err ) 927 handler.disconnect( )( handler, err )
928 handler:force_close() -- forced disconnect 928 handler:force_close() -- forced disconnect
976 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx ) 976 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
977 local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx ) 977 local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx )
978 if not handler then return nil, err end 978 if not handler then return nil, err end
979 _socketlist[ socket ] = handler 979 _socketlist[ socket ] = handler
980 if not sslctx then 980 if not sslctx then
981 _readlistlen = addsocket(_readlist, socket, _readlistlen)
981 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) 982 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
982 if listeners.onconnect then 983 if listeners.onconnect then
983 -- When socket is writeable, call onconnect 984 -- When socket is writeable, call onconnect
984 local _sendbuffer = handler.sendbuffer; 985 local _sendbuffer = handler.sendbuffer;
985 handler.sendbuffer = function () 986 handler.sendbuffer = function ()