Comparison

net/server_select.lua @ 2131:72411e239221

net.server_select: Bring up to date to new common connection API
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Nov 2009 04:45:13 +0000
parent 2109:0dc5eb9dbb62
child 2233:459c1d1cae95
comparison
equal deleted inserted replaced
2130:828e161cdfc7 2131:72411e239221
164 164
165 maxconnections = maxconnections or _maxclientsperserver 165 maxconnections = maxconnections or _maxclientsperserver
166 166
167 local connections = 0 167 local connections = 0
168 168
169 local dispatch, disconnect = listeners.incoming or listeners.listener, listeners.disconnect 169 local dispatch, disconnect = listeners.onincoming, listeners.ondisconnect
170 170
171 local err 171 local err
172 172
173 local ssl = false 173 local ssl = false
174 174
239 end 239 end
240 handler.close = function( ) 240 handler.close = function( )
241 for _, handler in pairs( _socketlist ) do 241 for _, handler in pairs( _socketlist ) do
242 if handler.serverport == serverport then 242 if handler.serverport == serverport then
243 handler.disconnect( handler, "server closed" ) 243 handler.disconnect( handler, "server closed" )
244 handler.close( true ) 244 handler:close( true )
245 end 245 end
246 end 246 end
247 socket:close( ) 247 socket:close( )
248 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) 248 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
249 _readlistlen = removesocket( _readlist, socket, _readlistlen ) 249 _readlistlen = removesocket( _readlist, socket, _readlistlen )
298 298
299 --// private closures of the object //-- 299 --// private closures of the object //--
300 300
301 local ssl 301 local ssl
302 302
303 local dispatch = listeners.incoming or listeners.listener 303 local dispatch = listeners.onincoming
304 local status = listeners.status 304 local status = listeners.status
305 local disconnect = listeners.disconnect 305 local disconnect = listeners.ondisconnect
306 306
307 local bufferqueue = { } -- buffer array 307 local bufferqueue = { } -- buffer array
308 local bufferqueuelen = 0 -- end of buffer array 308 local bufferqueuelen = 0 -- end of buffer array
309 309
310 local toclose 310 local toclose
329 return dispatch 329 return dispatch
330 end 330 end
331 handler.disconnect = function( ) 331 handler.disconnect = function( )
332 return disconnect 332 return disconnect
333 end 333 end
334 handler.setlistener = function( listeners ) 334 handler.setlistener = function( self, listeners )
335 dispatch = listeners.incoming 335 dispatch = listeners.onincoming
336 disconnect = listeners.disconnect 336 disconnect = listeners.ondisconnect
337 end 337 end
338 handler.getstats = function( ) 338 handler.getstats = function( )
339 return readtraffic, sendtraffic 339 return readtraffic, sendtraffic
340 end 340 end
341 handler.ssl = function( ) 341 handler.ssl = function( )
398 return serverport 398 return serverport
399 end 399 end
400 handler.clientport = function( ) 400 handler.clientport = function( )
401 return clientport 401 return clientport
402 end 402 end
403 local write = function( data ) 403 local write = function( self, data )
404 bufferlen = bufferlen + string_len( data ) 404 bufferlen = bufferlen + string_len( data )
405 if bufferlen > maxsendlen then 405 if bufferlen > maxsendlen then
406 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle 406 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle
407 handler.write = idfalse -- dont write anymore 407 handler.write = idfalse -- dont write anymore
408 return false 408 return false
415 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime 415 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime
416 end 416 end
417 return true 417 return true
418 end 418 end
419 handler.write = write 419 handler.write = write
420 handler.bufferqueue = function( ) 420 handler.bufferqueue = function( self )
421 return bufferqueue 421 return bufferqueue
422 end 422 end
423 handler.socket = function( ) 423 handler.socket = function( self )
424 return socket 424 return socket
425 end 425 end
426 handler.pattern = function( new ) 426 handler.pattern = function( self, new )
427 pattern = new or pattern 427 pattern = new or pattern
428 return pattern 428 return pattern
429 end 429 end
430 handler.setsend = function ( newsend ) 430 handler.setsend = function ( self, newsend )
431 send = newsend or send 431 send = newsend or send
432 return send 432 return send
433 end 433 end
434 handler.bufferlen = function( readlen, sendlen ) 434 handler.bufferlen = function( self, readlen, sendlen )
435 maxsendlen = sendlen or maxsendlen 435 maxsendlen = sendlen or maxsendlen
436 maxreadlen = readlen or maxreadlen 436 maxreadlen = readlen or maxreadlen
437 return maxreadlen, maxsendlen 437 return maxreadlen, maxsendlen
438 end 438 end
439 handler.lock = function( switch ) 439 handler.lock = function( self, switch )
440 if switch == true then 440 if switch == true then
441 handler.write = idfalse 441 handler.write = idfalse
442 local tmp = _sendlistlen 442 local tmp = _sendlistlen
443 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) 443 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
444 _writetimes[ handler ] = nil 444 _writetimes[ handler ] = nil
505 end 505 end
506 if succ then -- sending succesful 506 if succ then -- sending succesful
507 bufferqueuelen = 0 507 bufferqueuelen = 0
508 bufferlen = 0 508 bufferlen = 0
509 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist 509 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
510 _ = needtls and handler.starttls(true) 510 _ = needtls and handler:starttls(true)
511 _writetimes[ handler ] = nil 511 _writetimes[ handler ] = nil
512 _ = toclose and handler.close( ) 512 _ = toclose and handler.close( )
513 return true 513 return true
514 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write 514 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
515 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer 515 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
527 end 527 end
528 end 528 end
529 529
530 -- Set the sslctx 530 -- Set the sslctx
531 local handshake; 531 local handshake;
532 function handler.set_sslctx(new_sslctx) 532 function handler.set_sslctx(self, new_sslctx)
533 ssl = true 533 ssl = true
534 sslctx = new_sslctx; 534 sslctx = new_sslctx;
535 local wrote 535 local wrote
536 local read 536 local read
537 handshake = coroutine_wrap( function( client ) -- create handshake coroutine 537 handshake = coroutine_wrap( function( client ) -- create handshake coroutine
562 --coroutine_yield( handler, nil, err ) -- handshake not finished 562 --coroutine_yield( handler, nil, err ) -- handshake not finished
563 coroutine_yield( ) 563 coroutine_yield( )
564 end 564 end
565 end 565 end
566 disconnect( handler, "ssl handshake failed" ) 566 disconnect( handler, "ssl handshake failed" )
567 _ = handler and handler.close( true ) -- forced disconnect 567 _ = handler and handler:close( true ) -- forced disconnect
568 return false -- handshake failed 568 return false -- handshake failed
569 end 569 end
570 ) 570 )
571 end 571 end
572 if sslctx then -- ssl? 572 if sslctx then -- ssl?
573 handler.set_sslctx(sslctx); 573 handler:set_sslctx(sslctx);
574 if startssl then -- ssl now? 574 if startssl then -- ssl now?
575 --out_put("server.lua: ", "starting ssl handshake") 575 --out_put("server.lua: ", "starting ssl handshake")
576 local err 576 local err
577 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket 577 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
578 if err then 578 if err then
588 return nil, nil, "ssl handshake failed"; 588 return nil, nil, "ssl handshake failed";
589 end 589 end
590 else 590 else
591 -- We're not automatically doing SSL, so we're not secure (yet) 591 -- We're not automatically doing SSL, so we're not secure (yet)
592 ssl = false 592 ssl = false
593 handler.starttls = function( now ) 593 handler.starttls = function( self, now )
594 if not now then 594 if not now then
595 --out_put "server.lua: we need to do tls, but delaying until later" 595 --out_put "server.lua: we need to do tls, but delaying until later"
596 needtls = true 596 needtls = true
597 return 597 return
598 end 598 end
735 removeserver = function( port ) 735 removeserver = function( port )
736 local handler = _server[ port ] 736 local handler = _server[ port ]
737 if not handler then 737 if not handler then
738 return nil, "no server found on port '" .. tostring( port ) .. "'" 738 return nil, "no server found on port '" .. tostring( port ) .. "'"
739 end 739 end
740 handler.close( ) 740 handler:close( )
741 _server[ port ] = nil 741 _server[ port ] = nil
742 return true 742 return true
743 end 743 end
744 744
745 closeall = function( ) 745 closeall = function( )
746 for _, handler in pairs( _socketlist ) do 746 for _, handler in pairs( _socketlist ) do
747 handler.close( ) 747 handler:close( )
748 _socketlist[ _ ] = nil 748 _socketlist[ _ ] = nil
749 end 749 end
750 _readlistlen = 0 750 _readlistlen = 0
751 _sendlistlen = 0 751 _sendlistlen = 0
752 _timerlistlen = 0 752 _timerlistlen = 0
820 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen 820 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
821 end 821 end
822 end 822 end
823 for handler, err in pairs( _closelist ) do 823 for handler, err in pairs( _closelist ) do
824 handler.disconnect( )( handler, err ) 824 handler.disconnect( )( handler, err )
825 handler.close( true ) -- forced disconnect 825 handler:close( true ) -- forced disconnect
826 end 826 end
827 clean( _closelist ) 827 clean( _closelist )
828 _currenttime = os_time( ) 828 _currenttime = os_time( )
829 if os_difftime( _currenttime - _timer ) >= 1 then 829 if os_difftime( _currenttime - _timer ) >= 1 then
830 for i = 1, _timerlistlen do 830 for i = 1, _timerlistlen do
878 _starttime = _currenttime 878 _starttime = _currenttime
879 for handler, timestamp in pairs( _writetimes ) do 879 for handler, timestamp in pairs( _writetimes ) do
880 if os_difftime( _currenttime - timestamp ) > _sendtimeout then 880 if os_difftime( _currenttime - timestamp ) > _sendtimeout then
881 --_writetimes[ handler ] = nil 881 --_writetimes[ handler ] = nil
882 handler.disconnect( )( handler, "send timeout" ) 882 handler.disconnect( )( handler, "send timeout" )
883 handler.close( true ) -- forced disconnect 883 handler:close( true ) -- forced disconnect
884 end 884 end
885 end 885 end
886 for handler, timestamp in pairs( _readtimes ) do 886 for handler, timestamp in pairs( _readtimes ) do
887 if os_difftime( _currenttime - timestamp ) > _readtimeout then 887 if os_difftime( _currenttime - timestamp ) > _readtimeout then
888 --_readtimes[ handler ] = nil 888 --_readtimes[ handler ] = nil
889 handler.disconnect( )( handler, "read timeout" ) 889 handler.disconnect( )( handler, "read timeout" )
890 handler.close( ) -- forced disconnect? 890 handler:close( ) -- forced disconnect?
891 end 891 end
892 end 892 end
893 end 893 end
894 end 894 end
895 ) 895 )