From d872f7e07c5367f251cf5ebb70a03916251f5306 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 1 Oct 2008 16:46:32 +0000 Subject: Ok, NIDS is getting better and better, and I went ahead and cleaned up some exception related code that's been annoying me. You should no longer have to include any exception header explicitly for normal operations, every class that has it's own exception to throw defines it in it's own headers. This may break some code that uses libbu++, but it's an easy fix, just delete the include for exceptions.h. Sometime soon I would also like to move from Bu::ExceptionBase to Bu::Exception, but that will affect a lot more code than this change did. --- src/atom.h | 2 +- src/bzip2.cpp | 1 - src/client.cpp | 3 +- src/exceptionbase.cpp | 6 +++ src/exceptionbase.h | 6 +++ src/exceptions.cpp | 21 ---------- src/exceptions.h | 40 ------------------- src/fifo.cpp | 3 +- src/fifo.h | 3 ++ src/file.cpp | 3 +- src/file.h | 3 ++ src/minimacro.cpp | 1 - src/nids.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++------- src/nids.h | 34 ++++++++++++---- src/nidsstream.cpp | 95 +++++++++++++++++++++++++++++++++++++++----- src/nidsstream.h | 13 ++++--- src/plugger.cpp | 2 + src/plugger.h | 4 +- src/serversocket.cpp | 19 ++++----- src/serversocket.h | 3 ++ src/socket.cpp | 27 ++++++------- src/socket.h | 15 ++++++- src/tafnode.cpp | 2 + src/tafnode.h | 2 + src/tafreader.cpp | 1 - src/tests/nids.cpp | 10 +++-- src/unit/file.cpp | 1 - src/xmlreader.cpp | 3 +- src/xmlreader.h | 3 ++ 29 files changed, 297 insertions(+), 135 deletions(-) delete mode 100644 src/exceptions.cpp delete mode 100644 src/exceptions.h diff --git a/src/atom.h b/src/atom.h index 2655e2b..17c0d87 100644 --- a/src/atom.h +++ b/src/atom.h @@ -10,7 +10,7 @@ #include #include -#include "bu/exceptions.h" +#include "bu/exceptionbase.h" namespace Bu { diff --git a/src/bzip2.cpp b/src/bzip2.cpp index c6742f2..10cfe8a 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -6,7 +6,6 @@ */ #include "bu/bzip2.h" -#include "bu/exceptions.h" #include "bu/trace.h" using namespace Bu; diff --git a/src/client.cpp b/src/client.cpp index fb56e2c..0ab0618 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -9,7 +9,6 @@ #include "bu/socket.h" #include #include -#include "bu/exceptions.h" #include "bu/protocol.h" #include "bu/clientlink.h" #include "bu/clientlinkfactory.h" @@ -53,7 +52,7 @@ void Bu::Client::processInput() break; } } - catch( ConnectionException &e ) + catch( Bu::SocketException &e ) { pSocket->close(); bWantsDisconnect = true; diff --git a/src/exceptionbase.cpp b/src/exceptionbase.cpp index 9515e2d..207fa47 100644 --- a/src/exceptionbase.cpp +++ b/src/exceptionbase.cpp @@ -38,6 +38,7 @@ Bu::ExceptionBase::ExceptionBase( int nCode ) throw() : } Bu::ExceptionBase::ExceptionBase( const ExceptionBase &e ) throw () : + std::exception( e ), nErrorCode( e.nErrorCode ), sWhat( NULL ) { @@ -83,3 +84,8 @@ int Bu::ExceptionBase::getErrorCode() return nErrorCode; } + +namespace Bu +{ + subExceptionDef( UnsupportedException ) +} diff --git a/src/exceptionbase.h b/src/exceptionbase.h index 37f4418..6402481 100644 --- a/src/exceptionbase.h +++ b/src/exceptionbase.h @@ -178,4 +178,10 @@ name::name( const name &e ) throw() : \ { \ } +namespace Bu +{ + // Exceptions that are so general they could be used anywhere go here. + subExceptionDecl( UnsupportedException ) +} + #endif diff --git a/src/exceptions.cpp b/src/exceptions.cpp deleted file mode 100644 index 5d6deeb..0000000 --- a/src/exceptions.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "exceptions.h" -#include - -namespace Bu -{ - subExceptionDef( XmlException ) - subExceptionDef( TafException ) - subExceptionDef( FileException ) - subExceptionDef( FifoException ) - subExceptionDef( SocketException ) - subExceptionDef( ConnectionException ) - subExceptionDef( PluginException ) - subExceptionDef( UnsupportedException ) -} diff --git a/src/exceptions.h b/src/exceptions.h deleted file mode 100644 index 91e0e6d..0000000 --- a/src/exceptions.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_EXCEPTIONS_H -#define BU_EXCEPTIONS_H - -#include "bu/exceptionbase.h" -#include - -namespace Bu -{ - subExceptionDecl( XmlException ) - subExceptionDecl( TafException ) - subExceptionDecl( FileException ) - subExceptionDecl( FifoException ) - subExceptionDecl( SocketException ) - subExceptionDecl( ConnectionException ) - subExceptionDecl( PluginException ) - subExceptionDecl( UnsupportedException ) - - enum eFileException - { - excodeEOF - }; - - enum eConnectionException - { - excodeReadError, - excodeWriteError, - excodeBadReadError, - excodeConnectionClosed, - excodeSocketTimeout - }; -} - -#endif diff --git a/src/fifo.cpp b/src/fifo.cpp index f909e61..3f27b2c 100644 --- a/src/fifo.cpp +++ b/src/fifo.cpp @@ -6,12 +6,13 @@ */ #include "fifo.h" -#include "exceptions.h" #include #include #include #include +namespace Bu { subExceptionDef( FifoException ) } + Bu::Fifo::Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc ) : iFlags( iFlags ), iIn( -1 ), diff --git a/src/fifo.h b/src/fifo.h index 4989839..2da5f16 100644 --- a/src/fifo.h +++ b/src/fifo.h @@ -14,9 +14,12 @@ #include "bu/stream.h" #include "bu/fstring.h" +#include "bu/exceptionbase.h" namespace Bu { + subExceptionDecl( FifoException ); + /** * A fifo stream. *@ingroup Streams diff --git a/src/file.cpp b/src/file.cpp index b3bae96..3419216 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -6,12 +6,13 @@ */ #include "file.h" -#include "exceptions.h" #include #include #include #include +namespace Bu { subExceptionDef( FileException ) } + Bu::File::File( const char *sName, const char *sFlags ) { fh = fopen( sName, sFlags ); diff --git a/src/file.h b/src/file.h index 554ebc9..e48e6f1 100644 --- a/src/file.h +++ b/src/file.h @@ -14,9 +14,12 @@ #include "bu/stream.h" #include "bu/fstring.h" +#include "bu/exceptionbase.h" namespace Bu { + subExceptionDecl( FileException ); + /** * A file stream. *@ingroup Streams diff --git a/src/minimacro.cpp b/src/minimacro.cpp index 6fc5382..b97cefc 100644 --- a/src/minimacro.cpp +++ b/src/minimacro.cpp @@ -6,7 +6,6 @@ */ #include "bu/minimacro.h" -#include "bu/exceptions.h" Bu::MiniMacro::MiniMacro() { diff --git a/src/nids.cpp b/src/nids.cpp index 41c4dc1..d0cb843 100644 --- a/src/nids.cpp +++ b/src/nids.cpp @@ -3,9 +3,20 @@ #include "bu/nidsstream.h" #include +#define NIDS_MAGIC_CODE "\xFF\xC3\x99\xBD" + +namespace Bu +{ + subExceptionDef( NidsException ) +} + Bu::Nids::Nids( Bu::Stream &sStore ) : - sStore( sStore ) + sStore( sStore ), + iBlockSize( 0 ), + iBlocks( 0 ), + iBlockStart( -1 ) { + printf("blockUnused = %u\n", blockUnused ); printf("Stream caps:\n" " canRead: %s\n" " canWrite: %s\n" @@ -25,19 +36,32 @@ Bu::Nids::Nids( Bu::Stream &sStore ) : sStore.isOpen()?"yes":"no" ); printf("sizeof(Block) = %db\n", sizeof(Block) ); + + } Bu::Nids::~Nids() { } +void Bu::Nids::initialize() +{ + char buf[4]; + sStore.read( buf, 4 ); + if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) ) + { + throw NidsException( + "Stream does not appear to be a valid NIDS format."); + } +} + void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) { char cBuf = 0; int iBuf = 0; // Magic number - sStore.write( "\xFF\xC3\x99\xBD", 4 ); + sStore.write( NIDS_MAGIC_CODE, 4 ); // Version (0) sStore.write( &cBuf, 1 ); @@ -59,56 +83,112 @@ void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) this->iBlockSize = iBlockSize; this->iBlocks = iPreAllocate; this->iBlockStart = sStore.tell(); + printf("iBlockStart = %d\n", this->iBlockStart ); bsBlockUsed.setSize( iPreAllocate, true ); printf("%d blocks, %db each, %db block offset\n", iBlocks, iBlockSize, iBlockStart ); - char *block = new char[iBlockSize]; + Block *block = (Block *)new char[iBlockSize]; memset( block, 0, iBlockSize ); + block->uFirstBlock = block->uNextBlock = block->uPrevBlock = blockUnused; for( int j = 0; j < iPreAllocate; j++ ) { sStore.write( block, iBlockSize ); } + delete[] (char *)block; } -int Bu::Nids::createStream( int iPreAllocate ) +uint32_t Bu::Nids::createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock, + int /*iPreAllocate*/ ) { for( int j = 0; j < iBlocks; j++ ) { if( !bsBlockUsed.getBit( j ) ) { - Block b = { j, blockUnused, blockUnused, 0, 0 }; + Block b = { j, blockUnused, uPrevBlock, 0, 0, { } }; + if( uFirstBlock != blockUnused ) + b.uFirstBlock = uFirstBlock; bsBlockUsed.setBit( j ); sStore.setPos( iBlockStart+(iBlockSize*j) ); sStore.write( &b, sizeof(b) ); + return j; } } - return 0; + return blockUnused; } -void Bu::Nids::deleteStream( int iID ) +int Bu::Nids::createStream( int iPreAllocate ) +{ + return createBlock( blockUnused, blockUnused, iPreAllocate ); +} + +void Bu::Nids::deleteStream( int /*iID*/ ) { } Bu::NidsStream Bu::Nids::openStream( int iID ) { - return NidsStream( *this ); + if( iBlockStart < 0 ) + { + initialize(); + } + return NidsStream( *this, iID ); } -void Bu::Nids::extendStream( int iID, int iBlockCount ) +int Bu::Nids::getBlockSize() { + return iBlockSize; } +/* +void Bu::Nids::extendStream( int iID, int iBlockCount ) +{ +}*/ -void Bu::Nids::getBlock( int iIndex, Bu::Nids::Block *pBlock ) +void Bu::Nids::getBlock( uint32_t uIndex, Bu::Nids::Block *pBlock ) { - sStore.setPos( iBlockStart + (iBlockSize*iIndex) ); + sStore.setPos( iBlockStart + (iBlockSize*uIndex) ); sStore.read( pBlock, iBlockSize ); } -void Bu::Nids::setBlock( int iIndex, Bu::Nids::Block *pBlock ) +void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock ) { - sStore.setPos( iBlockStart + (iBlockSize*iIndex) ); + sStore.setPos( iBlockStart + (iBlockSize*uIndex) ); sStore.write( pBlock, iBlockSize ); } +void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize ) +{ + sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 ); + sStore.write( &uSize, 4 ); +} + +uint32_t Bu::Nids::getNextBlock( uint32_t uIndex, + struct Bu::Nids::Block *pBlock ) +{ + uint32_t uNew; + if( pBlock->uNextBlock == blockUnused ) + { + uNew = createBlock( pBlock->uFirstBlock, uIndex ); + sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 ); + sStore.write( &uNew, 4 ); + getBlock( uNew, pBlock ); + } + else + { + uNew = pBlock->uNextBlock; + getBlock( pBlock->uNextBlock, pBlock ); + } + return uNew; +} + +Bu::Nids::Block *Bu::Nids::newBlock() +{ + return (Block *)new char[iBlockSize]; +} + +void Bu::Nids::deleteBlock( Block *pBlock ) +{ + delete[] (char *)pBlock; +} + diff --git a/src/nids.h b/src/nids.h index e364289..cdefdcb 100644 --- a/src/nids.h +++ b/src/nids.h @@ -3,12 +3,15 @@ #include #include "bu/bitstring.h" +#include "bu/exceptionbase.h" namespace Bu { class Stream; class NidsStream; + subExceptionDecl( NidsException ) + /** * Numerically Indexed Data Streams. This is a working name so I can * actually get some code written instead of agonizing over the name. @@ -23,6 +26,14 @@ namespace Bu Nids( Bu::Stream &sStore ); virtual ~Nids(); + /** + * Initialize this object based on the data already in the assosiated + * stream. This will be called automatically for you if you forget, + * but if you want to pre-initialize for some reason, just call this + * once before you actually start doing anything with your Nids. + */ + void initialize(); + /** * Create a new Nids system in the assosiated stream. This should be * used carefully, it will destroy all data already within the stream. @@ -46,14 +57,16 @@ namespace Bu */ NidsStream openStream( int iID ); + int getBlockSize(); + private: typedef struct Block { - uint32_t iFirstBlock; - uint32_t iNextBlock; - uint32_t iPrevBlock; - uint32_t iBytesUsed; - uint32_t iReserved; + uint32_t uFirstBlock; + uint32_t uNextBlock; + uint32_t uPrevBlock; + uint32_t uBytesUsed; + uint32_t uReserved; unsigned char pData[0]; } Block; @@ -62,9 +75,14 @@ namespace Bu blockUnused = 0xFFFFFFFFUL }; - void extendStream( int iID, int iBlockCount=1 ); - void getBlock( int iIndex, struct Nids::Block *pBlock ); - void setBlock( int iIndex, struct Nids::Block *pBlock ); + uint32_t createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock, + int iPreAllocate=1 ); + void getBlock( uint32_t uIndex, struct Nids::Block *pBlock ); + void setBlock( uint32_t uIndex, struct Nids::Block *pBlock ); + void updateStreamSize( uint32_t uIndex, uint32_t uSize ); + uint32_t getNextBlock( uint32_t uIndex, struct Nids::Block *pBlock ); + Block *newBlock(); + void deleteBlock( Block *pBlock ); private: Bu::Stream &sStore; diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp index 2f24d69..740dc1f 100644 --- a/src/nidsstream.cpp +++ b/src/nidsstream.cpp @@ -1,20 +1,41 @@ #include "bu/nidsstream.h" -Bu::NidsStream::NidsStream( Nids &rNids ) : +Bu::NidsStream::NidsStream( Nids &rNids, uint32_t uStream ) : rNids( rNids ), - iPos( 0 ) + uStream( uStream ), + pCurBlock( NULL ), + uCurBlock( uStream ), + uSize( 0 ), + uBlockSize( rNids.iBlockSize-sizeof(Nids::Block) ), + uPos( 0 ) { + printf("NidsStream::allocated\n"); + pCurBlock = rNids.newBlock(); + rNids.getBlock( uStream, pCurBlock ); + uSize = pCurBlock->uBytesUsed; } +/* Bu::NidsStream::NidsStream( const Bu::NidsStream &rSrc ) : + Stream( rSrc ), rNids( rSrc.rNids ), - iPos( 0 ) -{ - -} + uStream( rSrc.uStream ), + pCurBlock( NULL ), + uCurBlock( uStream ), + uSize( 0 ), + uBlockSize( rSrc.uBlockSize ), + iUsable( uBlockSize-sizeof(Nids::Block) ), + uPos( 0 ) +{ + printf("NidsStream::copied\n"); + pCurBlock = rNids.newBlock(); + rNids.getBlock( uStream, pCurBlock ); +}*/ Bu::NidsStream::~NidsStream() { + rNids.updateStreamSize( uStream, uSize ); + rNids.deleteBlock( pCurBlock ); } void Bu::NidsStream::close() @@ -23,29 +44,85 @@ void Bu::NidsStream::close() size_t Bu::NidsStream::read( void *pBuf, size_t nBytes ) { + if( uPos%uBlockSize+nBytes < uBlockSize ) + { + size_t iRead = nBytes; + if( iRead > pCurBlock->uBytesUsed ) + iRead = pCurBlock->uBytesUsed; + memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); + uPos += nBytes; + printf("a: block %u = %ub (%ub total)\n", + uCurBlock, pCurBlock->uBytesUsed, uSize ); + return iRead; + } + else + { + //size_t iTotal = 0; + for(;;) + { + + } + } return 0; } size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes ) { - return 0; + if( uPos%uBlockSize+nBytes < uBlockSize ) + { + memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes ); + pCurBlock->uBytesUsed += nBytes; + rNids.setBlock( uCurBlock, pCurBlock ); + uPos += nBytes; + uSize += nBytes; + printf("a: block %u = %ub (%ub total)\n", + uCurBlock, pCurBlock->uBytesUsed, uSize ); + return nBytes; + } + else + { + size_t nTotal = 0; + for(;;) + { + uint32_t uNow = uBlockSize-(uPos%uBlockSize); + if( nBytes < uNow ) + uNow = nBytes; + memcpy( pCurBlock->pData+(uPos%uBlockSize), + &((char *)pBuf)[nTotal], uNow ); + pCurBlock->uBytesUsed += uNow; + rNids.setBlock( uCurBlock, pCurBlock ); + uSize += uNow; + uPos += uNow; + nTotal += uNow; + nBytes -= uNow; + printf("b: block %u = %ub (%ub total)\n", + uCurBlock, pCurBlock->uBytesUsed, uSize ); + if( nBytes == 0 ) + return nTotal; + if( pCurBlock->uBytesUsed == uBlockSize ) + uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock ); + } + } } long Bu::NidsStream::tell() { - return 0; + return uPos; } void Bu::NidsStream::seek( long offset ) { + uPos += offset; } void Bu::NidsStream::setPos( long pos ) { + uPos = pos; } void Bu::NidsStream::setPosEnd( long pos ) { + uPos = uSize-pos-1; } bool Bu::NidsStream::isEOS() @@ -92,7 +169,7 @@ bool Bu::NidsStream::isBlocking() return true; } -void Bu::NidsStream::setBlocking( bool bBlocking ) +void Bu::NidsStream::setBlocking( bool /*bBlocking*/ ) { } diff --git a/src/nidsstream.h b/src/nidsstream.h index f64fca4..c220e5c 100644 --- a/src/nidsstream.h +++ b/src/nidsstream.h @@ -13,10 +13,10 @@ namespace Bu /** * These can only be created by the Nids class. */ - NidsStream( Nids &rNids ); + NidsStream( Nids &rNids, uint32_t uStream ); public: - NidsStream( const NidsStream &rSrc ); +// NidsStream( const NidsStream &rSrc ); virtual ~NidsStream(); virtual void close(); @@ -40,9 +40,12 @@ namespace Bu private: Nids &rNids; - typedef struct Bu::Hash BlockHash; - BlockHash hBlock; - long iPos; + uint32_t uStream; + Nids::Block *pCurBlock; + uint32_t uCurBlock; + uint32_t uSize; + uint32_t uBlockSize; + uint32_t uPos; }; }; diff --git a/src/plugger.cpp b/src/plugger.cpp index d891174..6ff31c4 100644 --- a/src/plugger.cpp +++ b/src/plugger.cpp @@ -6,3 +6,5 @@ */ #include "plugger.h" + +namespace Bu { subExceptionDef( PluginException ) } diff --git a/src/plugger.h b/src/plugger.h index 6ae0296..746f5ac 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -12,12 +12,14 @@ #include "bu/hash.h" #include "bu/list.h" #include -#include "bu/exceptions.h" +#include "bu/exceptionbase.h" #include "bu/fstring.h" #include namespace Bu { + subExceptionDecl( PluginException ); + typedef struct PluginInfo { const char *sID; diff --git a/src/serversocket.cpp b/src/serversocket.cpp index 30f584d..6f7fc00 100644 --- a/src/serversocket.cpp +++ b/src/serversocket.cpp @@ -18,9 +18,10 @@ #include #include #include -#include "serversocket.h" -#include "exceptions.h" -#include "osx_compatibility.h" +#include "bu/serversocket.h" +#include "bu/osx_compatibility.h" + +namespace Bu { subExceptionDef( ServerSocketException ) } Bu::ServerSocket::ServerSocket( int nPort, int nPoolSize ) : nPort( nPort ) @@ -64,7 +65,7 @@ void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) nServer = socket( PF_INET, SOCK_STREAM, 0 ); if( nServer < 0 ) { - throw Bu::SocketException("Couldn't create a listen socket."); + throw Bu::ServerSocketException("Couldn't create a listen socket."); } int opt = 1; @@ -78,12 +79,12 @@ void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) if( bind( nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) { - throw Bu::SocketException("Couldn't bind to the listen socket."); + throw Bu::ServerSocketException("Couldn't bind to the listen socket."); } if( listen( nServer, nPoolSize ) < 0 ) { - throw Bu::SocketException( + throw Bu::ServerSocketException( "Couldn't begin listening to the server socket." ); } @@ -109,7 +110,7 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) if( TEMP_FAILURE_RETRY(select( nServer+1, &fdRead, NULL, NULL, &xT )) < 0 ) { - throw SocketException( + throw Bu::ServerSocketException( "Error scanning for new connections: %s", strerror( errno ) ); } @@ -134,7 +135,7 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) #endif /* __CYGWIN__ */ if( nClient < 0 ) { - throw SocketException( + throw Bu::ServerSocketException( "Error accepting a new connection: %s", strerror( errno ) ); } @@ -150,7 +151,7 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) flags |= O_NONBLOCK; if( fcntl( nClient, F_SETFL, flags ) < 0) { - throw SocketException( + throw Bu::ServerSocketException( "Error setting option on client socket: %s", strerror( errno ) ); diff --git a/src/serversocket.h b/src/serversocket.h index 8267cea..1742786 100644 --- a/src/serversocket.h +++ b/src/serversocket.h @@ -10,10 +10,13 @@ #include #include "bu/fstring.h" +#include "bu/exceptionbase.h" #include namespace Bu { + subExceptionDecl( ServerSocketException ); + /** * A single tcp/ip server socket. When created the server socket will bind * to the specified interface and port, and immediately begin listening for diff --git a/src/socket.cpp b/src/socket.cpp index 651a2e1..531d8ac 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -19,11 +19,12 @@ #include #include #include "socket.h" -#include "exceptions.h" #include "osx_compatibility.h" #define RBS (1024*2) +namespace Bu { subExceptionDef( SocketException ) } + Bu::Socket::Socket( int nSocket ) : nSocket( nSocket ), bActive( true ) @@ -50,7 +51,7 @@ Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) flags |= O_NONBLOCK; if (fcntl(nSocket, F_SETFL, flags) < 0) { - throw ExceptionBase("Couldn't set socket options.\n"); + throw Bu::SocketException("Couldn't set socket options.\n"); } /* Connect to the server. */ @@ -63,7 +64,7 @@ Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) hostinfo = gethostbyname( sAddr.getStr() ); if (hostinfo == NULL) { - throw ExceptionBase("Couldn't resolve hostname.\n"); + throw Bu::SocketException("Couldn't resolve hostname.\n"); } xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; } @@ -136,8 +137,8 @@ void Bu::Socket::read() { //printf("errno: %d, %s\n", errno, strerror( errno ) ); //perror("readInput"); - throw ConnectionException( - excodeReadError, + throw SocketException( + SocketException::cRead, "Read error: %s", strerror( errno ) ); @@ -162,8 +163,8 @@ void Bu::Socket::read() struct timeval tv = { 0, 0 }; int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); if( retval == -1 ) - throw ConnectionException( - excodeBadReadError, + throw SocketException( + SocketException::cBadRead, "Bad Read error" ); if( !FD_ISSET( nSocket, &rfds ) ) @@ -178,7 +179,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) ); if( nRead < 0 ) { - throw ConnectionException( excodeReadError, strerror(errno) ); + throw SocketException( SocketException::cRead, strerror(errno) ); } return nRead; } @@ -220,7 +221,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) if( nWrote < 0 ) { if( errno == EAGAIN ) return 0; - throw ConnectionException( excodeWriteError, strerror(errno) ); + throw SocketException( SocketException::cWrite, strerror(errno) ); } return nWrote; } @@ -288,8 +289,8 @@ bool Bu::Socket::canRead() struct timeval tv = { 0, 0 }; int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); if( retval == -1 ) - throw ConnectionException( - excodeBadReadError, + throw SocketException( + SocketException::cBadRead, "Bad Read error" ); if( !FD_ISSET( nSocket, &rfds ) ) @@ -305,8 +306,8 @@ bool Bu::Socket::canWrite() struct timeval tv = { 0, 0 }; int retval = select( nSocket+1, NULL, &wfds, NULL, &tv ); if( retval == -1 ) - throw ConnectionException( - excodeBadReadError, + throw SocketException( + SocketException::cBadRead, "Bad Read error" ); if( !FD_ISSET( nSocket, &wfds ) ) diff --git a/src/socket.h b/src/socket.h index 838dfad..337b797 100644 --- a/src/socket.h +++ b/src/socket.h @@ -10,11 +10,22 @@ #include -#include "stream.h" -#include "fstring.h" +#include "bu/stream.h" +#include "bu/fstring.h" +#include "bu/exceptionbase.h" namespace Bu { + subExceptionDeclBegin( SocketException ); + enum { + cRead, + cWrite, + cBadRead, + cClosed, + cTimeout + }; + subExceptionDeclEnd(); + /** *@author Mike Buland *@ingroup Serving diff --git a/src/tafnode.cpp b/src/tafnode.cpp index 35be4ff..128355a 100644 --- a/src/tafnode.cpp +++ b/src/tafnode.cpp @@ -7,6 +7,8 @@ #include "bu/tafnode.h" +namespace Bu { subExceptionDef( TafException ) } + Bu::TafNode::TafNode( NodeType eType ) : eType( eType ) { diff --git a/src/tafnode.h b/src/tafnode.h index fcdfdf5..526ef68 100644 --- a/src/tafnode.h +++ b/src/tafnode.h @@ -12,9 +12,11 @@ #include "bu/fstring.h" #include "bu/hash.h" #include "bu/list.h" +#include "bu/exceptionbase.h" namespace Bu { + subExceptionDecl( TafException ); /** * *@ingroup Taf diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 282f037..c5ef8cc 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -6,7 +6,6 @@ */ #include "bu/tafreader.h" -#include "bu/exceptions.h" #include "bu/fstring.h" #include diff --git a/src/tests/nids.cpp b/src/tests/nids.cpp index 18f66a6..4856883 100644 --- a/src/tests/nids.cpp +++ b/src/tests/nids.cpp @@ -10,15 +10,17 @@ int main( int argc, char *argv[] ) return 1; } - Bu::File fOut( argv[1], "wb"); + Bu::File fOut( argv[1], "wb+"); Bu::Nids n( fOut ); - n.initialize( 1024, 5 ); +// n.initialize( 120, 5 ); Bu::NidsStream s = n.openStream( n.createStream() ); - - Bu::FString sBuf("Hey there, man...uh...how's it going?"); +/* + Bu::FString sBuf( 350 ); + memset( sBuf.getStr(), 'a', 350 ); s.write( sBuf ); + */ return 0; } diff --git a/src/unit/file.cpp b/src/unit/file.cpp index 68574ca..a22239d 100644 --- a/src/unit/file.cpp +++ b/src/unit/file.cpp @@ -7,7 +7,6 @@ #include "bu/unitsuite.h" #include "bu/file.h" -#include "bu/exceptions.h" #include #include diff --git a/src/xmlreader.cpp b/src/xmlreader.cpp index 9d299e6..12d8dba 100644 --- a/src/xmlreader.cpp +++ b/src/xmlreader.cpp @@ -1,6 +1,7 @@ #include "bu/xmlreader.h" #include "bu/stream.h" -#include "bu/exceptions.h" + +namespace Bu { subExceptionDef( XmlException ) } Bu::XmlReader::XmlReader( Stream &rInput ) : rInput( rInput ), diff --git a/src/xmlreader.h b/src/xmlreader.h index 375dfe3..f2d975b 100644 --- a/src/xmlreader.h +++ b/src/xmlreader.h @@ -2,11 +2,14 @@ #define BU_XML_READER_H #include "bu/fstring.h" +#include "bu/exceptionbase.h" namespace Bu { class Stream; + subExceptionDecl( XmlException ); + class XmlReader { public: -- cgit v1.2.3