diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-09-11 04:22:18 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-09-11 04:22:18 +0000 |
commit | 3839f50c3e22089f346d76a40782223cdcfadca7 (patch) | |
tree | 2b0d1621314f46313a9c87984cfdbdfa125e7a27 /src/itoserver.cpp | |
parent | c82dc43edc9fd913e8ddb20bebe778781ec0d6f7 (diff) | |
download | libbu++-3839f50c3e22089f346d76a40782223cdcfadca7.tar.gz libbu++-3839f50c3e22089f346d76a40782223cdcfadca7.tar.bz2 libbu++-3839f50c3e22089f346d76a40782223cdcfadca7.tar.xz libbu++-3839f50c3e22089f346d76a40782223cdcfadca7.zip |
Ok...forgot a couple of things. Bu::ItoServer now monitors all client
connections and actually cleans up behind them when they're all done. Seems
important. It also will cleanup any lingering sockets that are laying around
at destruction time, although right now unless you force-stop the server thread
there is no way to interrupt it. That'll come in a bit.
Diffstat (limited to 'src/itoserver.cpp')
-rw-r--r-- | src/itoserver.cpp | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/src/itoserver.cpp b/src/itoserver.cpp index 0337057..1bad872 100644 --- a/src/itoserver.cpp +++ b/src/itoserver.cpp | |||
@@ -14,6 +14,18 @@ Bu::ItoServer::ItoServer() : | |||
14 | 14 | ||
15 | Bu::ItoServer::~ItoServer() | 15 | Bu::ItoServer::~ItoServer() |
16 | { | 16 | { |
17 | while( !qClientCleanup.isEmpty() ) | ||
18 | { | ||
19 | ItoClient *pCli = qClientCleanup.dequeue(); | ||
20 | pCli->join(); | ||
21 | delete pCli; | ||
22 | } | ||
23 | for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) | ||
24 | { | ||
25 | ItoClient *pCli = (*i); | ||
26 | pCli->join(); | ||
27 | delete pCli; | ||
28 | } | ||
17 | } | 29 | } |
18 | 30 | ||
19 | void Bu::ItoServer::addPort( int nPort, int nPoolSize ) | 31 | void Bu::ItoServer::addPort( int nPort, int nPoolSize ) |
@@ -37,58 +49,16 @@ void Bu::ItoServer::setTimeout( int nTimeoutSec, int nTimeoutUSec ) | |||
37 | this->nTimeoutSec = nTimeoutSec; | 49 | this->nTimeoutSec = nTimeoutSec; |
38 | this->nTimeoutUSec = nTimeoutUSec; | 50 | this->nTimeoutUSec = nTimeoutUSec; |
39 | } | 51 | } |
40 | /* | ||
41 | void Bu::ItoServer::scan() | ||
42 | { | ||
43 | struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; | ||
44 | |||
45 | fd_set fdRead = fdActive; | ||
46 | fd_set fdWrite = fdActive; | ||
47 | fd_set fdException = fdActive; | ||
48 | |||
49 | if( TEMP_FAILURE_RETRY( select( FD_SETSIZE, &fdRead, NULL, &fdException, &xTimeout ) ) < 0 ) | ||
50 | { | ||
51 | throw ExceptionBase("Error attempting to scan open connections."); | ||
52 | } | ||
53 | 52 | ||
54 | for( int j = 0; j < FD_SETSIZE; j++ ) | ||
55 | { | ||
56 | if( FD_ISSET( j, &fdRead ) ) | ||
57 | { | ||
58 | if( hServers.has( j ) ) | ||
59 | { | ||
60 | ServerSocket *pSrv = hServers.get( j ); | ||
61 | addClient( pSrv->accept(), pSrv->getPort() ); | ||
62 | } | ||
63 | else | ||
64 | { | ||
65 | Client *pClient = hClients.get( j ); | ||
66 | pClient->processInput(); | ||
67 | if( !pClient->isOpen() ) | ||
68 | { | ||
69 | onClosedConnection( pClient ); | ||
70 | hClients.erase( j ); | ||
71 | FD_CLR( j, &fdActive ); | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | // Now we just try to write all the pending data on all the sockets. | ||
78 | // this could be done better eventually, if we care about the socket | ||
79 | // wanting to accept writes (using a select). | ||
80 | for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) | ||
81 | { | ||
82 | (*i)->processOutput(); | ||
83 | } | ||
84 | } | ||
85 | */ | ||
86 | void Bu::ItoServer::addClient( int nSocket, int nPort ) | 53 | void Bu::ItoServer::addClient( int nSocket, int nPort ) |
87 | { | 54 | { |
88 | ItoClient *pC = new ItoClient( *this, nSocket, nPort, nTimeoutSec, | 55 | ItoClient *pC = new ItoClient( *this, nSocket, nPort, nTimeoutSec, |
89 | nTimeoutUSec ); | 56 | nTimeoutUSec ); |
90 | pC->start(); | 57 | pC->start(); |
91 | 58 | ||
59 | imClients.lock(); | ||
60 | hClients.insert( nSocket, pC ); | ||
61 | imClients.unlock(); | ||
92 | } | 62 | } |
93 | 63 | ||
94 | void *Bu::ItoServer::run() | 64 | void *Bu::ItoServer::run() |
@@ -114,11 +84,26 @@ void *Bu::ItoServer::run() | |||
114 | addClient( pSrv->accept(), pSrv->getPort() ); | 84 | addClient( pSrv->accept(), pSrv->getPort() ); |
115 | } | 85 | } |
116 | } | 86 | } |
87 | |||
88 | while( !qClientCleanup.isEmpty() ) | ||
89 | { | ||
90 | ItoClient *pCli = qClientCleanup.dequeue(); | ||
91 | pCli->join(); | ||
92 | delete pCli; | ||
93 | } | ||
117 | } | 94 | } |
118 | 95 | ||
119 | return NULL; | 96 | return NULL; |
120 | } | 97 | } |
121 | 98 | ||
99 | void Bu::ItoServer::clientCleanup( int iSocket ) | ||
100 | { | ||
101 | imClients.lock(); | ||
102 | ItoClient *pCli = hClients.get( iSocket ); | ||
103 | imClients.unlock(); | ||
104 | qClientCleanup.enqueue( pCli ); | ||
105 | } | ||
106 | |||
122 | Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, | 107 | Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, |
123 | int nTimeoutSec, int nTimeoutUSec ) : | 108 | int nTimeoutSec, int nTimeoutUSec ) : |
124 | rSrv( rSrv ), | 109 | rSrv( rSrv ), |
@@ -133,7 +118,6 @@ Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, | |||
133 | pClient = new Client( | 118 | pClient = new Client( |
134 | new Bu::Socket( iSocket ) | 119 | new Bu::Socket( iSocket ) |
135 | ); | 120 | ); |
136 | |||
137 | } | 121 | } |
138 | 122 | ||
139 | Bu::ItoServer::ItoClient::~ItoClient() | 123 | Bu::ItoServer::ItoClient::~ItoClient() |
@@ -163,6 +147,7 @@ void *Bu::ItoServer::ItoClient::run() | |||
163 | { | 147 | { |
164 | rSrv.onClosedConnection( pClient ); | 148 | rSrv.onClosedConnection( pClient ); |
165 | 149 | ||
150 | rSrv.clientCleanup( iSocket ); | ||
166 | return NULL; | 151 | return NULL; |
167 | } | 152 | } |
168 | } | 153 | } |