Diff

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
line wrap: on
line diff
--- a/net/server_select.lua	Sun Jun 12 22:21:10 2011 +0100
+++ b/net/server_select.lua	Wed Jun 15 23:44:18 2011 +0200
@@ -701,19 +701,19 @@
 	end
 	if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then
 		err = "invalid port"
-	elseif _server[ port ] then
-		err = "listeners on port '" .. port .. "' already exist"
+	elseif _server[ addr..":"..port ] then
+		err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist"
 	elseif sslctx and not luasec then
 		err = "luasec not found"
 	end
 	if err then
-		out_error( "server.lua, port ", port, ": ", err )
+		out_error( "server.lua, [", addr, "]:", port, ": ", err )
 		return nil, err
 	end
 	addr = addr or "*"
 	local server, err = socket_bind( addr, port )
 	if err then
-		out_error( "server.lua, port ", port, ": ", err )
+		out_error( "server.lua, [", addr, "]:", port, ": ", err )
 		return nil, err
 	end
 	local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket
@@ -723,23 +723,23 @@
 	end
 	server:settimeout( 0 )
 	_readlistlen = addsocket(_readlist, server, _readlistlen)
-	_server[ port ] = handler
+	_server[ addr..":"..port ] = handler
 	_socketlist[ server ] = handler
-	out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '", addr, ":", port, "'" )
+	out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '[", addr, "]:", port, "'" )
 	return handler
 end
 
-getserver = function ( port )
-	return _server[ port ];
+getserver = function ( addr, port )
+	return _server[ addr..":"..port ];
 end
 
-removeserver = function( port )
-	local handler = _server[ port ]
+removeserver = function( addr, port )
+	local handler = _server[ addr..":"..port ]
 	if not handler then
-		return nil, "no server found on port '" .. tostring( port ) .. "'"
+		return nil, "no server found on '[" .. addr .. "]:" .. tostring( port ) .. "'"
 	end
 	handler:close( )
-	_server[ port ] = nil
+	_server[ addr..":"..port ] = nil
 	return true
 end