net.server_select: Add server.step() to run through a single iteration of the event loop
authorMatthew Wild <mwild1@gmail.com>
Thu, 09 Sep 2010 20:10:28 +0100
changeset 3491 e8c06d20a18b
parent 3490 957b40dc6d7c
child 3492 6d782b1fcc8a
net.server_select: Add server.step() to run through a single iteration of the event loop
net/server_select.lua
--- a/net/server_select.lua	Sun Sep 05 19:28:37 2010 +0100
+++ b/net/server_select.lua	Thu Sep 09 20:10:28 2010 +0100
@@ -787,15 +787,16 @@
 	return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen
 end
 
-local dontstop = true; -- thinking about tomorrow, ...
+local quitting;
 
 setquitting = function (quit)
-	dontstop = not quit;
-	return;
+	quitting = not not quit;
 end
 
-loop = function( ) -- this is the main loop of the program
-	while dontstop do
+loop = function(once) -- this is the main loop of the program
+	if quitting then return "quitting"; end
+	if once then quitting = "once"; end
+	repeat
 		local read, write, err = socket_select( _readlist, _sendlist, _selecttimeout )
 		for i, socket in ipairs( write ) do -- send data waiting in writequeues
 			local handler = _socketlist[ socket ]
@@ -829,10 +830,15 @@
 		end
 		socket_sleep( _sleeptime ) -- wait some time
 		--collectgarbage( )
-	end
+	until quitting;
+	if once and quitting == "once" then quitting = nil; return; end
 	return "quitting"
 end
 
+step = function ()
+	return loop(true);
+end
+
 local function get_backend()
 	return "select";
 end