Comparison

net/server_select.lua @ 6054:7a5ddbaf758d

Merge 0.9->0.10
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Apr 2014 17:41:38 +0100
parent 6047:4db0403bfc0d
parent 5950:bd1d1c29a7e7
child 6055:596539a30e9b
child 6315:7a3e2f2d43fc
comparison
equal deleted inserted replaced
6053:2f93a04564b2 6054:7a5ddbaf758d
1 -- 1 --
2 -- server.lua by blastbeat of the luadch project 2 -- server.lua by blastbeat of the luadch project
3 -- Re-used here under the MIT/X Consortium License 3 -- Re-used here under the MIT/X Consortium License
4 -- 4 --
5 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain 5 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain
6 -- 6 --
7 7
8 -- // wrapping luadch stuff // -- 8 -- // wrapping luadch stuff // --
9 9
143 _tcpbacklog = 128 -- some kind of hint to the OS 143 _tcpbacklog = 128 -- some kind of hint to the OS
144 144
145 _maxsendlen = 51000 * 1024 -- max len of send buffer 145 _maxsendlen = 51000 * 1024 -- max len of send buffer
146 _maxreadlen = 25000 * 1024 -- max len of read buffer 146 _maxreadlen = 25000 * 1024 -- max len of read buffer
147 147
148 _checkinterval = 1200000 -- interval in secs to check idle clients 148 _checkinterval = 30 -- interval in secs to check idle clients
149 _sendtimeout = 60000 -- allowed send idle time in secs 149 _sendtimeout = 60000 -- allowed send idle time in secs
150 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs 150 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs
151 151
152 local is_windows = package.config:sub(1,1) == "\\" -- check the directory separator, to detemine whether this is Windows 152 local is_windows = package.config:sub(1,1) == "\\" -- check the directory separator, to detemine whether this is Windows
153 _maxfd = (is_windows and math.huge) or luasocket._SETSIZE or 1024 -- max fd number, limit to 1024 by default to prevent glibc buffer overflow, but not on Windows 153 _maxfd = (is_windows and math.huge) or luasocket._SETSIZE or 1024 -- max fd number, limit to 1024 by default to prevent glibc buffer overflow, but not on Windows
282 282
283 local dispatch = listeners.onincoming 283 local dispatch = listeners.onincoming
284 local status = listeners.onstatus 284 local status = listeners.onstatus
285 local disconnect = listeners.ondisconnect 285 local disconnect = listeners.ondisconnect
286 local drain = listeners.ondrain 286 local drain = listeners.ondrain
287 local onreadtimeout = listeners.onreadtimeout;
287 288
288 local bufferqueue = { } -- buffer array 289 local bufferqueue = { } -- buffer array
289 local bufferqueuelen = 0 -- end of buffer array 290 local bufferqueuelen = 0 -- end of buffer array
290 291
291 local toclose 292 local toclose
310 return dispatch 311 return dispatch
311 end 312 end
312 handler.disconnect = function( ) 313 handler.disconnect = function( )
313 return disconnect 314 return disconnect
314 end 315 end
316 handler.onreadtimeout = onreadtimeout;
317
315 handler.setlistener = function( self, listeners ) 318 handler.setlistener = function( self, listeners )
316 dispatch = listeners.onincoming 319 dispatch = listeners.onincoming
317 disconnect = listeners.ondisconnect 320 disconnect = listeners.ondisconnect
318 status = listeners.onstatus 321 status = listeners.onstatus
319 drain = listeners.ondrain 322 drain = listeners.ondrain
323 handler.onreadtimeout = listeners.onreadtimeout
320 end 324 end
321 handler.getstats = function( ) 325 handler.getstats = function( )
322 return readtraffic, sendtraffic 326 return readtraffic, sendtraffic
323 end 327 end
324 handler.ssl = function( ) 328 handler.ssl = function( )
606 send = socket.send 610 send = socket.send
607 receive = socket.receive 611 receive = socket.receive
608 shutdown = id 612 shutdown = id
609 _socketlist[ socket ] = handler 613 _socketlist[ socket ] = handler
610 _readlistlen = addsocket(_readlist, socket, _readlistlen) 614 _readlistlen = addsocket(_readlist, socket, _readlistlen)
611 615
612 -- remove traces of the old socket 616 -- remove traces of the old socket
613 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen ) 617 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen )
614 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen ) 618 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen )
615 _socketlist[ oldsocket ] = nil 619 _socketlist[ oldsocket ] = nil
616 620
694 if sender_locked and receiver.bufferlen() < buffersize then 698 if sender_locked and receiver.bufferlen() < buffersize then
695 sender:lock_read(false); -- Unlock now 699 sender:lock_read(false); -- Unlock now
696 sender_locked = nil; 700 sender_locked = nil;
697 end 701 end
698 end 702 end
699 703
700 local _readbuffer = sender.readbuffer; 704 local _readbuffer = sender.readbuffer;
701 function sender.readbuffer() 705 function sender.readbuffer()
702 _readbuffer(); 706 _readbuffer();
703 if not sender_locked and receiver.bufferlen() >= buffersize then 707 if not sender_locked and receiver.bufferlen() >= buffersize then
704 sender_locked = true; 708 sender_locked = true;
862 local difftime = os_difftime( _currenttime - _starttime ) 866 local difftime = os_difftime( _currenttime - _starttime )
863 if difftime > _checkinterval then 867 if difftime > _checkinterval then
864 _starttime = _currenttime 868 _starttime = _currenttime
865 for handler, timestamp in pairs( _writetimes ) do 869 for handler, timestamp in pairs( _writetimes ) do
866 if os_difftime( _currenttime - timestamp ) > _sendtimeout then 870 if os_difftime( _currenttime - timestamp ) > _sendtimeout then
867 --_writetimes[ handler ] = nil
868 handler.disconnect( )( handler, "send timeout" ) 871 handler.disconnect( )( handler, "send timeout" )
869 handler:force_close() -- forced disconnect 872 handler:force_close() -- forced disconnect
870 end 873 end
871 end 874 end
872 for handler, timestamp in pairs( _readtimes ) do 875 for handler, timestamp in pairs( _readtimes ) do
873 if os_difftime( _currenttime - timestamp ) > _readtimeout then 876 if os_difftime( _currenttime - timestamp ) > _readtimeout then
874 --_readtimes[ handler ] = nil 877 if not(handler.onreadtimeout) or handler:onreadtimeout() ~= true then
875 handler.disconnect( )( handler, "read timeout" ) 878 handler.disconnect( )( handler, "read timeout" )
876 handler:close( ) -- forced disconnect? 879 handler:close( ) -- forced disconnect?
880 end
877 end 881 end
878 end 882 end
879 end 883 end
880 884
881 -- Fire timers 885 -- Fire timers
932 return nil, err 936 return nil, err
933 end 937 end
934 client:settimeout( 0 ) 938 client:settimeout( 0 )
935 _, err = client:connect( address, port ) 939 _, err = client:connect( address, port )
936 if err then -- try again 940 if err then -- try again
937 local handler = wrapclient( client, address, port, listeners ) 941 return wrapclient( client, address, port, listeners, pattern, sslctx )
938 else 942 else
939 wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx ) 943 return wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx )
940 end 944 end
941 end 945 end
942 946
943 --// EXPERIMENTAL //-- 947 --// EXPERIMENTAL //--
944 948
964 return { 968 return {
965 _addtimer = addtimer, 969 _addtimer = addtimer,
966 970
967 addclient = addclient, 971 addclient = addclient,
968 wrapclient = wrapclient, 972 wrapclient = wrapclient,
969 973
970 loop = loop, 974 loop = loop,
971 link = link, 975 link = link,
972 step = step, 976 step = step,
973 stats = stats, 977 stats = stats,
974 closeall = closeall, 978 closeall = closeall,