Changeset

3382:fea0f8e19e4c

net.server_select: Ensure that onconnect is called for server sockets as well as clients, rather than onincoming. Fixes mod_console traceback, issue #197
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Jul 2010 22:28:50 +0100
parents 3381:28cb5ad72870
children 3383:c7acddf80583
files net/server_select.lua
diffstat 1 files changed, 13 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/net/server_select.lua	Fri Jul 16 16:53:54 2010 +0100
+++ b/net/server_select.lua	Fri Jul 16 22:28:50 2010 +0100
@@ -167,7 +167,7 @@
 
 	local connections = 0
 
-	local dispatch, disconnect = listeners.onincoming, listeners.ondisconnect
+	local dispatch, disconnect = listeners.onconnect or listeners.onincoming, listeners.ondisconnect
 
 	local accept = socket.accept
 
@@ -623,16 +623,6 @@
 
 	_socketlist[ socket ] = handler
 	_readlistlen = addsocket(_readlist, socket, _readlistlen)
-	if listeners.onconnect then
-		_sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
-		handler.sendbuffer = function ()
-			listeners.onconnect(handler);
-			handler.sendbuffer = _sendbuffer;
-			if bufferqueuelen > 0 then
-				return _sendbuffer();
-			end
-		end
-	end
 	return handler, socket
 end
 
@@ -854,6 +844,18 @@
 	local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx )
 	_socketlist[ socket ] = handler
 	_sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
+	if listeners.onconnect then
+		-- When socket is writeable, call onconnect
+		local _sendbuffer = handler.sendbuffer;
+		handler.sendbuffer = function ()
+			listeners.onconnect(handler);
+			handler.sendbuffer = _sendbuffer;
+			-- If there was data with the incoming packet, handle it now.
+			if #handler:bufferqueue() > 0 then
+				return _sendbuffer();
+			end
+		end
+	end
 	return handler, socket
 end