diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2009-01-11 23:07:44 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2009-01-11 23:07:44 +0000 |
| commit | 1291252a7b1317ad2dc13fbeb15f6e9d2d92192c (patch) | |
| tree | 5eb88c7cef5417c043b03b34a77334c57286e526 /src | |
| parent | 595afe519791894c77c77a949fba7732f4d94872 (diff) | |
| download | libbu++-1291252a7b1317ad2dc13fbeb15f6e9d2d92192c.tar.gz libbu++-1291252a7b1317ad2dc13fbeb15f6e9d2d92192c.tar.bz2 libbu++-1291252a7b1317ad2dc13fbeb15f6e9d2d92192c.tar.xz libbu++-1291252a7b1317ad2dc13fbeb15f6e9d2d92192c.zip | |
A new feature has been added to Bu::Server. It's going to be trickier to
implement in Bu::ItoServer, but I'll get to it. Basically you can trigger a
"tick" any time you want and it will propegate as an onTick event to all active
clients. You can also have these generated automatically everytime the system
passes through a polling cycle. In this case, you should never ever write
data to the socket every tick. It will cause your program to go bursurk.
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.cpp | 6 | ||||
| -rw-r--r-- | src/client.h | 1 | ||||
| -rw-r--r-- | src/protocol.cpp | 12 | ||||
| -rw-r--r-- | src/protocol.h | 5 | ||||
| -rw-r--r-- | src/server.cpp | 19 | ||||
| -rw-r--r-- | src/server.h | 4 | ||||
| -rw-r--r-- | src/tests/serverticks.cpp | 61 |
7 files changed, 105 insertions, 3 deletions
diff --git a/src/client.cpp b/src/client.cpp index 0ab0618..81f2285 100644 --- a/src/client.cpp +++ b/src/client.cpp | |||
| @@ -247,3 +247,9 @@ void Bu::Client::onMessage( const Bu::FString &sMsg ) | |||
| 247 | pProto->onMessage( this, sMsg ); | 247 | pProto->onMessage( this, sMsg ); |
| 248 | } | 248 | } |
| 249 | 249 | ||
| 250 | void Bu::Client::tick() | ||
| 251 | { | ||
| 252 | if( pProto ) | ||
| 253 | pProto->onTick( this ); | ||
| 254 | } | ||
| 255 | |||
diff --git a/src/client.h b/src/client.h index 2cd700b..ae2815b 100644 --- a/src/client.h +++ b/src/client.h | |||
| @@ -54,6 +54,7 @@ namespace Bu | |||
| 54 | 54 | ||
| 55 | bool isOpen(); | 55 | bool isOpen(); |
| 56 | void close(); | 56 | void close(); |
| 57 | void tick(); | ||
| 57 | 58 | ||
| 58 | const Bu::Socket *getSocket() const; | 59 | const Bu::Socket *getSocket() const; |
| 59 | 60 | ||
diff --git a/src/protocol.cpp b/src/protocol.cpp index 51ff105..b0efc07 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp | |||
| @@ -17,7 +17,19 @@ Bu::Protocol::~Protocol() | |||
| 17 | { | 17 | { |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | void Bu::Protocol::onNewConnection( Bu::Client * ) | ||
| 21 | { | ||
| 22 | } | ||
| 23 | |||
| 24 | void Bu::Protocol::onNewData( Bu::Client * ) | ||
| 25 | { | ||
| 26 | } | ||
| 27 | |||
| 20 | void Bu::Protocol::onMessage( Bu::Client *, const Bu::FString & ) | 28 | void Bu::Protocol::onMessage( Bu::Client *, const Bu::FString & ) |
| 21 | { | 29 | { |
| 22 | } | 30 | } |
| 23 | 31 | ||
| 32 | void Bu::Protocol::onTick( Bu::Client * ) | ||
| 33 | { | ||
| 34 | } | ||
| 35 | |||
diff --git a/src/protocol.h b/src/protocol.h index 2c84bfd..64e5ca7 100644 --- a/src/protocol.h +++ b/src/protocol.h | |||
| @@ -26,9 +26,10 @@ namespace Bu | |||
| 26 | Protocol(); | 26 | Protocol(); |
| 27 | virtual ~Protocol(); | 27 | virtual ~Protocol(); |
| 28 | 28 | ||
| 29 | virtual void onNewConnection( Bu::Client *pClient )=0; | 29 | virtual void onNewConnection( Bu::Client *pClient ); |
| 30 | virtual void onNewData( Bu::Client *pClient )=0; | 30 | virtual void onNewData( Bu::Client *pClient ); |
| 31 | virtual void onMessage( Bu::Client *pClient, const Bu::FString &sMsg ); | 31 | virtual void onMessage( Bu::Client *pClient, const Bu::FString &sMsg ); |
| 32 | virtual void onTick( Bu::Client *pClient ); | ||
| 32 | 33 | ||
| 33 | private: | 34 | private: |
| 34 | 35 | ||
diff --git a/src/server.cpp b/src/server.cpp index 804bbcc..b7dec41 100644 --- a/src/server.cpp +++ b/src/server.cpp | |||
| @@ -14,7 +14,8 @@ | |||
| 14 | 14 | ||
| 15 | Bu::Server::Server() : | 15 | Bu::Server::Server() : |
| 16 | nTimeoutSec( 0 ), | 16 | nTimeoutSec( 0 ), |
| 17 | nTimeoutUSec( 0 ) | 17 | nTimeoutUSec( 0 ), |
| 18 | bAutoTick( false ) | ||
| 18 | { | 19 | { |
| 19 | FD_ZERO( &fdActive ); | 20 | FD_ZERO( &fdActive ); |
| 20 | } | 21 | } |
| @@ -123,6 +124,9 @@ void Bu::Server::scan() | |||
| 123 | hClients.erase( *i ); | 124 | hClients.erase( *i ); |
| 124 | FD_CLR( *i, &fdActive ); | 125 | FD_CLR( *i, &fdActive ); |
| 125 | } | 126 | } |
| 127 | |||
| 128 | if( bAutoTick ) | ||
| 129 | tick(); | ||
| 126 | } | 130 | } |
| 127 | 131 | ||
| 128 | void Bu::Server::addClient( int nSocket, int nPort ) | 132 | void Bu::Server::addClient( int nSocket, int nPort ) |
| @@ -166,3 +170,16 @@ Bu::ClientLink *Bu::Server::SrvClientLinkFactory::createLink( | |||
| 166 | return new SrvClientLink( pClient ); | 170 | return new SrvClientLink( pClient ); |
| 167 | } | 171 | } |
| 168 | 172 | ||
| 173 | void Bu::Server::setAutoTick( bool bEnable ) | ||
| 174 | { | ||
| 175 | bAutoTick = bEnable; | ||
| 176 | } | ||
| 177 | |||
| 178 | void Bu::Server::tick() | ||
| 179 | { | ||
| 180 | for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) | ||
| 181 | { | ||
| 182 | (*i)->tick(); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
diff --git a/src/server.h b/src/server.h index 42cbb6a..55de081 100644 --- a/src/server.h +++ b/src/server.h | |||
| @@ -58,6 +58,9 @@ namespace Bu | |||
| 58 | 58 | ||
| 59 | void addClient( int nSocket, int nPort ); | 59 | void addClient( int nSocket, int nPort ); |
| 60 | 60 | ||
| 61 | void setAutoTick( bool bEnable=true ); | ||
| 62 | void tick(); | ||
| 63 | |||
| 61 | virtual void onNewConnection( Client *pClient, int nPort )=0; | 64 | virtual void onNewConnection( Client *pClient, int nPort )=0; |
| 62 | virtual void onClosedConnection( Client *pClient )=0; | 65 | virtual void onClosedConnection( Client *pClient )=0; |
| 63 | 66 | ||
| @@ -90,6 +93,7 @@ namespace Bu | |||
| 90 | SrvHash hServers; | 93 | SrvHash hServers; |
| 91 | typedef Hash<int,Client *> ClientHash; | 94 | typedef Hash<int,Client *> ClientHash; |
| 92 | ClientHash hClients; | 95 | ClientHash hClients; |
| 96 | bool bAutoTick; | ||
| 93 | }; | 97 | }; |
| 94 | } | 98 | } |
| 95 | 99 | ||
diff --git a/src/tests/serverticks.cpp b/src/tests/serverticks.cpp new file mode 100644 index 0000000..80c4dfa --- /dev/null +++ b/src/tests/serverticks.cpp | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #include "bu/server.h" | ||
| 2 | #include "bu/client.h" | ||
| 3 | #include "bu/protocol.h" | ||
| 4 | |||
| 5 | class TickProtocol : public Bu::Protocol | ||
| 6 | { | ||
| 7 | public: | ||
| 8 | TickProtocol() | ||
| 9 | { | ||
| 10 | } | ||
| 11 | |||
| 12 | virtual ~TickProtocol() | ||
| 13 | { | ||
| 14 | } | ||
| 15 | |||
| 16 | virtual void onTick( Bu::Client *pClient ) | ||
| 17 | { | ||
| 18 | printf("tick!\n"); | ||
| 19 | pClient->write("tick!\n"); | ||
| 20 | } | ||
| 21 | }; | ||
| 22 | |||
| 23 | class TickServer : public Bu::Server | ||
| 24 | { | ||
| 25 | public: | ||
| 26 | TickServer() | ||
| 27 | { | ||
| 28 | } | ||
| 29 | |||
| 30 | virtual ~TickServer() | ||
| 31 | { | ||
| 32 | } | ||
| 33 | |||
| 34 | virtual void onNewConnection( Bu::Client *pClient, int ) | ||
| 35 | { | ||
| 36 | pClient->setProtocol( new TickProtocol() ); | ||
| 37 | } | ||
| 38 | |||
| 39 | virtual void onClosedConnection( Bu::Client *pClient ) | ||
| 40 | { | ||
| 41 | delete pClient->getProtocol(); | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | int main( int , char *[] ) | ||
| 46 | { | ||
| 47 | TickServer ts; | ||
| 48 | |||
| 49 | ts.setTimeout( 1, 0 ); | ||
| 50 | ts.setAutoTick(); | ||
| 51 | ts.addPort( 5555 ); | ||
| 52 | |||
| 53 | for(;;) | ||
| 54 | { | ||
| 55 | ts.scan(); | ||
| 56 | sleep( 1 ); | ||
| 57 | } | ||
| 58 | |||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
