diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-05-03 08:42:32 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-05-03 08:42:32 +0000 |
commit | 68db829db66dbf8faad5bd8df35760b5af3a1491 (patch) | |
tree | d1b79a641a14966dbd1bb2bea6625454db7f64a4 /src/connectionmanager.cpp | |
parent | 96b00553a0ffe6bb34af6ad15e1cfc2bed67bd75 (diff) | |
download | libbu++-68db829db66dbf8faad5bd8df35760b5af3a1491.tar.gz libbu++-68db829db66dbf8faad5bd8df35760b5af3a1491.tar.bz2 libbu++-68db829db66dbf8faad5bd8df35760b5af3a1491.tar.xz libbu++-68db829db66dbf8faad5bd8df35760b5af3a1491.zip |
Sweet, all changes from the (once again 1 rev) branch have been merged back in.
Now to finalize the changes in my other programs.
Diffstat (limited to 'src/connectionmanager.cpp')
-rw-r--r-- | src/connectionmanager.cpp | 58 |
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 | ||
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 | ||