Software / code / prosody
Comparison
net/server_select.lua @ 3682:d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Thu, 02 Dec 2010 22:40:42 +0500 |
| parent | 3543:90b21508ac27 |
| child | 3685:63efe4eefa07 |
comparison
equal
deleted
inserted
replaced
| 3681:3dbdcc79bd66 | 3682:d12141cbbaa0 |
|---|---|
| 42 local string = use "string" | 42 local string = use "string" |
| 43 local coroutine = use "coroutine" | 43 local coroutine = use "coroutine" |
| 44 | 44 |
| 45 --// lua lib methods //-- | 45 --// lua lib methods //-- |
| 46 | 46 |
| 47 local os_time = os.time | |
| 48 local os_difftime = os.difftime | 47 local os_difftime = os.difftime |
| 48 local math_min = math.min | |
| 49 local math_huge = math.huge | |
| 49 local table_concat = table.concat | 50 local table_concat = table.concat |
| 50 local table_remove = table.remove | 51 local table_remove = table.remove |
| 51 local string_len = string.len | 52 local string_len = string.len |
| 52 local string_sub = string.sub | 53 local string_sub = string.sub |
| 53 local coroutine_wrap = coroutine.wrap | 54 local coroutine_wrap = coroutine.wrap |
| 55 | 56 |
| 56 --// extern libs //-- | 57 --// extern libs //-- |
| 57 | 58 |
| 58 local luasec = use "ssl" | 59 local luasec = use "ssl" |
| 59 local luasocket = use "socket" or require "socket" | 60 local luasocket = use "socket" or require "socket" |
| 61 local luasocket_gettime = luasocket.gettime | |
| 60 | 62 |
| 61 --// extern lib methods //-- | 63 --// extern lib methods //-- |
| 62 | 64 |
| 63 local ssl_wrap = ( luasec and luasec.wrap ) | 65 local ssl_wrap = ( luasec and luasec.wrap ) |
| 64 local socket_bind = luasocket.bind | 66 local socket_bind = luasocket.bind |
| 794 end | 796 end |
| 795 | 797 |
| 796 loop = function(once) -- this is the main loop of the program | 798 loop = function(once) -- this is the main loop of the program |
| 797 if quitting then return "quitting"; end | 799 if quitting then return "quitting"; end |
| 798 if once then quitting = "once"; end | 800 if once then quitting = "once"; end |
| 801 local next_timer_time = math_huge; | |
| 799 repeat | 802 repeat |
| 800 local read, write, err = socket_select( _readlist, _sendlist, _selecttimeout ) | 803 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) ) |
| 801 for i, socket in ipairs( write ) do -- send data waiting in writequeues | 804 for i, socket in ipairs( write ) do -- send data waiting in writequeues |
| 802 local handler = _socketlist[ socket ] | 805 local handler = _socketlist[ socket ] |
| 803 if handler then | 806 if handler then |
| 804 handler.sendbuffer( ) | 807 handler.sendbuffer( ) |
| 805 else | 808 else |
| 819 for handler, err in pairs( _closelist ) do | 822 for handler, err in pairs( _closelist ) do |
| 820 handler.disconnect( )( handler, err ) | 823 handler.disconnect( )( handler, err ) |
| 821 handler:close( true ) -- forced disconnect | 824 handler:close( true ) -- forced disconnect |
| 822 end | 825 end |
| 823 clean( _closelist ) | 826 clean( _closelist ) |
| 824 _currenttime = os_time( ) | 827 _currenttime = luasocket_gettime( ) |
| 825 if os_difftime( _currenttime - _timer ) >= 1 then | 828 if _currenttime - _timer >= math_min(next_timer_time, 1) then |
| 829 next_timer_time = math_huge; | |
| 826 for i = 1, _timerlistlen do | 830 for i = 1, _timerlistlen do |
| 827 _timerlist[ i ]( _currenttime ) -- fire timers | 831 local t = _timerlist[ i ]( _currenttime ) -- fire timers |
| 832 if t then next_timer_time = math_min(next_timer_time, t); end | |
| 828 end | 833 end |
| 829 _timer = _currenttime | 834 _timer = _currenttime |
| 835 else | |
| 836 next_timer_time = next_timer_time - (_currenttime - _timer); | |
| 830 end | 837 end |
| 831 socket_sleep( _sleeptime ) -- wait some time | 838 socket_sleep( _sleeptime ) -- wait some time |
| 832 --collectgarbage( ) | 839 --collectgarbage( ) |
| 833 until quitting; | 840 until quitting; |
| 834 if once and quitting == "once" then quitting = nil; return; end | 841 if once and quitting == "once" then quitting = nil; return; end |
| 884 | 891 |
| 885 use "setmetatable" ( _socketlist, { __mode = "k" } ) | 892 use "setmetatable" ( _socketlist, { __mode = "k" } ) |
| 886 use "setmetatable" ( _readtimes, { __mode = "k" } ) | 893 use "setmetatable" ( _readtimes, { __mode = "k" } ) |
| 887 use "setmetatable" ( _writetimes, { __mode = "k" } ) | 894 use "setmetatable" ( _writetimes, { __mode = "k" } ) |
| 888 | 895 |
| 889 _timer = os_time( ) | 896 _timer = luasocket_gettime( ) |
| 890 _starttime = os_time( ) | 897 _starttime = luasocket_gettime( ) |
| 891 | 898 |
| 892 addtimer( function( ) | 899 addtimer( function( ) |
| 893 local difftime = os_difftime( _currenttime - _starttime ) | 900 local difftime = os_difftime( _currenttime - _starttime ) |
| 894 if difftime > _checkinterval then | 901 if difftime > _checkinterval then |
| 895 _starttime = _currenttime | 902 _starttime = _currenttime |