aboutsummaryrefslogtreecommitdiff
path: root/src/itoserver.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-02-07 19:01:12 +0000
committerMike Buland <eichlan@xagasoft.com>2008-02-07 19:01:12 +0000
commit9ab87f39d7fc37e52d742d38b191eed9128d4b5b (patch)
tree17a00529eda3d9a5a979c05d688f219f7af1631a /src/itoserver.cpp
parent5574841cc88412854b2cb94253152b3606803e2a (diff)
downloadlibbu++-9ab87f39d7fc37e52d742d38b191eed9128d4b5b.tar.gz
libbu++-9ab87f39d7fc37e52d742d38b191eed9128d4b5b.tar.bz2
libbu++-9ab87f39d7fc37e52d742d38b191eed9128d4b5b.tar.xz
libbu++-9ab87f39d7fc37e52d742d38b191eed9128d4b5b.zip
Wowee, I think all this new stuff works, Conduit I don't need now, so it's not
done yet. The Client class now supports a function called getLink() which returns a ClientLink object. This object may then be passed off to any other class and called to send messages to that client object. It is threadsafe if ItoServer is being used, and not for Server. Sending a message via a ClientLink calls the onMessage function on the assosiated protocol. Note that sending messages from within protocol event handlers or functions they call, while safe, may be slow and it's reccomended that you avoid this.
Diffstat (limited to '')
-rw-r--r--src/itoserver.cpp67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/itoserver.cpp b/src/itoserver.cpp
index 7426e8a..db5ae04 100644
--- a/src/itoserver.cpp
+++ b/src/itoserver.cpp
@@ -127,7 +127,8 @@ Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort,
127 FD_SET( iSocket, &fdActive ); 127 FD_SET( iSocket, &fdActive );
128 128
129 pClient = new Client( 129 pClient = new Client(
130 new Bu::Socket( iSocket ) 130 new Bu::Socket( iSocket ),
131 new SrvClientLinkFactory( rSrv )
131 ); 132 );
132} 133}
133 134
@@ -137,7 +138,10 @@ Bu::ItoServer::ItoClient::~ItoClient()
137 138
138void *Bu::ItoServer::ItoClient::run() 139void *Bu::ItoServer::ItoClient::run()
139{ 140{
141 imProto.lock();
140 rSrv.onNewConnection( pClient, iPort ); 142 rSrv.onNewConnection( pClient, iPort );
143 pClient->processOutput();
144 imProto.unlock();
141 145
142 for(;;) 146 for(;;)
143 { 147 {
@@ -151,14 +155,29 @@ void *Bu::ItoServer::ItoClient::run()
151 throw ExceptionBase("Error attempting to scan open connections."); 155 throw ExceptionBase("Error attempting to scan open connections.");
152 } 156 }
153 157
158 while( !qMsg.isEmpty() )
159 {
160 imProto.lock();
161 Bu::FString *pMsg = qMsg.dequeue();
162 pClient->onMessage( *pMsg );
163 delete pMsg;
164 pClient->processOutput();
165 imProto.unlock();
166 }
167
154 if( FD_ISSET( iSocket, &fdRead ) ) 168 if( FD_ISSET( iSocket, &fdRead ) )
155 { 169 {
170 imProto.lock();
156 pClient->processInput(); 171 pClient->processInput();
172 imProto.unlock();
157 if( !pClient->isOpen() ) 173 if( !pClient->isOpen() )
158 { 174 {
175 imProto.lock();
159 rSrv.onClosedConnection( pClient ); 176 rSrv.onClosedConnection( pClient );
177 imProto.unlock();
160 178
161 rSrv.clientCleanup( iSocket ); 179 rSrv.clientCleanup( iSocket );
180
162 return NULL; 181 return NULL;
163 } 182 }
164 } 183 }
@@ -166,9 +185,55 @@ void *Bu::ItoServer::ItoClient::run()
166 // Now we just try to write all the pending data on the socket. 185 // Now we just try to write all the pending data on the socket.
167 // this could be done better eventually, if we care about the socket 186 // this could be done better eventually, if we care about the socket
168 // wanting to accept writes (using a select). 187 // wanting to accept writes (using a select).
188 imProto.lock();
169 pClient->processOutput(); 189 pClient->processOutput();
190 imProto.unlock();
170 } 191 }
171 192
172 return NULL; 193 return NULL;
173} 194}
174 195
196Bu::ItoServer::SrvClientLink::SrvClientLink( ItoClient *pClient ) :
197 pClient( pClient )
198{
199}
200
201Bu::ItoServer::SrvClientLink::~SrvClientLink()
202{
203}
204
205void Bu::ItoServer::SrvClientLink::sendMsg( const Bu::FString &sMsg )
206{
207 if( !pClient->imProto.trylock() )
208 {
209 pClient->pClient->onMessage( sMsg );
210 pClient->pClient->processOutput();
211 pClient->imProto.unlock();
212 }
213 else
214 {
215 Bu::FString *pMsg = new Bu::FString( sMsg );
216 pClient->qMsg.enqueue( pMsg );
217 }
218}
219
220Bu::ItoServer::SrvClientLinkFactory::SrvClientLinkFactory(
221 Bu::ItoServer &rSrv ) :
222 rSrv( rSrv )
223{
224}
225
226Bu::ItoServer::SrvClientLinkFactory::~SrvClientLinkFactory()
227{
228}
229
230Bu::ClientLink *Bu::ItoServer::SrvClientLinkFactory::createLink(
231 Bu::Client *pClient )
232{
233 rSrv.imClients.lock();
234 ItoClient *pCli = rSrv.hClients.get( *pClient->getSocket() );
235 rSrv.imClients.unlock();
236
237 return new SrvClientLink( pCli );
238}
239