Software /
code /
prosody
Comparison
net/server_select.lua @ 4322:aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Wed, 15 Jun 2011 23:44:18 +0200 |
parent | 4145:e992650185c8 |
child | 4346:28b9384c14fd |
comparison
equal
deleted
inserted
replaced
4321:4dcdba6900f2 | 4322:aff627b1ce95 |
---|---|
699 if type( listeners ) ~= "table" then | 699 if type( listeners ) ~= "table" then |
700 err = "invalid listener table" | 700 err = "invalid listener table" |
701 end | 701 end |
702 if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then | 702 if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then |
703 err = "invalid port" | 703 err = "invalid port" |
704 elseif _server[ port ] then | 704 elseif _server[ addr..":"..port ] then |
705 err = "listeners on port '" .. port .. "' already exist" | 705 err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist" |
706 elseif sslctx and not luasec then | 706 elseif sslctx and not luasec then |
707 err = "luasec not found" | 707 err = "luasec not found" |
708 end | 708 end |
709 if err then | 709 if err then |
710 out_error( "server.lua, port ", port, ": ", err ) | 710 out_error( "server.lua, [", addr, "]:", port, ": ", err ) |
711 return nil, err | 711 return nil, err |
712 end | 712 end |
713 addr = addr or "*" | 713 addr = addr or "*" |
714 local server, err = socket_bind( addr, port ) | 714 local server, err = socket_bind( addr, port ) |
715 if err then | 715 if err then |
716 out_error( "server.lua, port ", port, ": ", err ) | 716 out_error( "server.lua, [", addr, "]:", port, ": ", err ) |
717 return nil, err | 717 return nil, err |
718 end | 718 end |
719 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket | 719 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket |
720 if not handler then | 720 if not handler then |
721 server:close( ) | 721 server:close( ) |
722 return nil, err | 722 return nil, err |
723 end | 723 end |
724 server:settimeout( 0 ) | 724 server:settimeout( 0 ) |
725 _readlistlen = addsocket(_readlist, server, _readlistlen) | 725 _readlistlen = addsocket(_readlist, server, _readlistlen) |
726 _server[ port ] = handler | 726 _server[ addr..":"..port ] = handler |
727 _socketlist[ server ] = handler | 727 _socketlist[ server ] = handler |
728 out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '", addr, ":", port, "'" ) | 728 out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '[", addr, "]:", port, "'" ) |
729 return handler | 729 return handler |
730 end | 730 end |
731 | 731 |
732 getserver = function ( port ) | 732 getserver = function ( addr, port ) |
733 return _server[ port ]; | 733 return _server[ addr..":"..port ]; |
734 end | 734 end |
735 | 735 |
736 removeserver = function( port ) | 736 removeserver = function( addr, port ) |
737 local handler = _server[ port ] | 737 local handler = _server[ addr..":"..port ] |
738 if not handler then | 738 if not handler then |
739 return nil, "no server found on port '" .. tostring( port ) .. "'" | 739 return nil, "no server found on '[" .. addr .. "]:" .. tostring( port ) .. "'" |
740 end | 740 end |
741 handler:close( ) | 741 handler:close( ) |
742 _server[ port ] = nil | 742 _server[ addr..":"..port ] = nil |
743 return true | 743 return true |
744 end | 744 end |
745 | 745 |
746 closeall = function( ) | 746 closeall = function( ) |
747 for _, handler in pairs( _socketlist ) do | 747 for _, handler in pairs( _socketlist ) do |