From 68db829db66dbf8faad5bd8df35760b5af3a1491 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 3 May 2006 08:42:32 +0000 Subject: Sweet, all changes from the (once again 1 rev) branch have been merged back in. Now to finalize the changes in my other programs. --- src/connectionmanager.cpp | 58 ++++++++++++++++++------------ src/connectionmanager.h | 14 +++++--- src/connectionmonitor.h | 4 ++- src/test/httpsrv/httpconnectionmonitor.cpp | 3 +- src/test/httpsrv/httpconnectionmonitor.h | 2 +- src/test/httpsrv/main.cpp | 3 +- src/test/teltest/main.cpp | 2 +- src/test/teltest/telnetmonitor.cpp | 2 +- src/test/teltest/telnetmonitor.h | 2 +- 9 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp index ded5aa4..88d7878 100644 --- a/src/connectionmanager.cpp +++ b/src/connectionmanager.cpp @@ -13,11 +13,19 @@ #include "connectionmanager.h" #include -ConnectionManager::ConnectionManager() : +ConnectionManager::ConnectionManager( int nInitPool ) : xLog( MultiLog::getInstance() ) { - nMasterSocket = -1; + //nMasterSocket = -1; pMonitor = NULL; + for( int j = 0; j < nInitPool; j++ ) + { + lInactive.insert( lInactive.begin(), new Connection() ); + } + FD_ZERO (&fdActive); + FD_ZERO (&fdRead); + FD_ZERO (&fdWrite); + FD_ZERO (&fdException); } ConnectionManager::~ConnectionManager() @@ -33,13 +41,13 @@ ConnectionManager::~ConnectionManager() } } -bool ConnectionManager::startServer( int nPort, int nInitPool ) +bool ConnectionManager::startServer( int nPort ) { /* Create the socket and set it up to accept connections. */ struct sockaddr_in name; /* Create the socket. */ - nMasterSocket = socket (PF_INET, SOCK_STREAM, 0); + int nMasterSocket = socket (PF_INET, SOCK_STREAM, 0); if (nMasterSocket < 0) { xLog.LineLog( MultiLog::LError, "Couldn't create a listen socket."); @@ -55,7 +63,13 @@ bool ConnectionManager::startServer( int nPort, int nInitPool ) name.sin_addr.s_addr = htonl( INADDR_ANY ); int opt = 1; - setsockopt( nMasterSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); + setsockopt( + nMasterSocket, + SOL_SOCKET, + SO_REUSEADDR, + (char *)&opt, + sizeof(opt) + ); if (bind (nMasterSocket, (struct sockaddr *) &name, sizeof (name)) < 0) { @@ -70,28 +84,21 @@ bool ConnectionManager::startServer( int nPort, int nInitPool ) } /* Initialize the set of active sockets. */ - FD_ZERO (&fdActive); - FD_ZERO (&fdRead); - FD_ZERO (&fdWrite); - FD_ZERO (&fdException); FD_SET (nMasterSocket, &fdActive); - for( int j = 0; j < nInitPool; j++ ) - { - lInactive.insert( lInactive.begin(), new Connection() ); - } + sMasterSocket[nMasterSocket] = nPort; return true; } -bool ConnectionManager::startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ) +bool ConnectionManager::startServer( int nPort, int nNumTries, int nTimeout ) { struct timeval xTimeout; for( int j = 0; j < nNumTries; j++ ) { xLog.LineLog( MultiLog::LStatus, "Attempting to create server socket (attempt [%d/%d])...", j+1, nNumTries ); - if( startServer( nPort, nInitPool ) == true ) + if( startServer( nPort ) == true ) { return true; } @@ -146,9 +153,9 @@ bool ConnectionManager::scanConnections( int nTimeout, bool bForceTimeout ) { if( FD_ISSET( i, &fdRead ) ) { - if( i == nMasterSocket ) + if( sMasterSocket.find( i ) != sMasterSocket.end() ) { - addConnection(); + addConnection( i ); } else { @@ -234,8 +241,13 @@ bool ConnectionManager::shutdownServer() axConPool[i].close(); } */ - shutdown( nMasterSocket, SHUT_RDWR ); - close( nMasterSocket ); + std::map::iterator i; + for( i = sMasterSocket.begin(); i != sMasterSocket.end(); i++ ) + { + int nSocket = (*i).first; + shutdown( nSocket, SHUT_RDWR ); + close( nSocket ); + } return true; } @@ -255,7 +267,7 @@ bool ConnectionManager::broadcastMessage( const char *lpData, int nExcludeSocket return true; } -bool ConnectionManager::addConnection() +bool ConnectionManager::addConnection( int nSocket ) { struct sockaddr_in clientname; size_t size; @@ -263,9 +275,9 @@ bool ConnectionManager::addConnection() size = sizeof( clientname ); #ifdef __CYGWIN__ - newSocket = accept( nMasterSocket, (struct sockaddr *) &clientname, (int *)&size ); + newSocket = accept( nSocket, (struct sockaddr *) &clientname, (int *)&size ); #else - newSocket = accept( nMasterSocket, (struct sockaddr *) &clientname, &size ); + newSocket = accept( nSocket, (struct sockaddr *) &clientname, &size ); #endif if( newSocket < 0 ) { @@ -305,7 +317,7 @@ bool ConnectionManager::addConnection() Connection *pCon = getInactiveConnection(); pCon->open( newSocket ); - pMonitor->onNewConnection( pCon ); + pMonitor->onNewConnection( pCon, (*sMasterSocket.find(nSocket)).second ); lActive.insert( lActive.end(), pCon ); diff --git a/src/connectionmanager.h b/src/connectionmanager.h index 669fcd5..da49a39 100644 --- a/src/connectionmanager.h +++ b/src/connectionmanager.h @@ -12,6 +12,7 @@ #include "connectionmonitor.h" #include #include +#include /** Manges incoming network connections as a server. Creates and works with * Connection objects. All operations are performed on TCP/IP v4 right now, @@ -26,7 +27,7 @@ public: * actually start a server, bind to a port, or create a connection pool. * That's all handled by startServer(). */ - ConnectionManager(); + ConnectionManager( int nInitPool=40 ); /** * Cleans up everything, and even clears out all still-connected Connection @@ -42,7 +43,7 @@ public: *@returns True if the socket was bound to the port and serving was * started. False if there was a problem connecting to the port. */ - bool startServer( int nPort, int nInitPool ); + bool startServer( int nPort ); /** * This is identicle to the simpler startServer function except that it @@ -59,7 +60,7 @@ public: *@returns True if the socket was bound to the port and serving was * started. False if there was a problem connecting to the port. */ - bool startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ); + bool startServer( int nPort, int nNumTries, int nTimeout ); /** * Scans all open connections, halting the calling processes until data @@ -98,9 +99,11 @@ private: * accept the connection, set the initial modes, and add it to the master * list of active connections, as well as fire off any messages that need * to be handled by anything else. + *@param nSocket The handle of the listening socket that had an incoming + * connection. *@returns True if everything worked, False otherwise. */ - bool addConnection(); + bool addConnection( int nSocket ); /** * Seraches the internal lists of connections for one with a specific @@ -122,7 +125,8 @@ private: */ Connection *getInactiveConnection(); - int nMasterSocket; /**< The listening or server socket. */ + std::map sMasterSocket; + //int nMasterSocket; /**< The listening or server socket. */ fd_set fdActive; /**< The active socket set. */ fd_set fdRead; /**< The sockets ready for a read. */ fd_set fdWrite; /**< The sockets ready for a write. */ diff --git a/src/connectionmonitor.h b/src/connectionmonitor.h index b96b533..01b2cca 100644 --- a/src/connectionmonitor.h +++ b/src/connectionmonitor.h @@ -25,10 +25,12 @@ public: /** Receives the notification that new connection was received. *@param pCon The connection that was created. + *@param nSocket The socket that the client connected to, used to determine + * which protocol to apply. *@returns Should return a true value if everything is OK, a false to * force a shutdown. */ - virtual bool onNewConnection( Connection *pCon ) = 0; + virtual bool onNewConnection( Connection *pCon, int nPort ) = 0; /** Receives the notification that a connection was closed. *@param pCon The connection that was closed. diff --git a/src/test/httpsrv/httpconnectionmonitor.cpp b/src/test/httpsrv/httpconnectionmonitor.cpp index eaadb36..ee1eab3 100644 --- a/src/test/httpsrv/httpconnectionmonitor.cpp +++ b/src/test/httpsrv/httpconnectionmonitor.cpp @@ -10,8 +10,9 @@ HttpConnectionMonitor::~HttpConnectionMonitor() { } -bool HttpConnectionMonitor::onNewConnection( Connection *pCon ) +bool HttpConnectionMonitor::onNewConnection( Connection *pCon, int nPort ) { + printf("Got connection on port %d\n", nPort ); Http hp( pCon ); pCon->readInput( 60, 0 ); diff --git a/src/test/httpsrv/httpconnectionmonitor.h b/src/test/httpsrv/httpconnectionmonitor.h index 63f29e4..30c0afd 100644 --- a/src/test/httpsrv/httpconnectionmonitor.h +++ b/src/test/httpsrv/httpconnectionmonitor.h @@ -9,7 +9,7 @@ public: HttpConnectionMonitor(); ~HttpConnectionMonitor(); - bool onNewConnection( Connection *pCon ); + bool onNewConnection( Connection *pCon, int nPort ); bool onClosedConnection( Connection *pCon ); }; diff --git a/src/test/httpsrv/main.cpp b/src/test/httpsrv/main.cpp index 4ee1ad3..2f1563c 100644 --- a/src/test/httpsrv/main.cpp +++ b/src/test/httpsrv/main.cpp @@ -10,7 +10,8 @@ int main() srv.setConnectionMonitor( &http ); - srv.startServer( 7331, 40 ); + printf("Listening on port 7331\n"); + srv.startServer( 7331 ); for(;;) { diff --git a/src/test/teltest/main.cpp b/src/test/teltest/main.cpp index ce968c4..5d3ec26 100644 --- a/src/test/teltest/main.cpp +++ b/src/test/teltest/main.cpp @@ -10,7 +10,7 @@ int main() srv.setConnectionMonitor( &telnet ); - srv.startServer( 4001, 40 ); + srv.startServer( 4001 ); for(;;) { diff --git a/src/test/teltest/telnetmonitor.cpp b/src/test/teltest/telnetmonitor.cpp index 001932f..32c2924 100644 --- a/src/test/teltest/telnetmonitor.cpp +++ b/src/test/teltest/telnetmonitor.cpp @@ -36,7 +36,7 @@ LinkMessage* TelnetMonitor::processIRM( LinkMessage *pMsg ) { } -bool TelnetMonitor::onNewConnection( Connection *pCon ) +bool TelnetMonitor::onNewConnection( Connection *pCon, int nPort ) { ProtocolTelnet *pt = new ProtocolTelnet(); pCon->setProtocol( pt ); diff --git a/src/test/teltest/telnetmonitor.h b/src/test/teltest/telnetmonitor.h index 95c8493..ba5761e 100644 --- a/src/test/teltest/telnetmonitor.h +++ b/src/test/teltest/telnetmonitor.h @@ -16,7 +16,7 @@ public: bool timeSlice(); LinkMessage* processIRM( LinkMessage *pMsgIn ); - bool onNewConnection( Connection *pCon ); + bool onNewConnection( Connection *pCon, int nPort ); bool onClosedConnection( Connection *pCon ); private: -- cgit v1.2.3