Comparison

net/server_select.lua @ 3007:9d122a6ae674

Merge 0.7->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 05 May 2010 15:33:58 +0100
parent 2998:36c169ed1576
parent 3003:48871a549b4c
child 3010:52146b82f295
comparison
equal deleted inserted replaced
2999:9a8f942433c4 3007:9d122a6ae674
250 local ssl 250 local ssl
251 251
252 local dispatch = listeners.onincoming 252 local dispatch = listeners.onincoming
253 local status = listeners.onstatus 253 local status = listeners.onstatus
254 local disconnect = listeners.ondisconnect 254 local disconnect = listeners.ondisconnect
255 local drain = listeners.ondrain
255 256
256 local bufferqueue = { } -- buffer array 257 local bufferqueue = { } -- buffer array
257 local bufferqueuelen = 0 -- end of buffer array 258 local bufferqueuelen = 0 -- end of buffer array
258 259
259 local toclose 260 local toclose
282 end 283 end
283 handler.setlistener = function( self, listeners ) 284 handler.setlistener = function( self, listeners )
284 dispatch = listeners.onincoming 285 dispatch = listeners.onincoming
285 disconnect = listeners.ondisconnect 286 disconnect = listeners.ondisconnect
286 status = listeners.onstatus 287 status = listeners.onstatus
288 drain = listeners.ondrain
287 end 289 end
288 handler.getstats = function( ) 290 handler.getstats = function( )
289 return readtraffic, sendtraffic 291 return readtraffic, sendtraffic
290 end 292 end
291 handler.ssl = function( ) 293 handler.ssl = function( )
377 return bufferqueue 379 return bufferqueue
378 end 380 end
379 handler.socket = function( self ) 381 handler.socket = function( self )
380 return socket 382 return socket
381 end 383 end
382 handler.pattern = function( self, new ) 384 handler.set_mode = function( self, new )
383 pattern = new or pattern 385 pattern = new or pattern
384 return pattern 386 return pattern
385 end 387 end
386 handler.set_send = function ( self, newsend ) 388 handler.set_send = function ( self, newsend )
387 send = newsend or send 389 send = newsend or send
390 handler.bufferlen = function( self, readlen, sendlen ) 392 handler.bufferlen = function( self, readlen, sendlen )
391 maxsendlen = sendlen or maxsendlen 393 maxsendlen = sendlen or maxsendlen
392 maxreadlen = readlen or maxreadlen 394 maxreadlen = readlen or maxreadlen
393 return bufferlen, maxreadlen, maxsendlen 395 return bufferlen, maxreadlen, maxsendlen
394 end 396 end
397 --TODO: Deprecate
395 handler.lock_read = function (self, switch) 398 handler.lock_read = function (self, switch)
396 if switch == true then 399 if switch == true then
397 local tmp = _readlistlen 400 local tmp = _readlistlen
398 _readlistlen = removesocket( _readlist, socket, _readlistlen ) 401 _readlistlen = removesocket( _readlist, socket, _readlistlen )
399 _readtimes[ handler ] = nil 402 _readtimes[ handler ] = nil
406 _readlistlen = addsocket(_readlist, socket, _readlistlen) 409 _readlistlen = addsocket(_readlist, socket, _readlistlen)
407 _readtimes[ handler ] = _currenttime 410 _readtimes[ handler ] = _currenttime
408 end 411 end
409 end 412 end
410 return noread 413 return noread
414 end
415 handler.pause = function (self)
416 return self:lock_read(true);
417 end
418 handler.resume = function (self)
419 return self:lock_read(false);
411 end 420 end
412 handler.lock = function( self, switch ) 421 handler.lock = function( self, switch )
413 handler.lock_read (switch) 422 handler.lock_read (switch)
414 if switch == true then 423 if switch == true then
415 handler.write = idfalse 424 handler.write = idfalse
428 end 437 end
429 return noread, nosend 438 return noread, nosend
430 end 439 end
431 local _readbuffer = function( ) -- this function reads data 440 local _readbuffer = function( ) -- this function reads data
432 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" 441 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
433 if not err or (err == "wantread" or err == "timeout") or (part and string_len(part) > 0) then -- received something 442 if not err or (err == "wantread" or err == "timeout") then -- received something
434 local buffer = buffer or part or "" 443 local buffer = buffer or part or ""
435 local len = string_len( buffer ) 444 local len = string_len( buffer )
436 if len > maxreadlen then 445 if len > maxreadlen then
437 disconnect( handler, "receive buffer exceeded" ) 446 disconnect( handler, "receive buffer exceeded" )
438 handler:close( true ) 447 handler:close( true )
470 bufferqueuelen = 0 479 bufferqueuelen = 0
471 bufferlen = 0 480 bufferlen = 0
472 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist 481 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
473 _ = needtls and handler:starttls(nil, true) 482 _ = needtls and handler:starttls(nil, true)
474 _writetimes[ handler ] = nil 483 _writetimes[ handler ] = nil
484 if drain then
485 drain(handler)
486 end
475 _ = toclose and handler:close( ) 487 _ = toclose and handler:close( )
476 return true 488 return true
477 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write 489 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
478 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer 490 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
479 bufferqueue[ 1 ] = buffer -- insert new buffer in queue 491 bufferqueue[ 1 ] = buffer -- insert new buffer in queue
664 _socketlist[ socket ] = nil 676 _socketlist[ socket ] = nil
665 socket:close( ) 677 socket:close( )
666 --mem_free( ) 678 --mem_free( )
667 end 679 end
668 680
681 local function link(sender, receiver, buffersize)
682 sender:set_mode(buffersize);
683 local sender_locked;
684 local _sendbuffer = receiver.sendbuffer;
685 function receiver.sendbuffer()
686 _sendbuffer();
687 if sender_locked and receiver.bufferlen() < buffersize then
688 sender:lock_read(false); -- Unlock now
689 sender_locked = nil;
690 end
691 end
692
693 local _readbuffer = sender.readbuffer;
694 function sender.readbuffer()
695 _readbuffer();
696 if not sender_locked and receiver.bufferlen() >= buffersize then
697 sender_locked = true;
698 sender:lock_read(true);
699 end
700 end
701 end
702
669 ----------------------------------// PUBLIC //-- 703 ----------------------------------// PUBLIC //--
670 704
671 addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server 705 addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server
672 local err 706 local err
673 if type( listeners ) ~= "table" then 707 if type( listeners ) ~= "table" then
887 921
888 addclient = addclient, 922 addclient = addclient,
889 wrapclient = wrapclient, 923 wrapclient = wrapclient,
890 924
891 loop = loop, 925 loop = loop,
926 link = link,
892 stats = stats, 927 stats = stats,
893 closeall = closeall, 928 closeall = closeall,
894 addtimer = addtimer, 929 addtimer = addtimer,
895 addserver = addserver, 930 addserver = addserver,
896 getserver = getserver, 931 getserver = getserver,