From e0e7932a122614a0ff566fbfd8de5776de8b9f6d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 17 May 2007 05:56:26 +0000 Subject: Lots of cool new stuff, the Server class actually works for everything except actually interacting with clients, and the Client class is almost there, except that it doesn't really do anything yet. --- src/client.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/client.h (limited to 'src/client.h') diff --git a/src/client.h b/src/client.h new file mode 100644 index 0000000..27fbad4 --- /dev/null +++ b/src/client.h @@ -0,0 +1,23 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include "bu/socket.h" + +namespace Bu +{ + /** + * + */ + class Client + { + public: + Client(); + virtual ~Client(); + + private: + + }; +} + +#endif -- cgit v1.2.3 From 8b12972092777af56ae21f65b41f4c40d52c2367 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 18 Jun 2007 19:42:34 +0000 Subject: Added the protocol class. servers work, but don't send data, updated the streams to include many more state indicators and caps queries, and everything is working better in general. --- src/bzip2.cpp | 7 +++++- src/bzip2.h | 2 ++ src/client.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/client.h | 20 +++++++++++++--- src/file.cpp | 17 +++++++++++++- src/file.h | 6 ++++- src/filter.cpp | 19 ++++++++++++++-- src/filter.h | 6 ++++- src/protocol.cpp | 12 ++++++++++ src/protocol.h | 26 +++++++++++++++++++++ src/server.cpp | 11 ++++++--- src/server.h | 8 +++++-- src/socket.cpp | 48 ++++++++++++++++++++++++++++++++------- src/socket.h | 6 ++++- src/stream.h | 35 +++++++++++++++++++++++++++- 15 files changed, 266 insertions(+), 26 deletions(-) create mode 100644 src/protocol.cpp create mode 100644 src/protocol.h (limited to 'src/client.h') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index d3f237a..66786e4 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -128,7 +128,7 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) { if( bzState.avail_in > 0 ) { - if( rNext.canSeek() ) + if( rNext.isSeekable() ) { rNext.seek( -bzState.avail_in ); } @@ -185,3 +185,8 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes ) return sTotalOut; } +bool Bu::BZip2::isOpen() +{ + return (bzState.state != NULL); +} + diff --git a/src/bzip2.h b/src/bzip2.h index a23f07a..25f10c5 100644 --- a/src/bzip2.h +++ b/src/bzip2.h @@ -22,6 +22,8 @@ namespace Bu virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t write( const void *pBuf, size_t nBytes ); + virtual bool isOpen(); + private: void bzError( int code ); bz_stream bzState; diff --git a/src/client.cpp b/src/client.cpp index a33cdc3..cf96424 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,6 +1,16 @@ -#include "client.h" +#include "bu/client.h" +#include "bu/socket.h" +#include +#include +#include "bu/exceptions.h" +#include "bu/protocol.h" -Bu::Client::Client() +/** Read buffer size. */ +#define RBS (1024*2) + +Bu::Client::Client( Bu::Socket *pSocket ) : + pSocket( pSocket ), + pProto( NULL ) { } @@ -8,3 +18,58 @@ Bu::Client::~Client() { } +void Bu::Client::processInput() +{ + char buf[RBS]; + size_t nRead, nTotal=0; + + for(;;) + { + nRead = pSocket->read( buf, nRead ); + if( nRead < 0 ) + { + throw Bu::ConnectionException( + excodeReadError, + "Read error: %s", + strerror( errno ) + ); + } + else if( nRead == 0 ) + { + break; + } + else + { + nTotal += nRead; + sReadBuf.append( buf, nRead ); + if( !pSocket->canRead() ) + break; + } + } + + if( pProto && nTotal ) + { + pProto->onNewData( this ); + } +} + +void Bu::Client::setProtocol( Protocol *pProto ) +{ + this->pProto = pProto; +} + +Bu::Protocol *Bu::Client::getProtocol() +{ + return pProto; +} + +void Bu::Client::clearProtocol() +{ + pProto = NULL; +} + +Bu::FString &Bu::Client::getInput() +{ + return sReadBuf; +} + diff --git a/src/client.h b/src/client.h index 27fbad4..1a189e2 100644 --- a/src/client.h +++ b/src/client.h @@ -2,21 +2,35 @@ #define CLIENT_H #include -#include "bu/socket.h" + +#include "bu/fstring.h" namespace Bu { + class Protocol; + class Socket; + /** * */ class Client { public: - Client(); + Client( Bu::Socket *pSocket ); virtual ~Client(); - private: + void processInput(); + Bu::FString &getInput(); + + void setProtocol( Protocol *pProto ); + Bu::Protocol *getProtocol(); + void clearProtocol(); + + private: + Bu::Socket *pSocket; + Bu::Protocol *pProto; + Bu::FString sReadBuf; }; } diff --git a/src/file.cpp b/src/file.cpp index 2965afa..368b788 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -109,7 +109,17 @@ bool Bu::File::canWrite() return true; } -bool Bu::File::canSeek() +bool Bu::File::isReadable() +{ + return true; +} + +bool Bu::File::isWritable() +{ + return true; +} + +bool Bu::File::isSeekable() { return true; } @@ -139,3 +149,8 @@ void Bu::File::chmod( mode_t t ) fchmod( fileno( fh ), t ); } +bool Bu::File::isOpen() +{ + return (fh != NULL); +} + diff --git a/src/file.h b/src/file.h index 8107a1b..fe8dbda 100644 --- a/src/file.h +++ b/src/file.h @@ -25,12 +25,16 @@ namespace Bu virtual void setPos( long pos ); virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual bool isOpen(); virtual void flush(); virtual bool canRead(); virtual bool canWrite(); - virtual bool canSeek(); + + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); diff --git a/src/filter.cpp b/src/filter.cpp index 693fb9f..96a8694 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -50,6 +50,11 @@ bool Bu::Filter::isEOS() return rNext.isEOS(); } +bool Bu::Filter::isOpen() +{ + return rNext.isOpen(); +} + bool Bu::Filter::canRead() { return rNext.canRead(); @@ -60,9 +65,19 @@ bool Bu::Filter::canWrite() return rNext.canWrite(); } -bool Bu::Filter::canSeek() +bool Bu::Filter::isReadable() +{ + return rNext.isReadable(); +} + +bool Bu::Filter::isWritable() +{ + return rNext.isWritable(); +} + +bool Bu::Filter::isSeekable() { - return rNext.canSeek(); + return rNext.isSeekable(); } bool Bu::Filter::isBlocking() diff --git a/src/filter.h b/src/filter.h index 088d46e..7bb04bc 100644 --- a/src/filter.h +++ b/src/filter.h @@ -40,12 +40,16 @@ namespace Bu virtual void setPos( long pos ); virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual bool isOpen(); virtual void flush(); virtual bool canRead(); virtual bool canWrite(); - virtual bool canSeek(); + + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); diff --git a/src/protocol.cpp b/src/protocol.cpp new file mode 100644 index 0000000..0976b3b --- /dev/null +++ b/src/protocol.cpp @@ -0,0 +1,12 @@ +#include "bu/protocol.h" + +using namespace Bu; + +Bu::Protocol::Protocol() +{ +} + +Bu::Protocol::~Protocol() +{ +} + diff --git a/src/protocol.h b/src/protocol.h new file mode 100644 index 0000000..3accd99 --- /dev/null +++ b/src/protocol.h @@ -0,0 +1,26 @@ +#ifndef PROTOCOL_H +#define PROTOCOL_H + +#include + +namespace Bu +{ + class Client; + + /** + * + */ + class Protocol + { + public: + Protocol(); + virtual ~Protocol(); + + virtual void onNewData( Bu::Client *pClient )=0; + + private: + + }; +} + +#endif diff --git a/src/server.cpp b/src/server.cpp index abf4c5b..bceeb81 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1,5 +1,8 @@ -#include "server.h" +#include "bu/server.h" #include +#include "bu/serversocket.h" +#include "bu/client.h" +#include "bu/socket.h" Bu::Server::Server() : nTimeoutSec( 0 ), @@ -58,7 +61,7 @@ void Bu::Server::scan() } else { - + hClients.get( j )->processInput(); } } } @@ -68,7 +71,9 @@ void Bu::Server::addClient( int nSocket, int nPort ) { FD_SET( nSocket, &fdActive ); - Client *c = new Client(); + Client *c = new Client( + new Bu::Socket( nSocket ) + ); hClients.insert( nSocket, c ); onNewConnection( c, nPort ); diff --git a/src/server.h b/src/server.h index 942eb32..3331d2c 100644 --- a/src/server.h +++ b/src/server.h @@ -2,12 +2,16 @@ #define SERVER_H #include -#include "bu/serversocket.h" + +#include "bu/fstring.h" #include "bu/list.h" -#include "bu/client.h" namespace Bu { + class ServerSocket; + class Socket; + class Client; + /** * Core of a network server. This class is distinct from a ServerSocket in * that a ServerSocket is one listening socket, nothing more. Socket will diff --git a/src/socket.cpp b/src/socket.cpp index 1832898..bd05024 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -109,13 +109,6 @@ void Bu::Socket::close() ::close( nSocket ); } bActive = false; - //xInputBuf.clearData(); - //xOutputBuf.clearData(); - //if( pProtocol != NULL ) - //{ - // delete pProtocol; - // pProtocol = NULL; - //} } /* @@ -218,15 +211,49 @@ bool Bu::Socket::isEOS() bool Bu::Socket::canRead() { + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(nSocket, &rfds); + struct timeval tv = { 0, 0 }; + int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); + if( retval == -1 ) + throw ConnectionException( + excodeBadReadError, + "Bad Read error" + ); + if( !FD_ISSET( nSocket, &rfds ) ) + return false; return true; } bool Bu::Socket::canWrite() { + fd_set wfds; + FD_ZERO(&wfds); + FD_SET(nSocket, &wfds); + struct timeval tv = { 0, 0 }; + int retval = select( nSocket+1, NULL, &wfds, NULL, &tv ); + if( retval == -1 ) + throw ConnectionException( + excodeBadReadError, + "Bad Read error" + ); + if( !FD_ISSET( nSocket, &wfds ) ) + return false; return true; } -bool Bu::Socket::canSeek() +bool Bu::Socket::isReadable() +{ + return true; +} + +bool Bu::Socket::isWritable() +{ + return true; +} + +bool Bu::Socket::isSeekable() { return false; } @@ -244,3 +271,8 @@ void Bu::Socket::flush() { } +bool Bu::Socket::isOpen() +{ + return bActive; +} + diff --git a/src/socket.h b/src/socket.h index 30a43fb..c9dbd8d 100644 --- a/src/socket.h +++ b/src/socket.h @@ -28,12 +28,16 @@ namespace Bu virtual void setPos( long pos ); virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual bool isOpen(); virtual void flush(); virtual bool canRead(); virtual bool canWrite(); - virtual bool canSeek(); + + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); diff --git a/src/stream.h b/src/stream.h index a80586b..ba070d3 100644 --- a/src/stream.h +++ b/src/stream.h @@ -31,12 +31,45 @@ namespace Bu virtual void setPos( long pos ) = 0; virtual void setPosEnd( long pos ) = 0; virtual bool isEOS() = 0; + virtual bool isOpen() = 0; virtual void flush() = 0; + /** + * In non-blocking streams this indicates if a read operation will + * return data at the moment or not. In blocking streams this should + * return the same value as isEOS(). + */ virtual bool canRead() = 0; + + /** + * In non-blocking streams this indicates if a write operation will + * succeed or fail. In some cases writing is not allowed (e.g. + * internal buffers are full) temporarilly. In blocking streams this + * should return the same value as isWritable. + */ virtual bool canWrite() = 0; - virtual bool canSeek() = 0; + + /** + * Indicates if the stream is capable of read operations. This does not + * indicate if such operations will return useful data, see canRead for + * that. + */ + virtual bool isReadable() = 0; + + /** + * Indicates if the stream is capable of write operations. This does + * not indicate if such operations will succeed or fail, see canWrite + * for that. + */ + virtual bool isWritable() = 0; + + /** + * Indicates if the stream is capable of seek operations. This is + * generally false for non-blocking streams. Some buffered streams may + * support limited in-buffer seeking. + */ + virtual bool isSeekable() = 0; virtual bool isBlocking() = 0; virtual void setBlocking( bool bBlocking=true ) = 0; -- cgit v1.2.3 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/client.h') 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 From 932677862376e9642a496a15b7f09f1106f495a5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 20:49:39 +0000 Subject: Many minor changes, hopefully the header fixes will keep future header macro conflicts from happening. And, from now on, other projects should do -Ilibbu++ not -Ilibbu++/src so we can get ready for an installed version of libbu++. --- bu | 1 + src/archival.h | 4 ++-- src/archive.h | 4 ++-- src/atom.h | 4 ++-- src/bzip2.h | 4 ++-- src/client.h | 4 ++-- src/exceptionbase.h | 4 ++-- src/exceptions.h | 4 ++-- src/file.h | 4 ++-- src/filter.h | 4 ++-- src/fstring.h | 4 ++-- src/hash.h | 4 ++-- src/ito.h | 4 ++-- src/itoatom.h | 4 ++-- src/itocondition.h | 4 ++-- src/itomutex.h | 4 ++-- src/itoqueue.h | 4 ++-- src/linkmessage.h | 4 ++-- src/list.h | 4 ++-- src/membuf.h | 4 ++-- src/paramproc.h | 4 ++-- src/plugger.h | 4 ++-- src/programchain.h | 4 ++-- src/programlink.h | 4 ++-- src/protocol.h | 4 ++-- src/ringbuffer.h | 4 ++-- src/server.h | 4 ++-- src/serversocket.h | 4 ++-- src/singleton.h | 4 ++-- src/socket.h | 4 ++-- src/sptr.h | 4 ++-- src/stream.h | 4 ++-- src/unitsuite.h | 4 ++-- 33 files changed, 65 insertions(+), 64 deletions(-) create mode 120000 bu (limited to 'src/client.h') diff --git a/bu b/bu new file mode 120000 index 0000000..02b8847 --- /dev/null +++ b/bu @@ -0,0 +1 @@ +src/bu/ \ No newline at end of file diff --git a/src/archival.h b/src/archival.h index e2c803c..8be3308 100644 --- a/src/archival.h +++ b/src/archival.h @@ -1,5 +1,5 @@ -#ifndef ARCHIVAL_H -#define ARCHIVAL_H +#ifndef BU_ARCHIVAL_H +#define BU_ARCHIVAL_H namespace Bu { diff --git a/src/archive.h b/src/archive.h index 05c6f57..6c0c1df 100644 --- a/src/archive.h +++ b/src/archive.h @@ -1,5 +1,5 @@ -#ifndef ARCHIVE_H -#define ARCHIVE_H +#ifndef BU_ARCHIVE_H +#define BU_ARCHIVE_H #include #include diff --git a/src/atom.h b/src/atom.h index a0469b6..fad47eb 100644 --- a/src/atom.h +++ b/src/atom.h @@ -1,5 +1,5 @@ -#ifndef ATOM_H -#define ATOM_H +#ifndef BU_ATOM_H +#define BU_ATOM_H #include #include diff --git a/src/bzip2.h b/src/bzip2.h index 25f10c5..0a111e8 100644 --- a/src/bzip2.h +++ b/src/bzip2.h @@ -1,5 +1,5 @@ -#ifndef B_ZIP2_H -#define B_ZIP2_H +#ifndef BU_BZIP2_H +#define BU_BZIP2_H #include #include diff --git a/src/client.h b/src/client.h index 02ba077..cee558d 100644 --- a/src/client.h +++ b/src/client.h @@ -1,5 +1,5 @@ -#ifndef CLIENT_H -#define CLIENT_H +#ifndef BU_CLIENT_H +#define BU_CLIENT_H #include diff --git a/src/exceptionbase.h b/src/exceptionbase.h index fd78089..24e4bbf 100644 --- a/src/exceptionbase.h +++ b/src/exceptionbase.h @@ -1,5 +1,5 @@ -#ifndef EXCEPTION_BASE_H -#define EXCEPTION_BASE_H +#ifndef BU_EXCEPTION_BASE_H +#define BU_EXCEPTION_BASE_H #include #include diff --git a/src/exceptions.h b/src/exceptions.h index d5a9d39..b824b91 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -1,5 +1,5 @@ -#ifndef EXCEPTIONS_H -#define EXCEPTIONS_H +#ifndef BU_EXCEPTIONS_H +#define BU_EXCEPTIONS_H #include "exceptionbase.h" #include diff --git a/src/file.h b/src/file.h index 0f638a7..d4e43eb 100644 --- a/src/file.h +++ b/src/file.h @@ -1,5 +1,5 @@ -#ifndef FILE_H -#define FILE_H +#ifndef BU_FILE_H +#define BU_FILE_H #include diff --git a/src/filter.h b/src/filter.h index 7bb04bc..bd557b2 100644 --- a/src/filter.h +++ b/src/filter.h @@ -1,5 +1,5 @@ -#ifndef FILTER_H -#define FILTER_H +#ifndef BU_FILTER_H +#define BU_FILTER_H #include diff --git a/src/fstring.h b/src/fstring.h index 7180f5f..bf6518b 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -1,5 +1,5 @@ -#ifndef F_STRING_H -#define F_STRING_H +#ifndef BU_F_STRING_H +#define BU_F_STRING_H #include #include diff --git a/src/hash.h b/src/hash.h index 6c4a443..0fa3af7 100644 --- a/src/hash.h +++ b/src/hash.h @@ -1,5 +1,5 @@ -#ifndef HASH_H -#define HASH_H +#ifndef BU_HASH_H +#define BU_HASH_H #include #include diff --git a/src/ito.h b/src/ito.h index 01253f5..c062052 100644 --- a/src/ito.h +++ b/src/ito.h @@ -1,5 +1,5 @@ -#ifndef ITO_H -#define ITO_H +#ifndef BU_ITO_H +#define BU_ITO_H #include diff --git a/src/itoatom.h b/src/itoatom.h index 96090f2..7fc9090 100644 --- a/src/itoatom.h +++ b/src/itoatom.h @@ -1,5 +1,5 @@ -#ifndef ITO_QUEUE_H -#define ITO_QUEUE_H +#ifndef BU_ITO_QUEUE_H +#define BU_ITO_QUEUE_H #include diff --git a/src/itocondition.h b/src/itocondition.h index 4771b22..1793f81 100644 --- a/src/itocondition.h +++ b/src/itocondition.h @@ -1,5 +1,5 @@ -#ifndef ITO_CONDITION_H -#define ITO_CONDITION_H +#ifndef BU_ITO_CONDITION_H +#define BU_ITO_CONDITION_H #include diff --git a/src/itomutex.h b/src/itomutex.h index 80956b8..9c9d205 100644 --- a/src/itomutex.h +++ b/src/itomutex.h @@ -1,5 +1,5 @@ -#ifndef ITO_MUTEX_H -#define ITO_MUTEX_H +#ifndef BU_ITO_MUTEX_H +#define BU_ITO_MUTEX_H #include diff --git a/src/itoqueue.h b/src/itoqueue.h index 322698d..75a2f27 100644 --- a/src/itoqueue.h +++ b/src/itoqueue.h @@ -1,5 +1,5 @@ -#ifndef ITO_QUEUE_H -#define ITO_QUEUE_H +#ifndef BU_ITO_QUEUE_H +#define BU_ITO_QUEUE_H #include diff --git a/src/linkmessage.h b/src/linkmessage.h index 64b8bc2..baa22a6 100644 --- a/src/linkmessage.h +++ b/src/linkmessage.h @@ -1,8 +1,8 @@ /**\file linkmessage.h */ -#ifndef LINKMESSAGE_H -#define LINKMESSAGE_H +#ifndef BU_LINKMESSAGE_H +#define BU_LINKMESSAGE_H namespace Bu { diff --git a/src/list.h b/src/list.h index 314459e..6235619 100644 --- a/src/list.h +++ b/src/list.h @@ -1,5 +1,5 @@ -#ifndef LIST_H -#define LIST_H +#ifndef BU_LIST_H +#define BU_LIST_H #include #include "bu/exceptionbase.h" diff --git a/src/membuf.h b/src/membuf.h index 877b35e..b82f943 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -1,5 +1,5 @@ -#ifndef MEM_BUF_H -#define MEM_BUF_H +#ifndef BU_MEM_BUF_H +#define BU_MEM_BUF_H #include diff --git a/src/paramproc.h b/src/paramproc.h index beee4a0..2bca588 100644 --- a/src/paramproc.h +++ b/src/paramproc.h @@ -1,5 +1,5 @@ -#ifndef PARAM_PROC_H -#define PARAM_PROC_H +#ifndef BU_PARAM_PROC_H +#define BU_PARAM_PROC_H #include #include diff --git a/src/plugger.h b/src/plugger.h index 4d2e7af..79a3271 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -1,5 +1,5 @@ -#ifndef PLUGGER_H -#define PLUGGER_H +#ifndef BU_PLUGGER_H +#define BU_PLUGGER_H #include "bu/hash.h" diff --git a/src/programchain.h b/src/programchain.h index e73d659..336a9f1 100644 --- a/src/programchain.h +++ b/src/programchain.h @@ -1,5 +1,5 @@ -#ifndef PROGRAMCHAIN_H -#define PROGRAMCHAIN_H +#ifndef BU_PROGRAMCHAIN_H +#define BU_PROGRAMCHAIN_H #include "bu/list.h" #include "bu/linkmessage.h" diff --git a/src/programlink.h b/src/programlink.h index 7c31a18..07d7489 100644 --- a/src/programlink.h +++ b/src/programlink.h @@ -1,5 +1,5 @@ -#ifndef PROGRAMLINK_H -#define PROGRAMLINK_H +#ifndef BU_PROGRAMLINK_H +#define BU_PROGRAMLINK_H #include "bu/linkmessage.h" #include "bu/programchain.h" diff --git a/src/protocol.h b/src/protocol.h index 3accd99..c7a9275 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -1,5 +1,5 @@ -#ifndef PROTOCOL_H -#define PROTOCOL_H +#ifndef BU_PROTOCOL_H +#define BU_PROTOCOL_H #include diff --git a/src/ringbuffer.h b/src/ringbuffer.h index 625f65b..be80e2f 100644 --- a/src/ringbuffer.h +++ b/src/ringbuffer.h @@ -1,5 +1,5 @@ -#ifndef RING_BUFFER_H -#define RING_BUFFER_H +#ifndef BU_RING_BUFFER_H +#define BU_RING_BUFFER_H #include #include "bu/exceptionbase.h" diff --git a/src/server.h b/src/server.h index 07eef95..302b6e3 100644 --- a/src/server.h +++ b/src/server.h @@ -1,5 +1,5 @@ -#ifndef SERVER_H -#define SERVER_H +#ifndef BU_SERVER_H +#define BU_SERVER_H #include diff --git a/src/serversocket.h b/src/serversocket.h index cb86078..b4ee247 100644 --- a/src/serversocket.h +++ b/src/serversocket.h @@ -1,5 +1,5 @@ -#ifndef SERVER_SOCKET_H -#define SERVER_SOCKET_H +#ifndef BU_SERVER_SOCKET_H +#define BU_SERVER_SOCKET_H #include #include "bu/fstring.h" diff --git a/src/singleton.h b/src/singleton.h index 4976a61..c43d71b 100644 --- a/src/singleton.h +++ b/src/singleton.h @@ -1,5 +1,5 @@ -#ifndef SINGLETON_H -#define SINGLETON_H +#ifndef BU_SINGLETON_H +#define BU_SINGLETON_H #include diff --git a/src/socket.h b/src/socket.h index c9dbd8d..0ccee3b 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,5 +1,5 @@ -#ifndef SOCKET_H -#define SOCKET_H +#ifndef BU_SOCKET_H +#define BU_SOCKET_H #include diff --git a/src/sptr.h b/src/sptr.h index 4baa697..75851a6 100644 --- a/src/sptr.h +++ b/src/sptr.h @@ -1,5 +1,5 @@ -#ifndef SPTR_H -#define SPTR_H +#ifndef BU_SPTR_H +#define BU_SPTR_H #include #include diff --git a/src/stream.h b/src/stream.h index 056de0c..1e236a6 100644 --- a/src/stream.h +++ b/src/stream.h @@ -1,5 +1,5 @@ -#ifndef STREAM_H -#define STREAM_H +#ifndef BU_STREAM_H +#define BU_STREAM_H #include #include diff --git a/src/unitsuite.h b/src/unitsuite.h index 6e9270a..578b4cc 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h @@ -1,5 +1,5 @@ -#ifndef UNIT_SUITE_H -#define UNIT_SUITE_H +#ifndef BU_UNIT_SUITE_H +#define BU_UNIT_SUITE_H #include #include -- cgit v1.2.3 From afb50f535dd60b485a38f1f1f692b3303e28fecc Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 21:58:47 +0000 Subject: The FString has more things that it can do...now. --- src/client.h | 1 + src/fstring.cpp | 6 ++++++ src/fstring.h | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) (limited to 'src/client.h') diff --git a/src/client.h b/src/client.h index cee558d..f228e96 100644 --- a/src/client.h +++ b/src/client.h @@ -25,6 +25,7 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); void write( const char *pData, int nBytes ); + void read( const char *pData, int nBytes ); void setProtocol( Protocol *pProto ); Bu::Protocol *getProtocol(); diff --git a/src/fstring.cpp b/src/fstring.cpp index 56d2173..0b5a970 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -12,3 +12,9 @@ template<> bool Bu::__cmpHashKeys( return a == b; } +std::ostream& operator<< (std::ostream &os, Bu::FString &val ) +{ + os.write( val.getStr(), val.getSize() ); + return os; +} + diff --git a/src/fstring.h b/src/fstring.h index bf6518b..1f21b5f 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -586,6 +586,20 @@ namespace Bu return -1; } + /** + * Remove nAmnt bytes from the front of the string. This function + * operates in O(n) time and should be used sparingly. + */ + void trimFront( long nAmnt ) + { + long nNewLen = nLength - nAmnt; + flatten(); + Chunk *pNew = newChunk( nNewLen ); + cpy( pNew->pData, pFirst->pData, nNewLen ); + clear(); + appendChunk( pNew ); + } + /** * Function the archiver calls to archive your FString. *@param ar (Archive) The archive which is archiving your FString. @@ -882,4 +896,7 @@ namespace Bu template<> bool __cmpHashKeys( const FString &a, const FString &b ); } +#include +std::ostream& operator<< (std::ostream &os, Bu::FString &val ); + #endif -- cgit v1.2.3 From f896b0e207e0b656109ef0e9f721f27ce53a836e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 22:47:03 +0000 Subject: Client code is better, so is the socket, you can get addresses and other cool things from it. The plugger had yet another bugfix...plugger... --- src/client.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/client.h | 10 +++++++++- src/plugger.h | 6 +++--- src/socket.cpp | 12 ++++++++++++ src/socket.h | 2 ++ 5 files changed, 79 insertions(+), 5 deletions(-) (limited to 'src/client.h') diff --git a/src/client.cpp b/src/client.cpp index 0e48285..63822ba 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -10,7 +10,8 @@ Bu::Client::Client( Bu::Socket *pSocket ) : pSocket( pSocket ), - pProto( NULL ) + pProto( NULL ), + nRBOffset( 0 ) { } @@ -103,3 +104,54 @@ void Bu::Client::write( const char *pData, int nBytes ) sWriteBuf.append( pData, nBytes ); } +void Bu::Client::write( int8_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int16_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int32_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int64_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::read( char *pData, int nBytes ) +{ + memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); + nRBOffset += nBytes; + if( sReadBuf.getSize()-nRBOffset == 0 ) + { + sReadBuf.clear(); + nRBOffset = 0; + } + // This is an experimental threshold, maybe I'll make this configurable + // later on. + else if( + (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || + (nRBOffset >= sReadBuf.getSize()/4) + ) + { + sReadBuf.trimFront( nRBOffset ); + nRBOffset = 0; + } +} + +long Bu::Client::getInputSize() +{ + return sReadBuf.getSize()-nRBOffset; +} + +const Bu::Socket *Bu::Client::getSocket() const +{ + return pSocket; +} + diff --git a/src/client.h b/src/client.h index f228e96..b400bb3 100644 --- a/src/client.h +++ b/src/client.h @@ -25,7 +25,12 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); void write( const char *pData, int nBytes ); - void read( const char *pData, int nBytes ); + void write( int8_t nData ); + void write( int16_t nData ); + void write( int32_t nData ); + void write( int64_t nData ); + void read( char *pData, int nBytes ); + long getInputSize(); void setProtocol( Protocol *pProto ); Bu::Protocol *getProtocol(); @@ -33,10 +38,13 @@ namespace Bu bool isOpen(); + const Bu::Socket *getSocket() const; + private: Bu::Socket *pSocket; Bu::Protocol *pProto; Bu::FString sReadBuf; + int nRBOffset; Bu::FString sWriteBuf; }; } diff --git a/src/plugger.h b/src/plugger.h index 79a3271..615a662 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -37,7 +37,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo classname = { \ + Bu::PluginInfo classname = { \ #classname, name, ver, rev, \ create ##classname, destroy ##classname }; \ } @@ -52,7 +52,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo pluginname = { \ + Bu::PluginInfo pluginname = { \ #pluginname, name, ver, rev, \ (void *(*)())(create ##classname), \ (void (*)( void * ))(destroy ##classname) }; \ @@ -68,7 +68,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo structname = { \ + Bu::PluginInfo structname = { \ #pluginname, name, ver, rev, \ (void *(*)())(create ##classname), \ (void (*)( void * ))(destroy ##classname) }; \ diff --git a/src/socket.cpp b/src/socket.cpp index bd05024..5a3097c 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -276,3 +276,15 @@ bool Bu::Socket::isOpen() return bActive; } +Bu::FString Bu::Socket::getAddress() const +{ + struct sockaddr_in addr; + socklen_t len = sizeof(addr); + addr.sin_family = AF_INET; + getsockname( nSocket, (sockaddr *)(&addr), &len ); + char buf[150]; + sprintf( buf, "%s", inet_ntoa( addr.sin_addr ) ); + + return buf; +} + diff --git a/src/socket.h b/src/socket.h index 0ccee3b..9e36041 100644 --- a/src/socket.h +++ b/src/socket.h @@ -42,6 +42,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + Bu::FString getAddress() const; + private: int nSocket; bool bActive; -- cgit v1.2.3 From 76ea96f91115585f7c6b49d11ba60ec15bee12e6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 22:53:10 +0000 Subject: Updated the client, it now takes all intXX_t and uintXX_t as a param for write as a convinience. --- src/client.cpp | 20 ++++++++++++++++++++ src/client.h | 4 ++++ 2 files changed, 24 insertions(+) (limited to 'src/client.h') diff --git a/src/client.cpp b/src/client.cpp index 63822ba..2f293b7 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -124,6 +124,26 @@ void Bu::Client::write( int64_t nData ) sWriteBuf.append( (const char *)&nData, sizeof(nData) ); } +void Bu::Client::write( uint8_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( uint16_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( uint32_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( uint64_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + void Bu::Client::read( char *pData, int nBytes ) { memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); diff --git a/src/client.h b/src/client.h index b400bb3..1253dcd 100644 --- a/src/client.h +++ b/src/client.h @@ -29,6 +29,10 @@ namespace Bu void write( int16_t nData ); void write( int32_t nData ); void write( int64_t nData ); + void write( uint8_t nData ); + void write( uint16_t nData ); + void write( uint32_t nData ); + void write( uint64_t nData ); void read( char *pData, int nBytes ); long getInputSize(); -- cgit v1.2.3 From ec8ed8b4b44c7b039e87faaa50bb4d503393d336 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 29 Jun 2007 00:48:32 +0000 Subject: A few changes here and there, mainly related to getting the new Server system working in optimal condition... --- src/client.cpp | 35 ++++++++++++++++++++++++++++++++--- src/client.h | 11 +++++++++-- src/fstring.cpp | 2 +- src/fstring.h | 2 +- src/membuf.cpp | 11 +++++++++++ src/membuf.h | 3 +++ 6 files changed, 57 insertions(+), 7 deletions(-) (limited to 'src/client.h') diff --git a/src/client.cpp b/src/client.cpp index 2f293b7..8077b3d 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -99,9 +99,9 @@ bool Bu::Client::isOpen() return pSocket->isOpen(); } -void Bu::Client::write( const char *pData, int nBytes ) +void Bu::Client::write( const void *pData, int nBytes ) { - sWriteBuf.append( pData, nBytes ); + sWriteBuf.append( (const char *)pData, nBytes ); } void Bu::Client::write( int8_t nData ) @@ -144,7 +144,7 @@ void Bu::Client::write( uint64_t nData ) sWriteBuf.append( (const char *)&nData, sizeof(nData) ); } -void Bu::Client::read( char *pData, int nBytes ) +void Bu::Client::read( void *pData, int nBytes ) { memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); nRBOffset += nBytes; @@ -165,6 +165,31 @@ void Bu::Client::read( char *pData, int nBytes ) } } +void Bu::Client::peek( void *pData, int nBytes ) +{ + memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); +} + +void Bu::Client::seek( int nBytes ) +{ + nRBOffset += nBytes; + if( sReadBuf.getSize()-nRBOffset == 0 ) + { + sReadBuf.clear(); + nRBOffset = 0; + } + // This is an experimental threshold, maybe I'll make this configurable + // later on. + else if( + (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || + (nRBOffset >= sReadBuf.getSize()/4) + ) + { + sReadBuf.trimFront( nRBOffset ); + nRBOffset = 0; + } +} + long Bu::Client::getInputSize() { return sReadBuf.getSize()-nRBOffset; @@ -175,3 +200,7 @@ const Bu::Socket *Bu::Client::getSocket() const return pSocket; } +void Bu::Client::disconnect() +{ +} + diff --git a/src/client.h b/src/client.h index 1253dcd..5947521 100644 --- a/src/client.h +++ b/src/client.h @@ -24,7 +24,7 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); - void write( const char *pData, int nBytes ); + void write( const void *pData, int nBytes ); void write( int8_t nData ); void write( int16_t nData ); void write( int32_t nData ); @@ -33,7 +33,9 @@ namespace Bu void write( uint16_t nData ); void write( uint32_t nData ); void write( uint64_t nData ); - void read( char *pData, int nBytes ); + void read( void *pData, int nBytes ); + void peek( void *pData, int nBytes ); + void seek( int nBytes ); long getInputSize(); void setProtocol( Protocol *pProto ); @@ -44,6 +46,11 @@ namespace Bu const Bu::Socket *getSocket() const; + /** + *@todo Make this not suck. + */ + void disconnect(); + private: Bu::Socket *pSocket; Bu::Protocol *pProto; diff --git a/src/fstring.cpp b/src/fstring.cpp index 0b5a970..f71d6c1 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -12,7 +12,7 @@ template<> bool Bu::__cmpHashKeys( return a == b; } -std::ostream& operator<< (std::ostream &os, Bu::FString &val ) +std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FString &val ) { os.write( val.getStr(), val.getSize() ); return os; diff --git a/src/fstring.h b/src/fstring.h index 1f21b5f..f06c362 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -897,6 +897,6 @@ namespace Bu } #include -std::ostream& operator<< (std::ostream &os, Bu::FString &val ); +std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FString &val ); #endif diff --git a/src/membuf.cpp b/src/membuf.cpp index 3c394b0..45ff5bd 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -7,6 +7,12 @@ Bu::MemBuf::MemBuf() : { } +Bu::MemBuf::MemBuf( const Bu::FString &str ) : + sBuf( str ), + nPos( 0 ) +{ +} + Bu::MemBuf::~MemBuf() { } @@ -107,3 +113,8 @@ void Bu::MemBuf::setBlocking( bool bBlocking ) { } +Bu::FString &Bu::MemBuf::getString() +{ + return sBuf; +} + diff --git a/src/membuf.h b/src/membuf.h index b82f943..8f53d4b 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -15,6 +15,7 @@ namespace Bu { public: MemBuf(); + MemBuf( const Bu::FString &str ); virtual ~MemBuf(); virtual void close(); @@ -41,6 +42,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + Bu::FString &getString(); + private: Bu::FString sBuf; long nPos; -- cgit v1.2.3