Comparison

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
comparison
equal deleted inserted replaced
6538:f1eb66288f60 6539:f923140ee7c5
112 local _maxreadlen 112 local _maxreadlen
113 113
114 local _checkinterval 114 local _checkinterval
115 local _sendtimeout 115 local _sendtimeout
116 local _readtimeout 116 local _readtimeout
117
118 local _timer
119 117
120 local _maxselectlen 118 local _maxselectlen
121 local _maxfd 119 local _maxfd
122 120
123 local _maxsslhandshake 121 local _maxsslhandshake
888 end 886 end
889 887
890 loop = function(once) -- this is the main loop of the program 888 loop = function(once) -- this is the main loop of the program
891 if quitting then return "quitting"; end 889 if quitting then return "quitting"; end
892 if once then quitting = "once"; end 890 if once then quitting = "once"; end
893 local next_timer_time = math_huge; 891 _currenttime = luasocket_gettime( )
894 repeat 892 repeat
893 -- Fire timers
894 local next_timer_time = math_huge;
895 for i = 1, _timerlistlen do
896 local t = _timerlist[ i ]( _currenttime ) -- fire timers
897 if t then next_timer_time = math_min(next_timer_time, t); end
898 end
899
895 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) ) 900 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
896 for i, socket in ipairs( write ) do -- send data waiting in writequeues 901 for i, socket in ipairs( write ) do -- send data waiting in writequeues
897 local handler = _socketlist[ socket ] 902 local handler = _socketlist[ socket ]
898 if handler then 903 if handler then
899 handler.sendbuffer( ) 904 handler.sendbuffer( )
936 else 941 else
937 _readtimes[ handler ] = _currenttime -- reset timer 942 _readtimes[ handler ] = _currenttime -- reset timer
938 end 943 end
939 end 944 end
940 end 945 end
941 end
942
943 -- Fire timers
944 if _currenttime - _timer >= math_min(next_timer_time, 1) then
945 next_timer_time = math_huge;
946 for i = 1, _timerlistlen do
947 local t = _timerlist[ i ]( _currenttime ) -- fire timers
948 if t then next_timer_time = math_min(next_timer_time, t); end
949 end
950 _timer = _currenttime
951 else
952 next_timer_time = next_timer_time - (_currenttime - _timer);
953 end 946 end
954 947
955 -- wait some time (0 by default) 948 -- wait some time (0 by default)
956 socket_sleep( _sleeptime ) 949 socket_sleep( _sleeptime )
957 until quitting; 950 until quitting;
1035 1028
1036 use "setmetatable" ( _socketlist, { __mode = "k" } ) 1029 use "setmetatable" ( _socketlist, { __mode = "k" } )
1037 use "setmetatable" ( _readtimes, { __mode = "k" } ) 1030 use "setmetatable" ( _readtimes, { __mode = "k" } )
1038 use "setmetatable" ( _writetimes, { __mode = "k" } ) 1031 use "setmetatable" ( _writetimes, { __mode = "k" } )
1039 1032
1040 _timer = luasocket_gettime( )
1041 _starttime = luasocket_gettime( ) 1033 _starttime = luasocket_gettime( )
1042 1034
1043 local function setlogger(new_logger) 1035 local function setlogger(new_logger)
1044 local old_logger = log; 1036 local old_logger = log;
1045 if new_logger then 1037 if new_logger then