support listening on a specific interface

This commit is contained in:
David Rose 2009-12-01 01:34:02 +00:00
parent 876bd72fb9
commit f4ed61f659
3 changed files with 73 additions and 4 deletions

View File

@ -78,7 +78,8 @@ class ServerRepository:
# record its current fields. That is left to the clients.
def __init__(self, tcpPort, udpPort = None, dcFileNames = None,
def __init__(self, tcpPort, serverAddress = None,
udpPort = None, dcFileNames = None,
threadedNet = None):
if threadedNet is None:
# Default value.
@ -92,7 +93,9 @@ class ServerRepository:
self.qcl = QueuedConnectionListener(self.qcm, numThreads)
self.qcr = QueuedConnectionReader(self.qcm, numThreads)
self.cw = ConnectionWriter(self.qcm, numThreads)
self.tcpRendezvous = self.qcm.openTCPServerRendezvous(tcpPort, 10)
self.tcpRendezvous = self.qcm.openTCPServerRendezvous(
serverAddress or '', tcpPort, 10)
self.qcl.addConnection(self.tcpRendezvous)
taskMgr.add(self.listenerPoll, "serverListenerPollTask")
taskMgr.add(self.readerPollUntilEmpty, "serverReaderPollTask")

View File

@ -102,6 +102,7 @@ open_UDP_connection(int port) {
}
////////////////////////////////////////////////////////////////////
// Function: ConnectionManager::open_TCP_server_rendezvous
// Access: Public
@ -111,6 +112,10 @@ open_UDP_connection(int port) {
// a ConnectionListener (not to a generic
// ConnectionReader).
//
// This variant of this method accepts a single port,
// and will listen to that port on all available
// interfaces.
//
// backlog is the maximum length of the queue of pending
// connections.
////////////////////////////////////////////////////////////////////
@ -118,18 +123,75 @@ PT(Connection) ConnectionManager::
open_TCP_server_rendezvous(int port, int backlog) {
NetAddress address;
address.set_any(port);
return open_TCP_server_rendezvous(address, backlog);
}
////////////////////////////////////////////////////////////////////
// Function: ConnectionManager::open_TCP_server_rendezvous
// Access: Public
// Description: Creates a socket to be used as a rendezvous socket
// for a server to listen for TCP connections. The
// socket returned by this call should only be added to
// a ConnectionListener (not to a generic
// ConnectionReader).
//
// This variant of this method accepts a "hostname",
// which is usually just an IP address in dotted
// notation, and a port number. It will listen on the
// interface indicated by the IP address. If the IP
// address is empty string, it will listen on all
// interfaces.
//
// backlog is the maximum length of the queue of pending
// connections.
////////////////////////////////////////////////////////////////////
PT(Connection) ConnectionManager::
open_TCP_server_rendezvous(const string &hostname, int port, int backlog) {
NetAddress address;
if (hostname.empty()) {
address.set_any(port);
} else {
address.set_host(hostname, port);
}
return open_TCP_server_rendezvous(address, backlog);
}
////////////////////////////////////////////////////////////////////
// Function: ConnectionManager::open_TCP_server_rendezvous
// Access: Public
// Description: Creates a socket to be used as a rendezvous socket
// for a server to listen for TCP connections. The
// socket returned by this call should only be added to
// a ConnectionListener (not to a generic
// ConnectionReader).
//
// This variant of this method accepts a NetAddress,
// which allows you to specify a specific interface to
// listen to.
//
// backlog is the maximum length of the queue of pending
// connections.
////////////////////////////////////////////////////////////////////
PT(Connection) ConnectionManager::
open_TCP_server_rendezvous(const NetAddress &address, int backlog) {
ostringstream strm;
if (address.get_ip() == 0) {
strm << "port " << address.get_port();
} else {
strm << address.get_ip_string() << ":" << address.get_port();
}
Socket_TCP_Listen *socket = new Socket_TCP_Listen;
bool okflag = socket->OpenForListen(address.get_addr(), backlog);
if (!okflag) {
net_cat.info()
<< "Unable to listen to port " << port << " for TCP.\n";
<< "Unable to listen to " << strm.str() << " for TCP.\n";
delete socket;
return PT(Connection)();
}
net_cat.info()
<< "Listening for TCP connections on port " << port << "\n";
<< "Listening for TCP connections on " << strm.str() << "\n";
PT(Connection) connection = new Connection(this, socket);
new_connection(connection);

View File

@ -51,6 +51,10 @@ PUBLISHED:
PT(Connection) open_UDP_connection(int port = 0);
PT(Connection) open_TCP_server_rendezvous(int port, int backlog);
PT(Connection) open_TCP_server_rendezvous(const string &hostname,
int port, int backlog);
PT(Connection) open_TCP_server_rendezvous(const NetAddress &address,
int backlog);
PT(Connection) open_TCP_client_connection(const NetAddress &address,
int timeout_ms);
PT(Connection) open_TCP_client_connection(const string &hostname, int port,