diff options
| -rw-r--r-- | src/connectionmanager.cpp | 58 | ||||
| -rw-r--r-- | src/connectionmanager.h | 14 | ||||
| -rw-r--r-- | src/connectionmonitor.h | 4 | ||||
| -rw-r--r-- | src/test/httpsrv/httpconnectionmonitor.cpp | 3 | ||||
| -rw-r--r-- | src/test/httpsrv/httpconnectionmonitor.h | 2 | ||||
| -rw-r--r-- | src/test/httpsrv/main.cpp | 3 | ||||
| -rw-r--r-- | src/test/teltest/main.cpp | 2 | ||||
| -rw-r--r-- | src/test/teltest/telnetmonitor.cpp | 2 | ||||
| -rw-r--r-- | 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 @@ | |||
| 13 | #include "connectionmanager.h" | 13 | #include "connectionmanager.h" |
| 14 | #include <fcntl.h> | 14 | #include <fcntl.h> |
| 15 | 15 | ||
| 16 | ConnectionManager::ConnectionManager() : | 16 | ConnectionManager::ConnectionManager( int nInitPool ) : |
| 17 | xLog( MultiLog::getInstance() ) | 17 | xLog( MultiLog::getInstance() ) |
| 18 | { | 18 | { |
| 19 | nMasterSocket = -1; | 19 | //nMasterSocket = -1; |
| 20 | pMonitor = NULL; | 20 | pMonitor = NULL; |
| 21 | for( int j = 0; j < nInitPool; j++ ) | ||
| 22 | { | ||
| 23 | lInactive.insert( lInactive.begin(), new Connection() ); | ||
| 24 | } | ||
| 25 | FD_ZERO (&fdActive); | ||
| 26 | FD_ZERO (&fdRead); | ||
| 27 | FD_ZERO (&fdWrite); | ||
| 28 | FD_ZERO (&fdException); | ||
| 21 | } | 29 | } |
| 22 | 30 | ||
| 23 | ConnectionManager::~ConnectionManager() | 31 | ConnectionManager::~ConnectionManager() |
| @@ -33,13 +41,13 @@ ConnectionManager::~ConnectionManager() | |||
| 33 | } | 41 | } |
| 34 | } | 42 | } |
| 35 | 43 | ||
| 36 | bool ConnectionManager::startServer( int nPort, int nInitPool ) | 44 | bool ConnectionManager::startServer( int nPort ) |
| 37 | { | 45 | { |
| 38 | /* Create the socket and set it up to accept connections. */ | 46 | /* Create the socket and set it up to accept connections. */ |
| 39 | struct sockaddr_in name; | 47 | struct sockaddr_in name; |
| 40 | 48 | ||
| 41 | /* Create the socket. */ | 49 | /* Create the socket. */ |
| 42 | nMasterSocket = socket (PF_INET, SOCK_STREAM, 0); | 50 | int nMasterSocket = socket (PF_INET, SOCK_STREAM, 0); |
| 43 | if (nMasterSocket < 0) | 51 | if (nMasterSocket < 0) |
| 44 | { | 52 | { |
| 45 | xLog.LineLog( MultiLog::LError, "Couldn't create a listen socket."); | 53 | xLog.LineLog( MultiLog::LError, "Couldn't create a listen socket."); |
| @@ -55,7 +63,13 @@ bool ConnectionManager::startServer( int nPort, int nInitPool ) | |||
| 55 | name.sin_addr.s_addr = htonl( INADDR_ANY ); | 63 | name.sin_addr.s_addr = htonl( INADDR_ANY ); |
| 56 | 64 | ||
| 57 | int opt = 1; | 65 | int opt = 1; |
| 58 | setsockopt( nMasterSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); | 66 | setsockopt( |
| 67 | nMasterSocket, | ||
| 68 | SOL_SOCKET, | ||
| 69 | SO_REUSEADDR, | ||
| 70 | (char *)&opt, | ||
| 71 | sizeof(opt) | ||
| 72 | ); | ||
| 59 | 73 | ||
| 60 | if (bind (nMasterSocket, (struct sockaddr *) &name, sizeof (name)) < 0) | 74 | if (bind (nMasterSocket, (struct sockaddr *) &name, sizeof (name)) < 0) |
| 61 | { | 75 | { |
| @@ -70,28 +84,21 @@ bool ConnectionManager::startServer( int nPort, int nInitPool ) | |||
| 70 | } | 84 | } |
| 71 | 85 | ||
| 72 | /* Initialize the set of active sockets. */ | 86 | /* Initialize the set of active sockets. */ |
| 73 | FD_ZERO (&fdActive); | ||
| 74 | FD_ZERO (&fdRead); | ||
| 75 | FD_ZERO (&fdWrite); | ||
| 76 | FD_ZERO (&fdException); | ||
| 77 | FD_SET (nMasterSocket, &fdActive); | 87 | FD_SET (nMasterSocket, &fdActive); |
| 78 | 88 | ||
| 79 | for( int j = 0; j < nInitPool; j++ ) | 89 | sMasterSocket[nMasterSocket] = nPort; |
| 80 | { | ||
| 81 | lInactive.insert( lInactive.begin(), new Connection() ); | ||
| 82 | } | ||
| 83 | 90 | ||
| 84 | return true; | 91 | return true; |
| 85 | } | 92 | } |
| 86 | 93 | ||
| 87 | bool ConnectionManager::startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ) | 94 | bool ConnectionManager::startServer( int nPort, int nNumTries, int nTimeout ) |
| 88 | { | 95 | { |
| 89 | struct timeval xTimeout; | 96 | struct timeval xTimeout; |
| 90 | 97 | ||
| 91 | for( int j = 0; j < nNumTries; j++ ) | 98 | for( int j = 0; j < nNumTries; j++ ) |
| 92 | { | 99 | { |
| 93 | xLog.LineLog( MultiLog::LStatus, "Attempting to create server socket (attempt [%d/%d])...", j+1, nNumTries ); | 100 | xLog.LineLog( MultiLog::LStatus, "Attempting to create server socket (attempt [%d/%d])...", j+1, nNumTries ); |
| 94 | if( startServer( nPort, nInitPool ) == true ) | 101 | if( startServer( nPort ) == true ) |
| 95 | { | 102 | { |
| 96 | return true; | 103 | return true; |
| 97 | } | 104 | } |
| @@ -146,9 +153,9 @@ bool ConnectionManager::scanConnections( int nTimeout, bool bForceTimeout ) | |||
| 146 | { | 153 | { |
| 147 | if( FD_ISSET( i, &fdRead ) ) | 154 | if( FD_ISSET( i, &fdRead ) ) |
| 148 | { | 155 | { |
| 149 | if( i == nMasterSocket ) | 156 | if( sMasterSocket.find( i ) != sMasterSocket.end() ) |
| 150 | { | 157 | { |
| 151 | addConnection(); | 158 | addConnection( i ); |
| 152 | } | 159 | } |
| 153 | else | 160 | else |
| 154 | { | 161 | { |
| @@ -234,8 +241,13 @@ bool ConnectionManager::shutdownServer() | |||
| 234 | axConPool[i].close(); | 241 | axConPool[i].close(); |
| 235 | } | 242 | } |
| 236 | */ | 243 | */ |
| 237 | shutdown( nMasterSocket, SHUT_RDWR ); | 244 | std::map<int,int>::iterator i; |
| 238 | close( nMasterSocket ); | 245 | for( i = sMasterSocket.begin(); i != sMasterSocket.end(); i++ ) |
| 246 | { | ||
| 247 | int nSocket = (*i).first; | ||
| 248 | shutdown( nSocket, SHUT_RDWR ); | ||
| 249 | close( nSocket ); | ||
| 250 | } | ||
| 239 | 251 | ||
| 240 | return true; | 252 | return true; |
| 241 | } | 253 | } |
| @@ -255,7 +267,7 @@ bool ConnectionManager::broadcastMessage( const char *lpData, int nExcludeSocket | |||
| 255 | return true; | 267 | return true; |
| 256 | } | 268 | } |
| 257 | 269 | ||
| 258 | bool ConnectionManager::addConnection() | 270 | bool ConnectionManager::addConnection( int nSocket ) |
| 259 | { | 271 | { |
| 260 | struct sockaddr_in clientname; | 272 | struct sockaddr_in clientname; |
| 261 | size_t size; | 273 | size_t size; |
| @@ -263,9 +275,9 @@ bool ConnectionManager::addConnection() | |||
| 263 | 275 | ||
| 264 | size = sizeof( clientname ); | 276 | size = sizeof( clientname ); |
| 265 | #ifdef __CYGWIN__ | 277 | #ifdef __CYGWIN__ |
| 266 | newSocket = accept( nMasterSocket, (struct sockaddr *) &clientname, (int *)&size ); | 278 | newSocket = accept( nSocket, (struct sockaddr *) &clientname, (int *)&size ); |
| 267 | #else | 279 | #else |
| 268 | newSocket = accept( nMasterSocket, (struct sockaddr *) &clientname, &size ); | 280 | newSocket = accept( nSocket, (struct sockaddr *) &clientname, &size ); |
| 269 | #endif | 281 | #endif |
| 270 | if( newSocket < 0 ) | 282 | if( newSocket < 0 ) |
| 271 | { | 283 | { |
| @@ -305,7 +317,7 @@ bool ConnectionManager::addConnection() | |||
| 305 | Connection *pCon = getInactiveConnection(); | 317 | Connection *pCon = getInactiveConnection(); |
| 306 | pCon->open( newSocket ); | 318 | pCon->open( newSocket ); |
| 307 | 319 | ||
| 308 | pMonitor->onNewConnection( pCon ); | 320 | pMonitor->onNewConnection( pCon, (*sMasterSocket.find(nSocket)).second ); |
| 309 | 321 | ||
| 310 | lActive.insert( lActive.end(), pCon ); | 322 | lActive.insert( lActive.end(), pCon ); |
| 311 | 323 | ||
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 @@ | |||
| 12 | #include "connectionmonitor.h" | 12 | #include "connectionmonitor.h" |
| 13 | #include <sys/types.h> | 13 | #include <sys/types.h> |
| 14 | #include <list> | 14 | #include <list> |
| 15 | #include <map> | ||
| 15 | 16 | ||
| 16 | /** Manges incoming network connections as a server. Creates and works with | 17 | /** Manges incoming network connections as a server. Creates and works with |
| 17 | * Connection objects. All operations are performed on TCP/IP v4 right now, | 18 | * Connection objects. All operations are performed on TCP/IP v4 right now, |
| @@ -26,7 +27,7 @@ public: | |||
| 26 | * actually start a server, bind to a port, or create a connection pool. | 27 | * actually start a server, bind to a port, or create a connection pool. |
| 27 | * That's all handled by startServer(). | 28 | * That's all handled by startServer(). |
| 28 | */ | 29 | */ |
| 29 | ConnectionManager(); | 30 | ConnectionManager( int nInitPool=40 ); |
| 30 | 31 | ||
| 31 | /** | 32 | /** |
| 32 | * Cleans up everything, and even clears out all still-connected Connection | 33 | * Cleans up everything, and even clears out all still-connected Connection |
| @@ -42,7 +43,7 @@ public: | |||
| 42 | *@returns True if the socket was bound to the port and serving was | 43 | *@returns True if the socket was bound to the port and serving was |
| 43 | * started. False if there was a problem connecting to the port. | 44 | * started. False if there was a problem connecting to the port. |
| 44 | */ | 45 | */ |
| 45 | bool startServer( int nPort, int nInitPool ); | 46 | bool startServer( int nPort ); |
| 46 | 47 | ||
| 47 | /** | 48 | /** |
| 48 | * This is identicle to the simpler startServer function except that it | 49 | * This is identicle to the simpler startServer function except that it |
| @@ -59,7 +60,7 @@ public: | |||
| 59 | *@returns True if the socket was bound to the port and serving was | 60 | *@returns True if the socket was bound to the port and serving was |
| 60 | * started. False if there was a problem connecting to the port. | 61 | * started. False if there was a problem connecting to the port. |
| 61 | */ | 62 | */ |
| 62 | bool startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ); | 63 | bool startServer( int nPort, int nNumTries, int nTimeout ); |
| 63 | 64 | ||
| 64 | /** | 65 | /** |
| 65 | * Scans all open connections, halting the calling processes until data | 66 | * Scans all open connections, halting the calling processes until data |
| @@ -98,9 +99,11 @@ private: | |||
| 98 | * accept the connection, set the initial modes, and add it to the master | 99 | * accept the connection, set the initial modes, and add it to the master |
| 99 | * list of active connections, as well as fire off any messages that need | 100 | * list of active connections, as well as fire off any messages that need |
| 100 | * to be handled by anything else. | 101 | * to be handled by anything else. |
| 102 | *@param nSocket The handle of the listening socket that had an incoming | ||
| 103 | * connection. | ||
| 101 | *@returns True if everything worked, False otherwise. | 104 | *@returns True if everything worked, False otherwise. |
| 102 | */ | 105 | */ |
| 103 | bool addConnection(); | 106 | bool addConnection( int nSocket ); |
| 104 | 107 | ||
| 105 | /** | 108 | /** |
| 106 | * Seraches the internal lists of connections for one with a specific | 109 | * Seraches the internal lists of connections for one with a specific |
| @@ -122,7 +125,8 @@ private: | |||
| 122 | */ | 125 | */ |
| 123 | Connection *getInactiveConnection(); | 126 | Connection *getInactiveConnection(); |
| 124 | 127 | ||
| 125 | int nMasterSocket; /**< The listening or server socket. */ | 128 | std::map<int,int> sMasterSocket; |
| 129 | //int nMasterSocket; /**< The listening or server socket. */ | ||
| 126 | fd_set fdActive; /**< The active socket set. */ | 130 | fd_set fdActive; /**< The active socket set. */ |
| 127 | fd_set fdRead; /**< The sockets ready for a read. */ | 131 | fd_set fdRead; /**< The sockets ready for a read. */ |
| 128 | fd_set fdWrite; /**< The sockets ready for a write. */ | 132 | 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: | |||
| 25 | 25 | ||
| 26 | /** Receives the notification that new connection was received. | 26 | /** Receives the notification that new connection was received. |
| 27 | *@param pCon The connection that was created. | 27 | *@param pCon The connection that was created. |
| 28 | *@param nSocket The socket that the client connected to, used to determine | ||
| 29 | * which protocol to apply. | ||
| 28 | *@returns Should return a true value if everything is OK, a false to | 30 | *@returns Should return a true value if everything is OK, a false to |
| 29 | * force a shutdown. | 31 | * force a shutdown. |
| 30 | */ | 32 | */ |
| 31 | virtual bool onNewConnection( Connection *pCon ) = 0; | 33 | virtual bool onNewConnection( Connection *pCon, int nPort ) = 0; |
| 32 | 34 | ||
| 33 | /** Receives the notification that a connection was closed. | 35 | /** Receives the notification that a connection was closed. |
| 34 | *@param pCon The connection that was closed. | 36 | *@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() | |||
| 10 | { | 10 | { |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | bool HttpConnectionMonitor::onNewConnection( Connection *pCon ) | 13 | bool HttpConnectionMonitor::onNewConnection( Connection *pCon, int nPort ) |
| 14 | { | 14 | { |
| 15 | printf("Got connection on port %d\n", nPort ); | ||
| 15 | Http hp( pCon ); | 16 | Http hp( pCon ); |
| 16 | 17 | ||
| 17 | pCon->readInput( 60, 0 ); | 18 | 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: | |||
| 9 | HttpConnectionMonitor(); | 9 | HttpConnectionMonitor(); |
| 10 | ~HttpConnectionMonitor(); | 10 | ~HttpConnectionMonitor(); |
| 11 | 11 | ||
| 12 | bool onNewConnection( Connection *pCon ); | 12 | bool onNewConnection( Connection *pCon, int nPort ); |
| 13 | bool onClosedConnection( Connection *pCon ); | 13 | bool onClosedConnection( Connection *pCon ); |
| 14 | }; | 14 | }; |
| 15 | 15 | ||
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() | |||
| 10 | 10 | ||
| 11 | srv.setConnectionMonitor( &http ); | 11 | srv.setConnectionMonitor( &http ); |
| 12 | 12 | ||
| 13 | srv.startServer( 7331, 40 ); | 13 | printf("Listening on port 7331\n"); |
| 14 | srv.startServer( 7331 ); | ||
| 14 | 15 | ||
| 15 | for(;;) | 16 | for(;;) |
| 16 | { | 17 | { |
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() | |||
| 10 | 10 | ||
| 11 | srv.setConnectionMonitor( &telnet ); | 11 | srv.setConnectionMonitor( &telnet ); |
| 12 | 12 | ||
| 13 | srv.startServer( 4001, 40 ); | 13 | srv.startServer( 4001 ); |
| 14 | 14 | ||
| 15 | for(;;) | 15 | for(;;) |
| 16 | { | 16 | { |
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 ) | |||
| 36 | { | 36 | { |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | bool TelnetMonitor::onNewConnection( Connection *pCon ) | 39 | bool TelnetMonitor::onNewConnection( Connection *pCon, int nPort ) |
| 40 | { | 40 | { |
| 41 | ProtocolTelnet *pt = new ProtocolTelnet(); | 41 | ProtocolTelnet *pt = new ProtocolTelnet(); |
| 42 | pCon->setProtocol( pt ); | 42 | 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: | |||
| 16 | bool timeSlice(); | 16 | bool timeSlice(); |
| 17 | LinkMessage* processIRM( LinkMessage *pMsgIn ); | 17 | LinkMessage* processIRM( LinkMessage *pMsgIn ); |
| 18 | 18 | ||
| 19 | bool onNewConnection( Connection *pCon ); | 19 | bool onNewConnection( Connection *pCon, int nPort ); |
| 20 | bool onClosedConnection( Connection *pCon ); | 20 | bool onClosedConnection( Connection *pCon ); |
| 21 | 21 | ||
| 22 | private: | 22 | private: |
