From 1291252a7b1317ad2dc13fbeb15f6e9d2d92192c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 11 Jan 2009 23:07:44 +0000 Subject: 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. --- src/client.cpp | 6 +++++ src/client.h | 1 + src/protocol.cpp | 12 ++++++++++ src/protocol.h | 5 ++-- src/server.cpp | 19 ++++++++++++++- src/server.h | 4 ++++ src/tests/serverticks.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/tests/serverticks.cpp 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 ) pProto->onMessage( this, sMsg ); } +void Bu::Client::tick() +{ + if( pProto ) + pProto->onTick( this ); +} + 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 bool isOpen(); void close(); + void tick(); const Bu::Socket *getSocket() const; 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() { } +void Bu::Protocol::onNewConnection( Bu::Client * ) +{ +} + +void Bu::Protocol::onNewData( Bu::Client * ) +{ +} + void Bu::Protocol::onMessage( Bu::Client *, const Bu::FString & ) { } +void Bu::Protocol::onTick( Bu::Client * ) +{ +} + 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 Protocol(); virtual ~Protocol(); - virtual void onNewConnection( Bu::Client *pClient )=0; - virtual void onNewData( Bu::Client *pClient )=0; + virtual void onNewConnection( Bu::Client *pClient ); + virtual void onNewData( Bu::Client *pClient ); virtual void onMessage( Bu::Client *pClient, const Bu::FString &sMsg ); + virtual void onTick( Bu::Client *pClient ); private: 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 @@ Bu::Server::Server() : nTimeoutSec( 0 ), - nTimeoutUSec( 0 ) + nTimeoutUSec( 0 ), + bAutoTick( false ) { FD_ZERO( &fdActive ); } @@ -123,6 +124,9 @@ void Bu::Server::scan() hClients.erase( *i ); FD_CLR( *i, &fdActive ); } + + if( bAutoTick ) + tick(); } void Bu::Server::addClient( int nSocket, int nPort ) @@ -166,3 +170,16 @@ Bu::ClientLink *Bu::Server::SrvClientLinkFactory::createLink( return new SrvClientLink( pClient ); } +void Bu::Server::setAutoTick( bool bEnable ) +{ + bAutoTick = bEnable; +} + +void Bu::Server::tick() +{ + for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) + { + (*i)->tick(); + } +} + 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 void addClient( int nSocket, int nPort ); + void setAutoTick( bool bEnable=true ); + void tick(); + virtual void onNewConnection( Client *pClient, int nPort )=0; virtual void onClosedConnection( Client *pClient )=0; @@ -90,6 +93,7 @@ namespace Bu SrvHash hServers; typedef Hash ClientHash; ClientHash hClients; + bool bAutoTick; }; } 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 @@ +#include "bu/server.h" +#include "bu/client.h" +#include "bu/protocol.h" + +class TickProtocol : public Bu::Protocol +{ +public: + TickProtocol() + { + } + + virtual ~TickProtocol() + { + } + + virtual void onTick( Bu::Client *pClient ) + { + printf("tick!\n"); + pClient->write("tick!\n"); + } +}; + +class TickServer : public Bu::Server +{ +public: + TickServer() + { + } + + virtual ~TickServer() + { + } + + virtual void onNewConnection( Bu::Client *pClient, int ) + { + pClient->setProtocol( new TickProtocol() ); + } + + virtual void onClosedConnection( Bu::Client *pClient ) + { + delete pClient->getProtocol(); + } +}; + +int main( int , char *[] ) +{ + TickServer ts; + + ts.setTimeout( 1, 0 ); + ts.setAutoTick(); + ts.addPort( 5555 ); + + for(;;) + { + ts.scan(); + sleep( 1 ); + } + + return 0; +} + -- cgit v1.2.3