Comparison

net/server_select.lua @ 4962:1d8fb12a9b2d

net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jul 2012 16:54:33 +0100
parent 4904:5d2ff4627b72
child 4980:2e35cfe11835
comparison
equal deleted inserted replaced
4961:29b24a4028ed 4962:1d8fb12a9b2d
312 if socket.setoption then 312 if socket.setoption then
313 return socket:setoption(option, value); 313 return socket:setoption(option, value);
314 end 314 end
315 return false, "setoption not implemented"; 315 return false, "setoption not implemented";
316 end 316 end
317 handler.close = function( self, forced ) 317 handler.force_close = function ( self )
318 if bufferqueuelen ~= 0 then
319 out_put("discarding unwritten data for ", tostring(ip), ":", tostring(clientport))
320 for i = bufferqueuelen, 1, -1 do
321 bufferqueue[i] = nil;
322 end
323 bufferqueuelen = 0;
324 end
325 return self:close();
326 end
327 handler.close = function( self )
318 if not handler then return true; end 328 if not handler then return true; end
319 _readlistlen = removesocket( _readlist, socket, _readlistlen ) 329 _readlistlen = removesocket( _readlist, socket, _readlistlen )
320 _readtimes[ handler ] = nil 330 _readtimes[ handler ] = nil
321 if bufferqueuelen ~= 0 then 331 if bufferqueuelen ~= 0 then
322 if not ( forced or fatalerror ) then 332 handler.sendbuffer() -- Try now to send any outstanding data
323 handler.sendbuffer( ) 333 if bufferqueuelen ~= 0 then -- Still not empty, so we'll try again later
324 if bufferqueuelen ~= 0 then -- try again... 334 if handler then
325 if handler then 335 handler.write = nil -- ... but no further writing allowed
326 handler.write = nil -- ... but no further writing allowed
327 end
328 toclose = true
329 return false
330 end 336 end
331 else 337 toclose = true
332 send( socket, table_concat( bufferqueue, "", 1, bufferqueuelen ), 1, bufferlen ) -- forced send 338 return false
333 end 339 end
334 end 340 end
335 if socket then 341 if socket then
336 _ = shutdown and shutdown( socket ) 342 _ = shutdown and shutdown( socket )
337 socket:close( ) 343 socket:close( )
345 _writetimes[ handler ] = nil 351 _writetimes[ handler ] = nil
346 _closelist[ handler ] = nil 352 _closelist[ handler ] = nil
347 local _handler = handler; 353 local _handler = handler;
348 handler = nil 354 handler = nil
349 if disconnect then 355 if disconnect then
350 disconnect(_handler, "closed"); 356 disconnect(_handler, false);
351 end 357 end
352 end 358 end
353 if server then 359 if server then
354 server.remove( ) 360 server.remove( )
355 end 361 end
478 sendtraffic = sendtraffic + count 484 sendtraffic = sendtraffic + count
479 _sendtraffic = _sendtraffic + count 485 _sendtraffic = _sendtraffic + count
480 _ = _cleanqueue and clean( bufferqueue ) 486 _ = _cleanqueue and clean( bufferqueue )
481 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) 487 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) )
482 else 488 else
483 succ, err, count = false, "closed", 0; 489 succ, err, count = false, "unexpected close", 0;
484 end 490 end
485 if succ then -- sending succesful 491 if succ then -- sending succesful
486 bufferqueuelen = 0 492 bufferqueuelen = 0
487 bufferlen = 0 493 bufferlen = 0
488 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist 494 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
489 _writetimes[ handler ] = nil 495 _writetimes[ handler ] = nil
490 if drain then 496 if drain then
491 drain(handler) 497 drain(handler)
492 end 498 end
493 _ = needtls and handler:starttls(nil) 499 _ = needtls and handler:starttls(nil)
494 _ = toclose and handler:close( ) 500 _ = toclose and handler:force_close( )
495 return true 501 return true
496 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write 502 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
497 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer 503 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
498 bufferqueue[ 1 ] = buffer -- insert new buffer in queue 504 bufferqueue[ 1 ] = buffer -- insert new buffer in queue
499 bufferqueuelen = 1 505 bufferqueuelen = 1
502 return true 508 return true
503 else -- connection was closed during sending or fatal error 509 else -- connection was closed during sending or fatal error
504 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) ) 510 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) )
505 fatalerror = true 511 fatalerror = true
506 disconnect( handler, err ) 512 disconnect( handler, err )
507 _ = handler and handler:close( ) 513 _ = handler and handler:force_close( )
508 return false 514 return false
509 end 515 end
510 end 516 end
511 517
512 -- Set the sslctx 518 -- Set the sslctx
545 coroutine_yield( ) -- handshake not finished 551 coroutine_yield( ) -- handshake not finished
546 end 552 end
547 end 553 end
548 out_put( "server.lua: ssl handshake error: ", tostring(err or "handshake too long") ) 554 out_put( "server.lua: ssl handshake error: ", tostring(err or "handshake too long") )
549 disconnect( handler, "ssl handshake failed" ) 555 disconnect( handler, "ssl handshake failed" )
550 _ = handler and handler:close( true ) -- forced disconnect 556 _ = handler and handler:force_close()
551 return false, err -- handshake failed 557 return false, err -- handshake failed
552 end 558 end
553 ) 559 )
554 end 560 end
555 if luasec then 561 if luasec then
808 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen 814 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
809 end 815 end
810 end 816 end
811 for handler, err in pairs( _closelist ) do 817 for handler, err in pairs( _closelist ) do
812 handler.disconnect( )( handler, err ) 818 handler.disconnect( )( handler, err )
813 handler:close( true ) -- forced disconnect 819 handler:force_close() -- forced disconnect
814 end 820 end
815 clean( _closelist ) 821 clean( _closelist )
816 _currenttime = luasocket_gettime( ) 822 _currenttime = luasocket_gettime( )
817 if _currenttime - _timer >= math_min(next_timer_time, 1) then 823 if _currenttime - _timer >= math_min(next_timer_time, 1) then
818 next_timer_time = math_huge; 824 next_timer_time = math_huge;
894 _starttime = _currenttime 900 _starttime = _currenttime
895 for handler, timestamp in pairs( _writetimes ) do 901 for handler, timestamp in pairs( _writetimes ) do
896 if os_difftime( _currenttime - timestamp ) > _sendtimeout then 902 if os_difftime( _currenttime - timestamp ) > _sendtimeout then
897 --_writetimes[ handler ] = nil 903 --_writetimes[ handler ] = nil
898 handler.disconnect( )( handler, "send timeout" ) 904 handler.disconnect( )( handler, "send timeout" )
899 handler:close( true ) -- forced disconnect 905 handler:force_close() -- forced disconnect
900 end 906 end
901 end 907 end
902 for handler, timestamp in pairs( _readtimes ) do 908 for handler, timestamp in pairs( _readtimes ) do
903 if os_difftime( _currenttime - timestamp ) > _readtimeout then 909 if os_difftime( _currenttime - timestamp ) > _readtimeout then
904 --_readtimes[ handler ] = nil 910 --_readtimes[ handler ] = nil