Software /
code /
prosody
Comparison
net/server.lua @ 2057:ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 26 Oct 2009 23:29:04 +0000 |
parent | 1979:52ddaa2e6ab8 |
child | 2059:d4fb80b60c65 |
comparison
equal
deleted
inserted
replaced
2050:adc1b92b8c6b | 2057:ee2929e620bb |
---|---|
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 |
409 elseif socket and not _sendlist[ socket ] then | 409 elseif socket and not _sendlist[ socket ] then |
410 _sendlistlen = _sendlistlen + 1 | 410 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
411 _sendlist[ _sendlistlen ] = socket | |
412 _sendlist[ socket ] = _sendlistlen | |
413 end | 411 end |
414 bufferqueuelen = bufferqueuelen + 1 | 412 bufferqueuelen = bufferqueuelen + 1 |
415 bufferqueue[ bufferqueuelen ] = data | 413 bufferqueue[ bufferqueuelen ] = data |
416 if handler then | 414 if handler then |
417 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime | 415 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime |
455 end | 453 end |
456 elseif switch == false then | 454 elseif switch == false then |
457 handler.write = write | 455 handler.write = write |
458 if noread then | 456 if noread then |
459 noread = false | 457 noread = false |
460 _readlistlen = _readlistlen + 1 | 458 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
461 _readlist[ socket ] = _readlistlen | |
462 _readlist[ _readlistlen ] = socket | |
463 _readtimes[ handler ] = _currenttime | 459 _readtimes[ handler ] = _currenttime |
464 end | 460 end |
465 if nosend then | 461 if nosend then |
466 nosend = false | 462 nosend = false |
467 write( "" ) | 463 write( "" ) |
548 if not err then | 544 if not err then |
549 out_put( "server.lua: ssl handshake done" ) | 545 out_put( "server.lua: ssl handshake done" ) |
550 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions | 546 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions |
551 handler.sendbuffer = _sendbuffer | 547 handler.sendbuffer = _sendbuffer |
552 _ = status and status( handler, "ssl-handshake-complete" ) | 548 _ = status and status( handler, "ssl-handshake-complete" ) |
549 _readlistlen = addsocket(_readlist, socket, _readlistlen) | |
553 return true | 550 return true |
554 else | 551 else |
555 out_put( "server.lua: error during ssl handshake: ", tostring(err) ) | 552 out_put( "server.lua: error during ssl handshake: ", tostring(err) ) |
556 if err == "wantwrite" and not wrote then | 553 if err == "wantwrite" and not wrote then |
557 _sendlistlen = _sendlistlen + 1 | 554 _sendlistlen = addsocket(_sendlist, client, _sendlistlen) |
558 _sendlist[ _sendlistlen ] = client | |
559 wrote = true | 555 wrote = true |
560 elseif err == "wantread" and not read then | 556 elseif err == "wantread" and not read then |
561 _readlistlen = _readlistlen + 1 | 557 _readlistlen = addsocket(_readlist, client, _readlistlen) |
562 _readlist [ _readlistlen ] = client | |
563 read = true | 558 read = true |
564 else | 559 else |
565 break; | 560 break; |
566 end | 561 end |
567 --coroutine_yield( handler, nil, err ) -- handshake not finished | 562 --coroutine_yield( handler, nil, err ) -- handshake not finished |
617 send = socket.send | 612 send = socket.send |
618 receive = socket.receive | 613 receive = socket.receive |
619 shutdown = id | 614 shutdown = id |
620 | 615 |
621 _socketlist[ socket ] = handler | 616 _socketlist[ socket ] = handler |
622 _readlistlen = _readlistlen + 1 | 617 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
623 _readlist[ _readlistlen ] = socket | |
624 _readlist[ socket ] = _readlistlen | |
625 | 618 |
626 -- remove traces of the old socket | 619 -- remove traces of the old socket |
627 | 620 |
628 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen ) | 621 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen ) |
629 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen ) | 622 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen ) |
651 send = socket.send | 644 send = socket.send |
652 receive = socket.receive | 645 receive = socket.receive |
653 shutdown = ( ssl and id ) or socket.shutdown | 646 shutdown = ( ssl and id ) or socket.shutdown |
654 | 647 |
655 _socketlist[ socket ] = handler | 648 _socketlist[ socket ] = handler |
656 _readlistlen = _readlistlen + 1 | 649 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
657 _readlist[ _readlistlen ] = socket | |
658 _readlist[ socket ] = _readlistlen | |
659 | 650 |
660 return handler, socket | 651 return handler, socket |
661 end | 652 end |
662 | 653 |
663 id = function( ) | 654 id = function( ) |
664 end | 655 end |
665 | 656 |
666 idfalse = function( ) | 657 idfalse = function( ) |
667 return false | 658 return false |
659 end | |
660 | |
661 addsocket = function( list, socket, len ) | |
662 if not list[ socket ] then | |
663 len = len + 1 | |
664 list[ len ] = socket | |
665 list[ socket ] = len | |
666 end | |
667 return len; | |
668 end | 668 end |
669 | 669 |
670 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas ) | 670 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas ) |
671 local pos = list[ socket ] | 671 local pos = list[ socket ] |
672 if pos then | 672 if pos then |
719 if not handler then | 719 if not handler then |
720 server:close( ) | 720 server:close( ) |
721 return nil, err | 721 return nil, err |
722 end | 722 end |
723 server:settimeout( 0 ) | 723 server:settimeout( 0 ) |
724 _readlistlen = _readlistlen + 1 | 724 _readlistlen = addsocket(_readlist, server, _readlistlen) |
725 _readlist[ _readlistlen ] = server | |
726 _server[ port ] = handler | 725 _server[ port ] = handler |
727 _socketlist[ server ] = handler | 726 _socketlist[ server ] = handler |
728 out_put( "server.lua: new server listener on '", addr, ":", port, "'" ) | 727 out_put( "server.lua: new server listener on '", addr, ":", port, "'" ) |
729 return handler | 728 return handler |
730 end | 729 end |
842 --// EXPERIMENTAL //-- | 841 --// EXPERIMENTAL //-- |
843 | 842 |
844 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, startssl ) | 843 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, startssl ) |
845 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, startssl ) | 844 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, startssl ) |
846 _socketlist[ socket ] = handler | 845 _socketlist[ socket ] = handler |
847 _sendlistlen = _sendlistlen + 1 | 846 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
848 _sendlist[ _sendlistlen ] = socket | |
849 _sendlist[ socket ] = _sendlistlen | |
850 return handler, socket | 847 return handler, socket |
851 end | 848 end |
852 | 849 |
853 local addclient = function( address, port, listeners, pattern, sslctx, startssl ) | 850 local addclient = function( address, port, listeners, pattern, sslctx, startssl ) |
854 local client, err = luasocket.tcp( ) | 851 local client, err = luasocket.tcp( ) |