From 01ecf54b07e75c17ca5f7039084daeefaea9a1c7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 26 Jun 2007 19:22:02 +0000 Subject: Fixed a bug in the plugger and added the skeleton of the MemBuf class. --- src/membuf.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/membuf.cpp (limited to 'src/membuf.cpp') diff --git a/src/membuf.cpp b/src/membuf.cpp new file mode 100644 index 0000000..fdb4366 --- /dev/null +++ b/src/membuf.cpp @@ -0,0 +1,81 @@ +#include "bu/membuf.h" + +using namespace Bu; + +Bu::MemBuf::MemBuf() +{ +} + +Bu::MemBuf::~MemBuf() +{ +} + +void Bu::MemBuf::close() +{ +} + +size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) +{ + +} + +size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) +{ +} + +long Bu::MemBuf::tell() +{ +} + +void Bu::MemBuf::seek( long offset ) +{ +} + +void Bu::MemBuf::setPos( long pos ) +{ +} + +void Bu::MemBuf::setPosEnd( long pos ) +{ +} + +bool Bu::MemBuf::isEOS() +{ +} + +bool Bu::MemBuf::isOpen() +{ +} + +void Bu::MemBuf::flush() +{ +} + +bool Bu::MemBuf::canRead() +{ +} + +bool Bu::MemBuf::canWrite() +{ +} + +bool Bu::MemBuf::isReadable() +{ +} + +bool Bu::MemBuf::isWritable() +{ +} + +bool Bu::MemBuf::isSeekable() +{ +} + +bool Bu::MemBuf::isBlocking() +{ +} + +void Bu::MemBuf::setBlocking( bool bBlocking ) +{ +} + -- cgit v1.2.3 From 5ec9a131e12d021c42b46b601f5e79502485bebb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 27 Jun 2007 15:42:50 +0000 Subject: The MemBuf works just fine, although it still can't over-write data in the buffer. --- build.conf | 9 --------- src/membuf.cpp | 30 +++++++++++++++++++++++++++++- src/membuf.h | 7 +++++++ src/unit/entities/unit | 30 ++++++++++++++++++++++++++++++ src/unit/file.cpp | 10 ++++++++-- src/unit/hash.cpp | 4 ++-- src/unit/membuf.cpp | 37 +++++++++++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 src/unit/entities/unit create mode 100644 src/unit/membuf.cpp (limited to 'src/membuf.cpp') diff --git a/build.conf b/build.conf index f493d67..d994977 100644 --- a/build.conf +++ b/build.conf @@ -41,15 +41,6 @@ filesIn("src/tests") filter regexp("^src/tests/(.*)\\.cpp$", "tests/{re:1}"): ["tests/itoqueue1", "tests/itoqueue2"]: set "LDFLAGS" += "-lpthread" -directoriesIn("src/unit","unit/"): - rule "exe", - target file, - group "tests", - requires "libbu++.a", - set "CXXFLAGS" += "-Isrc", - set "LDFLAGS" += "-L. -lbu++", - input filesIn("{fulldir}") filter regexp("^.*\\.cpp$") - filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): rule "exe", target file, diff --git a/src/membuf.cpp b/src/membuf.cpp index fdb4366..3c394b0 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -2,7 +2,8 @@ using namespace Bu; -Bu::MemBuf::MemBuf() +Bu::MemBuf::MemBuf() : + nPos( 0 ) { } @@ -16,35 +17,56 @@ void Bu::MemBuf::close() size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) { + if( (size_t)sBuf.getSize()-(size_t)nPos < nBytes ) + nBytes = sBuf.getSize()-nPos; + memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); + nPos += nBytes; + + return nBytes; } size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) { + sBuf.append( (const char *)pBuf, nBytes ); + nPos += nBytes; + return nBytes; } long Bu::MemBuf::tell() { + return nPos; } void Bu::MemBuf::seek( long offset ) { + nPos += offset; + if( nPos < 0 ) nPos = 0; + else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); } void Bu::MemBuf::setPos( long pos ) { + nPos = pos; + if( nPos < 0 ) nPos = 0; + else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); } void Bu::MemBuf::setPosEnd( long pos ) { + nPos = sBuf.getSize()-pos; + if( nPos < 0 ) nPos = 0; + else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); } bool Bu::MemBuf::isEOS() { + return (nPos == sBuf.getSize()); } bool Bu::MemBuf::isOpen() { + return true; } void Bu::MemBuf::flush() @@ -53,26 +75,32 @@ void Bu::MemBuf::flush() bool Bu::MemBuf::canRead() { + return !isEOS(); } bool Bu::MemBuf::canWrite() { + return isEOS(); } bool Bu::MemBuf::isReadable() { + return true; } bool Bu::MemBuf::isWritable() { + return true; } bool Bu::MemBuf::isSeekable() { + return true; } bool Bu::MemBuf::isBlocking() { + return true; } void Bu::MemBuf::setBlocking( bool bBlocking ) diff --git a/src/membuf.h b/src/membuf.h index 2cbbbdc..877b35e 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -4,6 +4,7 @@ #include #include "bu/stream.h" +#include "bu/fstring.h" namespace Bu { @@ -18,6 +19,12 @@ namespace Bu virtual void close(); virtual size_t read( void *pBuf, size_t nBytes ); + + /** + *@todo Allow writes at the current position, not just appending to + * the current buffer. This is a silly way to do it, but it covers all + * of our bases for now. + */ virtual size_t write( const void *pBuf, size_t nBytes ); virtual long tell(); virtual void seek( long offset ); diff --git a/src/unit/entities/unit b/src/unit/entities/unit new file mode 100644 index 0000000..28db45f --- /dev/null +++ b/src/unit/entities/unit @@ -0,0 +1,30 @@ + + + + #include "bu/unitsuite.h" + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("{=name}"); + addTest( Unit::test01 ); + } + + virtual ~Unit() + { + } + + void test01() + { + unitTest( 0 == 5 ); + } +}; + +int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } + + diff --git a/src/unit/file.cpp b/src/unit/file.cpp index a45b8d7..1eaaf36 100644 --- a/src/unit/file.cpp +++ b/src/unit/file.cpp @@ -87,8 +87,14 @@ public: unitTest( sf.isEOS() == false ); try { - sf.read( buf, 5 ); - unitFailed("No exception thrown"); + if( sf.read( buf, 5 ) > 0 ) + { + unitFailed("Non-zero read result"); + } + else + { + sf.close(); + } } catch( Bu::FileException &e ) { diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp index 588e687..9ea933f 100644 --- a/src/unit/hash.cpp +++ b/src/unit/hash.cpp @@ -1,6 +1,6 @@ #include "bu/fstring.h" #include "bu/hash.h" -#include "unitsuite.h" +#include "bu/unitsuite.h" #include @@ -23,7 +23,7 @@ public: { StrIntHash h; char buf[20]; - for(int i=0;i<10000;i++) + for(int i=1;i<10000;i++) { sprintf(buf,"%d",i); Bu::FString sTmp(buf); diff --git a/src/unit/membuf.cpp b/src/unit/membuf.cpp new file mode 100644 index 0000000..65ba82a --- /dev/null +++ b/src/unit/membuf.cpp @@ -0,0 +1,37 @@ +#include "bu/unitsuite.h" +#include "bu/membuf.h" + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("MemBuf"); + addTest( Unit::testWriteRead01 ); + } + + virtual ~Unit() + { + } + + void testWriteRead01() + { + Bu::MemBuf mb; + unitTest( mb.write("ab", 2 ) == 2 ); + unitTest( mb.write("cde", 3 ) == 3 ); + unitTest( mb.write("FG", 2 ) == 2 ); + + mb.setPos( 0 ); + + char buf[8]; + buf[7] = '\0'; + unitTest( mb.read( buf, 7 ) == 7 ); + unitTest( !strncmp( buf, "abcdeFG", 7 ) ); + unitTest( mb.read( buf, 7 ) == 0 ); + mb.seek( -3 ); + unitTest( mb.read( buf, 7 ) == 3 ); + unitTest( !strncmp( buf, "eFG", 3 ) ); + } +}; + +int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } -- 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/membuf.cpp') 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