aboutsummaryrefslogtreecommitdiff
path: root/src/connectionmanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/connectionmanager.cpp58
1 files changed, 35 insertions, 23 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
16ConnectionManager::ConnectionManager() : 16ConnectionManager::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
23ConnectionManager::~ConnectionManager() 31ConnectionManager::~ConnectionManager()
@@ -33,13 +41,13 @@ ConnectionManager::~ConnectionManager()
33 } 41 }
34} 42}
35 43
36bool ConnectionManager::startServer( int nPort, int nInitPool ) 44bool 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
87bool ConnectionManager::startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ) 94bool 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
258bool ConnectionManager::addConnection() 270bool 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