net.server_select: When an SSL handshake is connected, if there is pending data to...
[prosody.git] / net / server_select.lua
index 98e9f8475cf8c932b90742dac26b01bd6054b3c2..33c18a606140b316ecc55045104bac1750e683a7 100644 (file)
@@ -1,7 +1,7 @@
--- 
+--
 -- server.lua by blastbeat of the luadch project
 -- Re-used here under the MIT/X Consortium License
--- 
+--
 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain
 --
 
@@ -284,6 +284,8 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
        local status = listeners.onstatus
        local disconnect = listeners.ondisconnect
        local drain = listeners.ondrain
+       local onreadtimeout = listeners.onreadtimeout;
+       local detach = listeners.ondetach
 
        local bufferqueue = { } -- buffer array
        local bufferqueuelen = 0        -- end of buffer array
@@ -312,11 +314,18 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
        handler.disconnect = function( )
                return disconnect
        end
+       handler.onreadtimeout = onreadtimeout;
+
        handler.setlistener = function( self, listeners )
+               if detach then
+                       detach(self) -- Notify listener that it is no longer responsible for this connection
+               end
                dispatch = listeners.onincoming
                disconnect = listeners.ondisconnect
                status = listeners.onstatus
                drain = listeners.ondrain
+               handler.onreadtimeout = listeners.onreadtimeout
+               detach = listeners.ondetach
        end
        handler.getstats = function( )
                return readtraffic, sendtraffic
@@ -397,6 +406,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
        handler.clientport = function( )
                return clientport
        end
+       handler.port = handler.clientport -- COMPAT server_event
        local write = function( self, data )
                bufferlen = bufferlen + #data
                if bufferlen > maxsendlen then
@@ -558,6 +568,9 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
                                                _ = status and status( handler, "ssl-handshake-complete" )
                                                if self.autostart_ssl and listeners.onconnect then
                                                        listeners.onconnect(self);
+                                                       if bufferqueuelen ~= 0 then
+                                                               _sendlistlen = addsocket(_sendlist, client, _sendlistlen)
+                                                       end
                                                end
                                                _readlistlen = addsocket(_readlist, client, _readlistlen)
                                                return true
@@ -607,7 +620,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
                        shutdown = id
                        _socketlist[ socket ] = handler
                        _readlistlen = addsocket(_readlist, socket, _readlistlen)
-                       
+
                        -- remove traces of the old socket
                        _readlistlen = removesocket( _readlist, oldsocket, _readlistlen )
                        _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen )
@@ -695,7 +708,7 @@ local function link(sender, receiver, buffersize)
                        sender_locked = nil;
                end
        end
-       
+
        local _readbuffer = sender.readbuffer;
        function sender.readbuffer()
                _readbuffer();
@@ -704,6 +717,7 @@ local function link(sender, receiver, buffersize)
                        sender:lock_read(true);
                end
        end
+       sender:set_mode("*a");
 end
 
 ----------------------------------// PUBLIC //--
@@ -916,13 +930,9 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx
                        -- When socket is writeable, call onconnect
                        local _sendbuffer = handler.sendbuffer;
                        handler.sendbuffer = function ()
-                               _sendlistlen = removesocket( _sendlist, socket, _sendlistlen );
                                handler.sendbuffer = _sendbuffer;
                                listeners.onconnect(handler);
-                               -- If there was data with the incoming packet, handle it now.
-                               if #handler:bufferqueue() > 0 then
-                                       return _sendbuffer();
-                               end
+                               return _sendbuffer(); -- Send any queued outgoing data
                        end
                end
        end
@@ -937,9 +947,9 @@ local addclient = function( address, port, listeners, pattern, sslctx )
        client:settimeout( 0 )
        _, err = client:connect( address, port )
        if err then -- try again
-               local handler = wrapclient( client, address, port, listeners )
+               return wrapclient( client, address, port, listeners, pattern, sslctx )
        else
-               wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx )
+               return wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx )
        end
 end
 
@@ -969,7 +979,7 @@ return {
 
        addclient = addclient,
        wrapclient = wrapclient,
-       
+
        loop = loop,
        link = link,
        step = step,