From 33012c4ef4d39efad4fbc2b19f494f5c860fff51 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 18 Jun 2007 22:44:45 +0000 Subject: The client/server system now works both ways, in and out, and works as well as the old one in pretty much every way, and better in most. It's much easier to understand. And the atom class is better. --- src/atom.h | 2 +- src/client.cpp | 25 +++++++++++++++++++++++++ src/client.h | 6 ++++++ src/server.cpp | 17 ++++++++++++++++- src/server.h | 3 ++- 5 files changed, 50 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/atom.h b/src/atom.h index 731e08b..f876274 100644 --- a/src/atom.h +++ b/src/atom.h @@ -27,7 +27,7 @@ namespace Bu clear(); } - bool isSet() const + bool has() const { return (pData != NULL); } diff --git a/src/client.cpp b/src/client.cpp index 6d7d81c..0e48285 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -58,6 +58,15 @@ void Bu::Client::processInput() } } +void Bu::Client::processOutput() +{ + if( sWriteBuf.getSize() > 0 ) + { + pSocket->write( sWriteBuf.getStr(), sWriteBuf.getSize() ); + sWriteBuf.clear(); + } +} + void Bu::Client::setProtocol( Protocol *pProto ) { this->pProto = pProto; @@ -78,3 +87,19 @@ Bu::FString &Bu::Client::getInput() return sReadBuf; } +Bu::FString &Bu::Client::getOutput() +{ + return sWriteBuf; +} + +bool Bu::Client::isOpen() +{ + if( !pSocket ) return false; + return pSocket->isOpen(); +} + +void Bu::Client::write( const char *pData, int nBytes ) +{ + sWriteBuf.append( pData, nBytes ); +} + diff --git a/src/client.h b/src/client.h index 1a189e2..02ba077 100644 --- a/src/client.h +++ b/src/client.h @@ -20,17 +20,23 @@ namespace Bu virtual ~Client(); void processInput(); + void processOutput(); Bu::FString &getInput(); + Bu::FString &getOutput(); + void write( const char *pData, int nBytes ); void setProtocol( Protocol *pProto ); Bu::Protocol *getProtocol(); void clearProtocol(); + bool isOpen(); + private: Bu::Socket *pSocket; Bu::Protocol *pProto; Bu::FString sReadBuf; + Bu::FString sWriteBuf; }; } diff --git a/src/server.cpp b/src/server.cpp index bceeb81..d07a597 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -61,10 +61,25 @@ void Bu::Server::scan() } else { - hClients.get( j )->processInput(); + Client *pClient = hClients.get( j ); + pClient->processInput(); + if( !pClient->isOpen() ) + { + onClosedConnection( pClient ); + hClients.erase( j ); + FD_CLR( j, &fdActive ); + } } } } + + // Now we just try to write all the pending data on all the sockets. + // this could be done better eventually, if we care about the socket + // wanting to accept writes (using a select). + for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) + { + (*i)->processOutput(); + } } void Bu::Server::addClient( int nSocket, int nPort ) diff --git a/src/server.h b/src/server.h index 3331d2c..07eef95 100644 --- a/src/server.h +++ b/src/server.h @@ -53,7 +53,8 @@ namespace Bu int nTimeoutUSec; fd_set fdActive; Hash hServers; - Hash hClients; + typedef Hash ClientHash; + ClientHash hClients; }; } -- cgit v1.2.3