Diff

net/server_select.lua @ 6539:f923140ee7c5

net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
author daurnimator <quae@daurnimator.com>
date Thu, 15 Jan 2015 09:03:00 -0500
parent 6481:dbc72cd1332e
child 6540:31a8b3bfb31d
line wrap: on
line diff
--- a/net/server_select.lua	Fri Jan 16 12:06:42 2015 -0500
+++ b/net/server_select.lua	Thu Jan 15 09:03:00 2015 -0500
@@ -115,8 +115,6 @@
 local _sendtimeout
 local _readtimeout
 
-local _timer
-
 local _maxselectlen
 local _maxfd
 
@@ -890,8 +888,15 @@
 loop = function(once) -- this is the main loop of the program
 	if quitting then return "quitting"; end
 	if once then quitting = "once"; end
-	local next_timer_time = math_huge;
+	_currenttime = luasocket_gettime( )
 	repeat
+		-- Fire timers
+		local next_timer_time = math_huge;
+		for i = 1, _timerlistlen do
+			local t = _timerlist[ i ]( _currenttime ) -- fire timers
+			if t then next_timer_time = math_min(next_timer_time, t); end
+		end
+
 		local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
 		for i, socket in ipairs( write ) do -- send data waiting in writequeues
 			local handler = _socketlist[ socket ]
@@ -940,18 +945,6 @@
 			end
 		end
 
-		-- Fire timers
-		if _currenttime - _timer >= math_min(next_timer_time, 1) then
-			next_timer_time = math_huge;
-			for i = 1, _timerlistlen do
-				local t = _timerlist[ i ]( _currenttime ) -- fire timers
-				if t then next_timer_time = math_min(next_timer_time, t); end
-			end
-			_timer = _currenttime
-		else
-			next_timer_time = next_timer_time - (_currenttime - _timer);
-		end
-
 		-- wait some time (0 by default)
 		socket_sleep( _sleeptime )
 	until quitting;
@@ -1037,7 +1030,6 @@
 use "setmetatable" ( _readtimes, { __mode = "k" } )
 use "setmetatable" ( _writetimes, { __mode = "k" } )
 
-_timer = luasocket_gettime( )
 _starttime = luasocket_gettime( )
 
 local function setlogger(new_logger)