From 89df61f21e525c7a36846ce1a960ffba5501cdca Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 30 Jun 2009 04:28:47 +0000 Subject: Bu::Client now gives you the option to add additional filters to the filter chain on the base stream, which for the moment is a socket. I also demonstrate this in the new rot13 test, with a rot13 filter, and a simple echo protocol. --- src/tests/rot13.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/tests/rot13.cpp (limited to 'src/tests/rot13.cpp') diff --git a/src/tests/rot13.cpp b/src/tests/rot13.cpp new file mode 100644 index 0000000..e677440 --- /dev/null +++ b/src/tests/rot13.cpp @@ -0,0 +1,80 @@ +#include "bu/protocol.h" +#include "bu/multiserver.h" +#include "bu/client.h" +#include "bu/filter.h" + +using namespace Bu; + +class Rot13Filter : public Filter +{ +public: + Rot13Filter( Stream &next ) : + Filter( next ) + { + } + + virtual ~Rot13Filter() + { + } + + virtual void start() + { + } + + virtual size_t stop() + { + return 0; + } + + virtual size_t read( void *pBuf, size_t nBytes ) + { + return rNext.read( pBuf, nBytes ); + } + + virtual size_t write( const void *pBuf, size_t nBytes ) + { + const char *cBuf = (const char *)pBuf; + char *buf = new char[nBytes]; + for( size_t j = 0; j < nBytes; j++ ) + { + if( cBuf[j] >= 'a' && cBuf[j] <= 'z' ) + buf[j] = (cBuf[j]-'a'+13)%26+'a'; + else if( cBuf[j] >= 'A' && cBuf[j] <= 'Z' ) + buf[j] = (cBuf[j]-'A'+13)%26+'A'; + else + buf[j] = cBuf[j]; + } + rNext.write( buf, nBytes ); + delete[] buf; + return nBytes; + } +}; + +class Rot13Protocol : public Protocol +{ +public: + void onNewConnection( Bu::Client *pClient ) + { + pClient->pushFilter(); + } + + void onNewData( Bu::Client *pClient ) + { + pClient->write( pClient->getInput().getStr(), pClient->getInputSize() ); + pClient->seek( pClient->getInputSize() ); + } +}; + +int main() +{ + MultiServer xSrv; + + xSrv.setTimeout( 5 ); + xSrv.addProtocol( genProtocol, 9999 ); + + for(;;) + xSrv.scan(); + + return 0; +} + -- cgit v1.2.3