diff options
author | Matthew Wild <mwild1@gmail.com> | 2020-06-01 13:38:47 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2020-06-01 13:38:47 +0100 |
commit | 30d3969c22ef22ae5fd455a019ab0879c28f43fb (patch) | |
tree | c5940301867a9248565cc28dec381cd444aea75b | |
parent | 5abc2e6a5ce803060cd2c03182d3ae95bd29f694 (diff) | |
download | prosody-30d3969c22ef22ae5fd455a019ab0879c28f43fb.tar.gz prosody-30d3969c22ef22ae5fd455a019ab0879c28f43fb.zip |
net.server_select: Ensure onconnect is always called before onincoming
This changes the code to call onconnect when the first data is sucessfully
read or written, instead of simply when the socket first becomes writable.
A writable socket can mean a connection error, and if the client already
sent some data it may get passed to onincoming before processing writable
sockets. This fixes the issue.
-rw-r--r-- | net/server_select.lua | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/net/server_select.lua b/net/server_select.lua index 9cd3463e..de2556f0 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -289,6 +289,8 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local ssl + local pending + local dispatch = listeners.onincoming local status = listeners.onstatus local disconnect = listeners.ondisconnect @@ -343,6 +345,9 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport listeners.onattach(self, data) end end + handler._setpending = function( ) + pending = true + end handler.getstats = function( ) return readtraffic, sendtraffic end @@ -525,6 +530,12 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport _readtraffic = _readtraffic + count _readtimes[ handler ] = _currenttime --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err ) + if pending then -- connection established + pending = nil + if listeners.onconnect then + listeners.onconnect(handler) + end + end return dispatch( handler, buffer, err ) else -- connections was closed or fatal error out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) ) @@ -535,6 +546,12 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local _sendbuffer = function( ) -- this function sends data local succ, err, byte, buffer, count; if socket then + if pending then + pending = nil + if listeners.onconnect then + listeners.onconnect(handler); + end + end buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) succ, err, byte = send( socket, buffer, 1, bufferlen ) count = ( succ or byte or 0 ) * STAT_UNIT @@ -1015,17 +1032,9 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, if not handler then return nil, err end _socketlist[ socket ] = handler if not sslctx then + handler._setpending() _readlistlen = addsocket(_readlist, socket, _readlistlen) _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) - if listeners.onconnect then - -- When socket is writeable, call onconnect - local _sendbuffer = handler.sendbuffer; - handler.sendbuffer = function () - handler.sendbuffer = _sendbuffer; - listeners.onconnect(handler); - return _sendbuffer(); -- Send any queued outgoing data - end - end end return handler, socket end |