summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-09-11 04:22:18 +0000
committerMike Buland <eichlan@xagasoft.com>2007-09-11 04:22:18 +0000
commit3839f50c3e22089f346d76a40782223cdcfadca7 (patch)
tree2b0d1621314f46313a9c87984cfdbdfa125e7a27
parentc82dc43edc9fd913e8ddb20bebe778781ec0d6f7 (diff)
downloadlibbu++-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 '')
-rw-r--r--src/itoserver.cpp79
-rw-r--r--src/itoserver.h14
2 files changed, 41 insertions, 52 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
15Bu::ItoServer::~ItoServer() 15Bu::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
19void Bu::ItoServer::addPort( int nPort, int nPoolSize ) 31void 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/*
41void 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*/
86void Bu::ItoServer::addClient( int nSocket, int nPort ) 53void 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
94void *Bu::ItoServer::run() 64void *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
99void Bu::ItoServer::clientCleanup( int iSocket )
100{
101 imClients.lock();
102 ItoClient *pCli = hClients.get( iSocket );
103 imClients.unlock();
104 qClientCleanup.enqueue( pCli );
105}
106
122Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, 107Bu::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
139Bu::ItoServer::ItoClient::~ItoClient() 123Bu::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 }
diff --git a/src/itoserver.h b/src/itoserver.h
index 19c34ca..60b15b4 100644
--- a/src/itoserver.h
+++ b/src/itoserver.h
@@ -7,6 +7,7 @@
7#include "bu/list.h" 7#include "bu/list.h"
8#include "bu/ito.h" 8#include "bu/ito.h"
9#include "bu/itomutex.h" 9#include "bu/itomutex.h"
10#include "bu/itoqueue.h"
10#include "bu/set.h" 11#include "bu/set.h"
11 12
12namespace Bu 13namespace Bu
@@ -36,6 +37,7 @@ namespace Bu
36 */ 37 */
37 class ItoServer : public Ito 38 class ItoServer : public Ito
38 { 39 {
40 friend class ItoClient;
39 public: 41 public:
40 ItoServer(); 42 ItoServer();
41 virtual ~ItoServer(); 43 virtual ~ItoServer();
@@ -78,11 +80,13 @@ namespace Bu
78 fd_set fdActive; 80 fd_set fdActive;
79 typedef Hash<int,ServerSocket *> ServerHash; 81 typedef Hash<int,ServerSocket *> ServerHash;
80 ServerHash hServers; 82 ServerHash hServers;
81 //typedef Bu::Set<ItoClient *> ClientSet; 83 typedef Hash<int,ItoClient *> ClientHash;
82 //ClientSet sClients; 84 typedef ItoQueue<ItoClient *> ClientQueue;
83 //typedef Hash<int,Client *> ClientHash; 85 ClientHash hClients;
84 //ClientHash hClients; 86 ClientQueue qClientCleanup;
85 ItoMutex im; 87 ItoMutex imClients;
88
89 void clientCleanup( int iSocket );
86 }; 90 };
87} 91}
88 92