From 9e8a4944e50fab432012878c66e1bdac20649f76 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 7 Jun 2007 19:56:20 +0000 Subject: The Stream Filter archetecture is finished, it's actually much cooler than I had anticipated, and much cleaner. I'll have to add some documentation to it, because it's not really obvious how any of it fits together from the outset, although I have to say that the bzip2 test program is the easiest general bzip2 compression program I've ever made...it just goes :) Decompression in Bu::BZip2 isn't finished yet, but that's ok, it's coming soon. --- src/bzip2.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/bzip2.cpp (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp new file mode 100644 index 0000000..b8c0f74 --- /dev/null +++ b/src/bzip2.cpp @@ -0,0 +1,123 @@ +#include "bu/bzip2.h" +#include "bu/exceptions.h" + +using namespace Bu; + +Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : + Bu::Filter( rNext ), + nCompression( nCompression ) +{ + start(); +} + +Bu::BZip2::~BZip2() +{ + printf("-> Bu::BZip2::~BZip2()\n"); + stop(); +} + +void Bu::BZip2::start() +{ + printf("-> Bu::BZip2::start()\n"); + printf("Hey, it's starting...\n"); + bzState.state = NULL; + bzState.bzalloc = NULL; + bzState.bzfree = NULL; + bzState.opaque = NULL; + + nBufSize = 50000; + pBuf = new char[nBufSize]; +} + +void Bu::BZip2::stop() +{ + printf("-> Bu::BZip2::stop()\n"); + if( bzState.state ) + { + if( bReading ) + { + } + else + { + for(;;) + { + bzState.next_in = NULL; + bzState.avail_in = 0; + bzState.avail_out = nBufSize; + bzState.next_out = pBuf; + int res = BZ2_bzCompress( &bzState, BZ_FINISH ); + if( bzState.avail_out < nBufSize ) + { + rNext.write( pBuf, nBufSize-bzState.avail_out ); + } + if( res == BZ_STREAM_END ) + break; + } + BZ2_bzCompressEnd( &bzState ); + } + } +} + +void Bu::BZip2::bzError( int code ) +{ + switch( code ) + { + case BZ_OK: + return; + + case BZ_CONFIG_ERROR: + throw ExceptionBase("The bzip2 library has been miscompiled."); + + case BZ_PARAM_ERROR: + throw ExceptionBase("bzip2 parameter error."); + + case BZ_MEM_ERROR: + throw ExceptionBase("Not enough memory available for bzip2."); + } +} + +size_t Bu::BZip2::read( void *pData, size_t nBytes ) +{ + if( !bzState.state ) + { + bReading = true; + } + if( bReading == false ) + throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); + //bzState.next_in = pData; + //bzState.avail_in = nSizeIn; + + //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 ); + return 0; +} + +size_t Bu::BZip2::write( const void *pData, size_t nBytes ) +{ + if( !bzState.state ) + { + bReading = false; + BZ2_bzCompressInit( &bzState, nCompression, 0, 30 ); + } + if( bReading == true ) + throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); + + bzState.next_in = (char *)pData; + bzState.avail_in = nBytes; + for(;;) + { + bzState.avail_out = nBufSize; + bzState.next_out = pBuf; + + BZ2_bzCompress( &bzState, BZ_RUN ); + + if( bzState.avail_out < nBufSize ) + { + rNext.write( pBuf, nBufSize-bzState.avail_out ); + } + if( bzState.avail_in == 0 ) + break; + } + + return 0; +} + -- cgit v1.2.3 From f5352edf3dc23c044a91f1d1537fa0dc0f0babc7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 9 Jun 2007 05:43:04 +0000 Subject: Alright, looks like the bzip2 filter can decompress just fine. It won't try to compensate for overshooting the end of the compression block yet, which it won't be able to do on streams that don't support seeking...I think I'll make it only try on stop commands, and try to re-use the buffer otherwise...maybe...it's an interesting problem since it *always* overshoots (unless you're really, really lucky...) --- src/bzip2.cpp | 37 ++++++++++++++++++++++++++++++------- src/tests/bzip2.cpp | 10 +++++----- 2 files changed, 35 insertions(+), 12 deletions(-) (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index b8c0f74..433fc91 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -12,14 +12,11 @@ Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : Bu::BZip2::~BZip2() { - printf("-> Bu::BZip2::~BZip2()\n"); stop(); } void Bu::BZip2::start() { - printf("-> Bu::BZip2::start()\n"); - printf("Hey, it's starting...\n"); bzState.state = NULL; bzState.bzalloc = NULL; bzState.bzfree = NULL; @@ -31,11 +28,11 @@ void Bu::BZip2::start() void Bu::BZip2::stop() { - printf("-> Bu::BZip2::stop()\n"); if( bzState.state ) { if( bReading ) { + BZ2_bzDecompressEnd( &bzState ); } else { @@ -81,13 +78,39 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) if( !bzState.state ) { bReading = true; + BZ2_bzDecompressInit( &bzState, 0, 0 ); + bzState.next_in = pBuf; + bzState.avail_in = 0; } if( bReading == false ) throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); - //bzState.next_in = pData; - //bzState.avail_in = nSizeIn; + + int nRead = 0; + int nReadTotal = bzState.total_out_lo32; + for(;;) + { + bzState.next_out = (char *)pData; + bzState.avail_out = nBytes; + int ret = BZ2_bzDecompress( &bzState ); - //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 ); + nReadTotal += nRead-bzState.avail_out; + + if( ret == BZ_STREAM_END ) + { + return nBytes-bzState.avail_out; + } + + if( bzState.avail_out ) + { + nRead = rNext.read( pBuf, nBufSize ); + bzState.next_in = pBuf; + bzState.avail_in = nRead; + } + else + { + return nBytes-bzState.avail_out; + } + } return 0; } diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp index 683d3d7..ef9328f 100644 --- a/src/tests/bzip2.cpp +++ b/src/tests/bzip2.cpp @@ -6,17 +6,17 @@ int main( int argc, char *argv[] ) char buf[1024]; size_t nRead; - Bu::File f( "test.bz2", "wb" ); + Bu::File f( "test.bz2", "rb" ); Bu::BZip2 bz2( f ); - Bu::File fin( argv[1], "rb"); + Bu::File fin( argv[1], "wb"); for(;;) { - nRead = fin.read( buf, 1024 ); + nRead = bz2.read( buf, 1024 ); if( nRead > 0 ) - bz2.write( buf, nRead ); - if( fin.isEOS() ) + fin.write( buf, nRead ); + if( bz2.isEOS() ) break; } } -- cgit v1.2.3 From 5f39066a4f561e9a94a6cc9293ab9b978ebf1f81 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 10 Jun 2007 21:28:14 +0000 Subject: Bunch of maintenence type things. Minor tweaks and the like. The file class has a lot more helper functions and the like, the filters give more info back to the caller, minor updates to taf. --- src/bzip2.cpp | 41 ++++++++++++++++++++++++++++++++++------- src/bzip2.h | 2 +- src/file.cpp | 31 +++++++++++++++++++++++++++++++ src/file.h | 17 ++++++++++++++++- src/filter.cpp | 7 ++++++- src/filter.h | 4 +++- src/socket.cpp | 4 ++++ src/socket.h | 2 ++ src/stream.h | 2 ++ src/tafnode.cpp | 6 +++--- src/tafnode.h | 4 ++-- src/tests/taf.cpp | 2 +- 12 files changed, 105 insertions(+), 17 deletions(-) (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 433fc91..5423a10 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -26,16 +26,18 @@ void Bu::BZip2::start() pBuf = new char[nBufSize]; } -void Bu::BZip2::stop() +size_t Bu::BZip2::stop() { if( bzState.state ) { if( bReading ) { BZ2_bzDecompressEnd( &bzState ); + return 0; } else { + size_t sTotal = 0; for(;;) { bzState.next_in = NULL; @@ -45,14 +47,16 @@ void Bu::BZip2::stop() int res = BZ2_bzCompress( &bzState, BZ_FINISH ); if( bzState.avail_out < nBufSize ) { - rNext.write( pBuf, nBufSize-bzState.avail_out ); + sTotal += rNext.write( pBuf, nBufSize-bzState.avail_out ); } if( res == BZ_STREAM_END ) break; } BZ2_bzCompressEnd( &bzState ); + return sTotal; } } + return 0; } void Bu::BZip2::bzError( int code ) @@ -63,13 +67,35 @@ void Bu::BZip2::bzError( int code ) return; case BZ_CONFIG_ERROR: - throw ExceptionBase("The bzip2 library has been miscompiled."); + throw ExceptionBase("BZip2: Library configured improperly, reinstall."); + + case BZ_SEQUENCE_ERROR: + throw ExceptionBase("BZip2: Functions were called in an invalid sequence."); case BZ_PARAM_ERROR: - throw ExceptionBase("bzip2 parameter error."); + throw ExceptionBase("BZip2: Invalid parameter was passed into a function."); case BZ_MEM_ERROR: - throw ExceptionBase("Not enough memory available for bzip2."); + throw ExceptionBase("BZip2: Couldn't allocate sufficient memory."); + + case BZ_DATA_ERROR: + throw ExceptionBase("BZip2: Data was corrupted before decompression."); + + case BZ_DATA_ERROR_MAGIC: + throw ExceptionBase("BZip2: Stream does not appear to be bzip2 data."); + + case BZ_IO_ERROR: + throw ExceptionBase("BZip2: File couldn't be read from / written to."); + + case BZ_UNEXPECTED_EOF: + throw ExceptionBase("BZip2: End of file encountered before end of stream."); + + case BZ_OUTBUFF_FULL: + throw ExceptionBase("BZip2: Buffer not large enough to accomidate data."); + + default: + throw ExceptionBase("BZip2: Unknown error encountered."); + } } @@ -124,6 +150,7 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes ) if( bReading == true ) throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); + size_t sTotalOut = 0; bzState.next_in = (char *)pData; bzState.avail_in = nBytes; for(;;) @@ -135,12 +162,12 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes ) if( bzState.avail_out < nBufSize ) { - rNext.write( pBuf, nBufSize-bzState.avail_out ); + sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out ); } if( bzState.avail_in == 0 ) break; } - return 0; + return sTotalOut; } diff --git a/src/bzip2.h b/src/bzip2.h index 056f336..a23f07a 100644 --- a/src/bzip2.h +++ b/src/bzip2.h @@ -18,7 +18,7 @@ namespace Bu virtual ~BZip2(); virtual void start(); - virtual void stop(); + virtual size_t stop(); virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t write( const void *pBuf, size_t nBytes ); diff --git a/src/file.cpp b/src/file.cpp index 26986a5..14b6e54 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -1,6 +1,8 @@ #include "file.h" #include "exceptions.h" #include +#include +#include Bu::File::File( const char *sName, const char *sFlags ) { @@ -11,6 +13,20 @@ Bu::File::File( const char *sName, const char *sFlags ) } } +Bu::File::File( const Bu::FString &sName, const char *sFlags ) +{ + fh = fopen( sName.getStr(), sFlags ); + if( fh == NULL ) + { + throw Bu::FileException( errno, strerror(errno) ); + } +} + +Bu::File::File( int fd, const char *sFlags ) +{ + fh = fdopen( fd, sFlags ); +} + Bu::File::~File() { close(); @@ -108,3 +124,18 @@ void Bu::File::setBlocking( bool bBlocking ) return; } +void Bu::File::truncate( long nSize ) +{ + ftruncate( fileno( fh ), nSize ); +} + +void Bu::File::flush() +{ + fflush( fh ); +} + +void Bu::File::chmod( mode_t t ) +{ + fchmod( fileno( fh ), t ); +} + diff --git a/src/file.h b/src/file.h index ee3fdb3..8107a1b 100644 --- a/src/file.h +++ b/src/file.h @@ -3,7 +3,8 @@ #include -#include "stream.h" +#include "bu/stream.h" +#include "bu/fstring.h" namespace Bu { @@ -11,6 +12,8 @@ namespace Bu { public: File( const char *sName, const char *sFlags ); + File( const Bu::FString &sName, const char *sFlags ); + File( int fd, const char *sFlags ); virtual ~File(); virtual void close(); @@ -23,6 +26,8 @@ namespace Bu virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual void flush(); + virtual bool canRead(); virtual bool canWrite(); virtual bool canSeek(); @@ -30,6 +35,16 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + inline static Bu::File tempFile( Bu::FString &sName, const char *sFlags ) + { + int afh_d = mkstemp( sName.getStr() ); + + return Bu::File( afh_d, sFlags ); + } + + void truncate( long nSize ); + void chmod( mode_t t ); + private: FILE *fh; diff --git a/src/filter.cpp b/src/filter.cpp index d3faa00..693fb9f 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -7,7 +7,7 @@ Bu::Filter::Filter( Bu::Stream &rNext ) : Bu::Filter::~Filter() { - printf("-> Bu::Filter::~Filter()\n"); + //printf("-> Bu::Filter::~Filter()\n"); } /* void Bu::Filter::start() @@ -75,3 +75,8 @@ void Bu::Filter::setBlocking( bool bBlocking ) rNext.setBlocking( bBlocking ); } +void Bu::Filter::flush() +{ + rNext.flush(); +} + diff --git a/src/filter.h b/src/filter.h index b068206..088d46e 100644 --- a/src/filter.h +++ b/src/filter.h @@ -33,7 +33,7 @@ namespace Bu virtual ~Filter(); virtual void start()=0; - virtual void stop()=0; + virtual size_t stop()=0; virtual void close(); virtual long tell(); virtual void seek( long offset ); @@ -41,6 +41,8 @@ namespace Bu virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual void flush(); + virtual bool canRead(); virtual bool canWrite(); virtual bool canSeek(); diff --git a/src/socket.cpp b/src/socket.cpp index 441678a..1832898 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -240,3 +240,7 @@ void Bu::Socket::setBlocking( bool bBlocking ) { } +void Bu::Socket::flush() +{ +} + diff --git a/src/socket.h b/src/socket.h index e65eb74..30a43fb 100644 --- a/src/socket.h +++ b/src/socket.h @@ -29,6 +29,8 @@ namespace Bu virtual void setPosEnd( long pos ); virtual bool isEOS(); + virtual void flush(); + virtual bool canRead(); virtual bool canWrite(); virtual bool canSeek(); diff --git a/src/stream.h b/src/stream.h index fa0a606..a80586b 100644 --- a/src/stream.h +++ b/src/stream.h @@ -32,6 +32,8 @@ namespace Bu virtual void setPosEnd( long pos ) = 0; virtual bool isEOS() = 0; + virtual void flush() = 0; + virtual bool canRead() = 0; virtual bool canWrite() = 0; virtual bool canSeek() = 0; diff --git a/src/tafnode.cpp b/src/tafnode.cpp index 3060606..b9a4a24 100644 --- a/src/tafnode.cpp +++ b/src/tafnode.cpp @@ -45,7 +45,7 @@ const Bu::TafNode::PropList &Bu::TafNode::getProperties( const Bu::FString &sNam return hProp.get( sName ); } -const Bu::TafNode::NodeList &Bu::TafNode::getNodes( const Bu::FString &sName ) const +const Bu::TafNode::NodeList &Bu::TafNode::getChildren( const Bu::FString &sName ) const { return hChildren.get( sName ); } @@ -55,9 +55,9 @@ const Bu::FString &Bu::TafNode::getProperty( const Bu::FString &sName ) const return getProperties( sName ).first(); } -const Bu::TafNode *Bu::TafNode::getNode( const Bu::FString &sName ) const +const Bu::TafNode *Bu::TafNode::getChild( const Bu::FString &sName ) const { - return getNodes( sName ).first(); + return getChildren( sName ).first(); } void Bu::TafNode::setName( const Bu::FString &sName ) diff --git a/src/tafnode.h b/src/tafnode.h index 10232d2..08f78e8 100644 --- a/src/tafnode.h +++ b/src/tafnode.h @@ -28,9 +28,9 @@ namespace Bu void setProperty( Bu::FString sName, Bu::FString sValue ); const Bu::FString &getProperty( const Bu::FString &sName ) const; - const TafNode *getNode( const Bu::FString &sName ) const; const PropList &getProperties( const Bu::FString &sName ) const; - const NodeList &getNodes( const Bu::FString &sName ) const; + const TafNode *getChild( const Bu::FString &sName ) const; + const NodeList &getChildren( const Bu::FString &sName ) const; void addChild( TafNode *pNode ); private: diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp index e7bad52..d135e78 100644 --- a/src/tests/taf.cpp +++ b/src/tests/taf.cpp @@ -8,7 +8,7 @@ int main() Bu::TafNode *pNode = tr.getNode(); - const Bu::TafNode *pStats = pNode->getNode("stats"); + const Bu::TafNode *pStats = pNode->getChild("stats"); printf("%s\n", pStats->getName().getStr() ); printf(" str = %s\n", pStats->getProperty("str").getStr() ); -- cgit v1.2.3 From 408aca47fd423e7c4c38665b892a13c1c9fb1e9a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 11 Jun 2007 07:31:52 +0000 Subject: Few minor tweaks to bzip2, it reports errors now...and there's a bug in odpm that could be in this, but it's going to be hard to tell... --- src/bzip2.cpp | 31 +++++++++++++++++++++++++++---- src/tests/bzip2.cpp | 10 +++++----- 2 files changed, 32 insertions(+), 9 deletions(-) (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 5423a10..6bb1429 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -64,6 +64,13 @@ void Bu::BZip2::bzError( int code ) switch( code ) { case BZ_OK: + printf("\n"); return; + case BZ_RUN_OK: + printf("\n"); return; + case BZ_FLUSH_OK: + printf("\n"); return; + case BZ_FINISH_OK: + printf("\n"); return; return; case BZ_CONFIG_ERROR: @@ -117,20 +124,36 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) { bzState.next_out = (char *)pData; bzState.avail_out = nBytes; + printf(" (pre) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); int ret = BZ2_bzDecompress( &bzState ); + printf("(post) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); nReadTotal += nRead-bzState.avail_out; if( ret == BZ_STREAM_END ) { + printf("\n"); + if( bzState.avail_in > 0 ) + { + if( rNext.canSeek() ) + { + rNext.seek( -bzState.avail_in ); + } + } return nBytes-bzState.avail_out; } + bzError( ret ); if( bzState.avail_out ) { - nRead = rNext.read( pBuf, nBufSize ); - bzState.next_in = pBuf; - bzState.avail_in = nRead; + printf("Still more to fill, in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); + + if( bzState.avail_in == 0 ) + { + nRead = rNext.read( pBuf, nBufSize ); + bzState.next_in = pBuf; + bzState.avail_in = nRead; + } } else { @@ -158,7 +181,7 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes ) bzState.avail_out = nBufSize; bzState.next_out = pBuf; - BZ2_bzCompress( &bzState, BZ_RUN ); + bzError( BZ2_bzCompress( &bzState, BZ_RUN ) ); if( bzState.avail_out < nBufSize ) { diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp index ef9328f..683d3d7 100644 --- a/src/tests/bzip2.cpp +++ b/src/tests/bzip2.cpp @@ -6,17 +6,17 @@ int main( int argc, char *argv[] ) char buf[1024]; size_t nRead; - Bu::File f( "test.bz2", "rb" ); + Bu::File f( "test.bz2", "wb" ); Bu::BZip2 bz2( f ); - Bu::File fin( argv[1], "wb"); + Bu::File fin( argv[1], "rb"); for(;;) { - nRead = bz2.read( buf, 1024 ); + nRead = fin.read( buf, 1024 ); if( nRead > 0 ) - fin.write( buf, nRead ); - if( bz2.isEOS() ) + bz2.write( buf, nRead ); + if( fin.isEOS() ) break; } } -- cgit v1.2.3 From 55244c8fd50dfda9c7c434cd05fc6280204447dd Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 11 Jun 2007 07:58:18 +0000 Subject: Wow that was a stupid bug. OK, decompression is working really well, and it corrects the underlying stream's position if it can seek, otherwise you just lose data (for now). --- src/bzip2.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 6bb1429..d3f237a 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -64,13 +64,9 @@ void Bu::BZip2::bzError( int code ) switch( code ) { case BZ_OK: - printf("\n"); return; case BZ_RUN_OK: - printf("\n"); return; case BZ_FLUSH_OK: - printf("\n"); return; case BZ_FINISH_OK: - printf("\n"); return; return; case BZ_CONFIG_ERROR: @@ -120,19 +116,16 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) int nRead = 0; int nReadTotal = bzState.total_out_lo32; + bzState.next_out = (char *)pData; + bzState.avail_out = nBytes; for(;;) { - bzState.next_out = (char *)pData; - bzState.avail_out = nBytes; - printf(" (pre) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); int ret = BZ2_bzDecompress( &bzState ); - printf("(post) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); nReadTotal += nRead-bzState.avail_out; if( ret == BZ_STREAM_END ) { - printf("\n"); if( bzState.avail_in > 0 ) { if( rNext.canSeek() ) @@ -146,8 +139,6 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) if( bzState.avail_out ) { - printf("Still more to fill, in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); - if( bzState.avail_in == 0 ) { nRead = rNext.read( pBuf, nBufSize ); -- 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/bzip2.cpp') 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 20584158e3d7f6a012a677476ba36d1691c7aa66 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 26 Jun 2007 15:11:57 +0000 Subject: Corrected a memory leak in Bu::BZip2, it wasn't cleaning up the big buffer. --- src/bzip2.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 66786e4..fbe5712 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -33,6 +33,8 @@ size_t Bu::BZip2::stop() if( bReading ) { BZ2_bzDecompressEnd( &bzState ); + delete[] pBuf; + pBuf = NULL; return 0; } else @@ -53,6 +55,8 @@ size_t Bu::BZip2::stop() break; } BZ2_bzCompressEnd( &bzState ); + delete[] pBuf; + pBuf = NULL; return sTotal; } } -- cgit v1.2.3