net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
authorMatthew Wild <mwild1@gmail.com>
Fri, 29 Aug 2014 11:39:56 +0100
changeset 6379 a280bd6ccce2
parent 6378 3cec0eef0b70
child 6380 4220ffb87b22
net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
net/server_event.lua
net/server_select.lua
--- a/net/server_event.lua	Tue Sep 02 17:24:25 2014 +0200
+++ b/net/server_event.lua	Fri Aug 29 11:39:56 2014 +0100
@@ -438,8 +438,11 @@
 	end
 	
 	function interface_mt:setlistener(listener)
-		self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, self.onstatus
-			= listener.onconnect, listener.ondisconnect, listener.onincoming, listener.ontimeout, listener.onstatus;
+		self:ondetach(); -- Notify listener that it is no longer responsible for this connection
+		self.onconnect, self.ondisconnect, self.onincoming,
+		self.ontimeout, self.onstatus, self.ondetach
+			= listener.onconnect, listener.ondisconnect, listener.onincoming,
+			listener.ontimeout, listener.onstatus, listener.ondetach;
 	end
 	
 	-- Stub handlers
@@ -453,6 +456,8 @@
 	end
 	function interface_mt:ondrain()
 	end
+	function interface_mt:ondetach()
+	end
 	function interface_mt:onstatus()
 	end
 end
@@ -479,6 +484,7 @@
 			onincoming = listener.onincoming;  -- will be called when client sends data
 			ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs
 			ondrain = listener.ondrain; -- called when writebuffer is empty
+			ondetach = listener.ondetach; -- called when disassociating this listener from this connection
 			onstatus = listener.onstatus; -- called for status changes (e.g. of SSL/TLS)
 			eventread = false, eventwrite = false, eventclose = false,
 			eventhandshake = false, eventstarthandshake = false;  -- event handler
--- a/net/server_select.lua	Tue Sep 02 17:24:25 2014 +0200
+++ b/net/server_select.lua	Fri Aug 29 11:39:56 2014 +0100
@@ -284,6 +284,7 @@
 	local status = listeners.onstatus
 	local disconnect = listeners.ondisconnect
 	local drain = listeners.ondrain
+	local detach = listener.ondetach
 
 	local bufferqueue = { } -- buffer array
 	local bufferqueuelen = 0	-- end of buffer array
@@ -313,10 +314,14 @@
 		return disconnect
 	end
 	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
+		detach = listeners.ondetach
 	end
 	handler.getstats = function( )
 		return readtraffic, sendtraffic