From 10c557562e1d67c55314c212371ea9cb7f802863 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 19 Jan 2011 23:02:44 +0000 Subject: Started work adding more functions to stream, and changing to a new size type. --- src/client.h | 1 + src/config.h | 2 ++ src/extratypes.h | 17 +++++++++++++++++ src/membuf.cpp | 18 +++++++++--------- src/membuf.h | 17 +++++++++-------- src/stdstream.cpp | 14 +++++++------- src/stdstream.h | 14 +++++++------- src/stream.cpp | 2 +- src/stream.h | 42 +++++++++++++++++++++++++++++++++--------- src/tcpsocket.cpp | 40 ++++++++++++++++++++++++++++------------ src/tcpsocket.h | 23 ++++++++++++++--------- 11 files changed, 128 insertions(+), 62 deletions(-) create mode 100644 src/extratypes.h diff --git a/src/client.h b/src/client.h index 096df2f..98b31f7 100644 --- a/src/client.h +++ b/src/client.h @@ -10,6 +10,7 @@ #include +#include "bu/config.h" #include "bu/fstring.h" #include "bu/queuebuf.h" diff --git a/src/config.h b/src/config.h index ad4991e..3a19a1f 100644 --- a/src/config.h +++ b/src/config.h @@ -15,4 +15,6 @@ #include "bu/compat/osx.h" #include "bu/compat/linux.h" +#include "bu/extratypes.h" + #endif diff --git a/src/extratypes.h b/src/extratypes.h new file mode 100644 index 0000000..bc32dcd --- /dev/null +++ b/src/extratypes.h @@ -0,0 +1,17 @@ +#ifndef EXTRA_TYPES_H +#define EXTRA_TYPES_H + +#include "bu/config.h" + +namespace Bu +{ +#ifdef USE_64BIT_IO + typedef int64_t size; + typedef uint64_t usize; +#else + typedef int32_t size; + typedef uint32_t usize; +#endif +}; + +#endif diff --git a/src/membuf.cpp b/src/membuf.cpp index b822641..1a6bf9a 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -28,9 +28,9 @@ void Bu::MemBuf::close() { } -size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) +size Bu::MemBuf::read( void *pBuf, size nBytes ) { - if( (size_t)sBuf.getSize()-(size_t)nPos < nBytes ) + if( (size)sBuf.getSize()-(size)nPos < nBytes ) nBytes = sBuf.getSize()-nPos; memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); @@ -39,7 +39,7 @@ size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) return nBytes; } -size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) +size Bu::MemBuf::write( const void *pBuf, size nBytes ) { if( nPos == sBuf.getSize() ) { @@ -52,7 +52,7 @@ size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) { // Trickier, we must do this in two parts, overwrite, then append // Frist, overwrite. - size_t iOver = sBuf.getSize() - nPos; + size iOver = sBuf.getSize() - nPos; if( iOver > nBytes ) iOver = nBytes; memcpy( sBuf.getStr()+nPos, pBuf, iOver ); @@ -66,26 +66,26 @@ size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) } } -long Bu::MemBuf::tell() +size Bu::MemBuf::tell() { return nPos; } -void Bu::MemBuf::seek( long offset ) +void Bu::MemBuf::seek( size offset ) { nPos += offset; if( nPos < 0 ) nPos = 0; else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); } -void Bu::MemBuf::setPos( long pos ) +void Bu::MemBuf::setPos( size pos ) { nPos = pos; if( nPos < 0 ) nPos = 0; else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); } -void Bu::MemBuf::setPosEnd( long pos ) +void Bu::MemBuf::setPosEnd( size pos ) { nPos = sBuf.getSize()-pos; if( nPos < 0 ) nPos = 0; @@ -140,7 +140,7 @@ void Bu::MemBuf::setBlocking( bool ) { } -void Bu::MemBuf::setSize( long iSize ) +void Bu::MemBuf::setSize( size iSize ) { if( iSize < 0 ) iSize = 0; diff --git a/src/membuf.h b/src/membuf.h index 9e406c1..a75aca1 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -10,6 +10,7 @@ #include +#include "bu/config.h" #include "bu/stream.h" #include "bu/fstring.h" @@ -27,14 +28,14 @@ namespace Bu virtual ~MemBuf(); virtual void close(); - virtual size_t read( void *pBuf, size_t nBytes ); + virtual size read( void *pBuf, size iBytes ); - virtual size_t write( const void *pBuf, size_t nBytes ); + virtual size write( const void *pBuf, size iBytes ); using Stream::write; - virtual long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); + virtual size tell(); + virtual void seek( size offset ); + virtual void setPos( size pos ); + virtual void setPosEnd( size pos ); virtual bool isEos(); virtual bool isOpen(); virtual void flush(); @@ -45,14 +46,14 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( long iSize ); + virtual void setSize( size iSize ); Bu::FString &getString(); void setString( const Bu::FString &sNewData ); private: Bu::FString sBuf; - long nPos; + size nPos; }; } diff --git a/src/stdstream.cpp b/src/stdstream.cpp index 32ddec4..64ca1cd 100644 --- a/src/stdstream.cpp +++ b/src/stdstream.cpp @@ -20,30 +20,30 @@ void Bu::StdStream::close() { } -size_t Bu::StdStream::read( void *pBuf, size_t nBytes ) +Bu::size Bu::StdStream::read( void *pBuf, Bu::size nBytes ) { return fread( pBuf, 1, nBytes, stdin ); } -size_t Bu::StdStream::write( const void *pBuf, size_t nBytes ) +Bu::size Bu::StdStream::write( const void *pBuf, Bu::size nBytes ) { return fwrite( pBuf, 1, nBytes, stdout ); } -long Bu::StdStream::tell() +Bu::size Bu::StdStream::tell() { return 0; } -void Bu::StdStream::seek( long ) +void Bu::StdStream::seek( Bu::size ) { } -void Bu::StdStream::setPos( long ) +void Bu::StdStream::setPos( Bu::size ) { } -void Bu::StdStream::setPosEnd( long ) +void Bu::StdStream::setPosEnd( Bu::size ) { } @@ -96,7 +96,7 @@ void Bu::StdStream::setBlocking( bool ) { } -void Bu::StdStream::setSize( long ) +void Bu::StdStream::setSize( Bu::size ) { } diff --git a/src/stdstream.h b/src/stdstream.h index 4efeece..c500dfc 100644 --- a/src/stdstream.h +++ b/src/stdstream.h @@ -23,13 +23,13 @@ namespace Bu virtual ~StdStream(); virtual void close(); - virtual size_t read( void *pBuf, size_t nBytes ); - virtual size_t write( const void *pBuf, size_t nBytes ); + virtual size read( void *pBuf, size nBytes ); + virtual size write( const void *pBuf, size nBytes ); using Stream::write; - virtual long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); + virtual size tell(); + virtual void seek( size offset ); + virtual void setPos( size pos ); + virtual void setPosEnd( size pos ); virtual bool isEos(); virtual bool isOpen(); virtual void flush(); @@ -40,7 +40,7 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( long iSize ); + virtual void setSize( size iSize ); }; } diff --git a/src/stream.cpp b/src/stream.cpp index 0e05cad..f13d33a 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -30,7 +30,7 @@ Bu::FString Bu::Stream::readLine() } } -size_t Bu::Stream::write( const Bu::FString &sBuf ) +Bu::size Bu::Stream::write( const Bu::FString &sBuf ) { return write( sBuf.getStr(), sBuf.getSize() ); } diff --git a/src/stream.h b/src/stream.h index 0ea5560..e7db7af 100644 --- a/src/stream.h +++ b/src/stream.h @@ -8,6 +8,8 @@ #ifndef BU_STREAM_H #define BU_STREAM_H +#include "bu/config.h" + #include #include @@ -43,7 +45,7 @@ namespace Bu *@param nBytes (size_t) Max data to read. *@returns (size_t) Amount of data read. */ - virtual size_t read( void *pBuf, size_t nBytes ) = 0; + virtual size read( void *pBuf, size iBytes ) = 0; /** * Attempts to read a complete line from the stream. This will stop @@ -59,33 +61,33 @@ namespace Bu *@param nBytes (size_t) Amount of data to write from pBuf. *@returns (size_t) Amount of data actually written. */ - virtual size_t write( const void *pBuf, size_t nBytes ) = 0; + virtual size write( const void *pBuf, size iBytes ) = 0; - virtual size_t write( const Bu::FString &sBuf ); + virtual size write( const Bu::FString &sBuf ); /** * Get the current position in the stream. *@returns (long) The current position in the stream. */ - virtual long tell() = 0; + virtual size tell() = 0; /** * Seek to a position in the stream relative to the current position. *@param offset (long) Offset from current position to seek to. */ - virtual void seek( long offset ) = 0; + virtual void seek( size offset ) = 0; /** * Set position in the stream relative to the start of the stream. *@param pos (long) The position. */ - virtual void setPos( long pos ) = 0; + virtual void setPos( size pos ) = 0; /** * Set position in the stream relative to the end of the stream. *@param pos (long) The position. */ - virtual void setPosEnd( long pos ) = 0; + virtual void setPosEnd( size pos ) = 0; /** * Are we at the end of the stream? @@ -158,10 +160,32 @@ namespace Bu * removed from the end of the stream, but the content of the added * data is undefined. */ - virtual void setSize( long iSize ) = 0; + virtual void setSize( size iSize ) = 0; + + /** + * Returns the size of the stream if the stream can have a size. For + * streams that do not (sockets, pipes, etc.) this should throw an + * unsupported exception. + */ + virtual size getSize() const = 0; - public: // Filters + /** + * Returns the block-size of the stream, if it has one. This should + * throw an unsupported exception. In some cases the block size + * returned will not represent quite the same thing, for example, + * sockets will return their MTU, while files will return the + * filesystem's block size, and memory buffers will throw an exception. + */ + virtual size getBlockSize() const = 0; + /** + * If possible, this returns a string that can be used to describe how + * to access the open stream. Not all streams support this, such as + * MemBuf, but for files it may give you a path to a file, for a socket + * it may give you an ip address, etc. If it isn't supported, an empty + * string may be returned. + */ + virtual Bu::FString getLocation() const = 0; private: diff --git a/src/tcpsocket.cpp b/src/tcpsocket.cpp index bbd9cf5..22acdf7 100644 --- a/src/tcpsocket.cpp +++ b/src/tcpsocket.cpp @@ -149,7 +149,7 @@ void Bu::TcpSocket::close() bActive = false; } -size_t Bu::TcpSocket::read( void *pBuf, size_t nBytes ) +Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes ) { fd_set rfds; FD_ZERO(&rfds); @@ -195,11 +195,11 @@ size_t Bu::TcpSocket::read( void *pBuf, size_t nBytes ) return 0; } -size_t Bu::TcpSocket::read( void *pBuf, size_t nBytes, +Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes, uint32_t nSec, uint32_t nUSec ) { struct timeval tv; - size_t nRead = 0; + Bu::size nRead = 0; fd_set rfds; FD_ZERO(&rfds); @@ -239,7 +239,7 @@ size_t Bu::TcpSocket::read( void *pBuf, size_t nBytes, return nRead; } -size_t Bu::TcpSocket::write( const void *pBuf, size_t nBytes ) +Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes ) { //#ifdef WIN32 int nWrote = TEMP_FAILURE_RETRY( @@ -261,10 +261,10 @@ size_t Bu::TcpSocket::write( const void *pBuf, size_t nBytes ) return nWrote; } -size_t Bu::TcpSocket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) +Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes, uint32_t nSec, uint32_t nUSec ) { struct timeval tv; - size_t nWrote = 0; + Bu::size nWrote = 0; fd_set wfds; FD_ZERO(&wfds); @@ -304,22 +304,22 @@ size_t Bu::TcpSocket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uin return nWrote; } -long Bu::TcpSocket::tell() +Bu::size Bu::TcpSocket::tell() { throw UnsupportedException(); } -void Bu::TcpSocket::seek( long ) +void Bu::TcpSocket::seek( Bu::size ) { throw UnsupportedException(); } -void Bu::TcpSocket::setPos( long ) +void Bu::TcpSocket::setPos( Bu::size ) { throw UnsupportedException(); } -void Bu::TcpSocket::setPosEnd( long ) +void Bu::TcpSocket::setPosEnd( Bu::size ) { throw UnsupportedException(); } @@ -401,7 +401,7 @@ void Bu::TcpSocket::setBlocking( bool bBlocking ) fcntl( nTcpSocket, F_SETFL, fcntl( nTcpSocket, F_GETFL, 0 ) | O_NONBLOCK ); } #else - u_long iMode; + u_Bu::size iMode; if( bBlocking ) iMode = 0; else @@ -416,7 +416,7 @@ void Bu::TcpSocket::setBlocking( bool bBlocking ) #endif } -void Bu::TcpSocket::setSize( long ) +void Bu::TcpSocket::setSize( Bu::size ) { } @@ -436,6 +436,8 @@ void Bu::TcpSocket::setAddress() addr.sin_family = AF_INET; bu_getpeername( nTcpSocket, (sockaddr *)(&addr), &len ); sAddress = bu_inet_ntoa( addr.sin_addr ); + + printf("%d\n", IP_MTU ); } Bu::FString Bu::TcpSocket::getAddress() const @@ -448,3 +450,17 @@ Bu::TcpSocket::operator int() const return nTcpSocket; } +Bu::size Bu::TcpSocket::getSize() const +{ + throw UnsupportedException(); +} + +Bu::size Bu::TcpSocket::getBlockSize() const +{ + +} + +Bu::FString Bu::TcpSocket::getLocation() const +{ +} + diff --git a/src/tcpsocket.h b/src/tcpsocket.h index 3361e84..8543ad0 100644 --- a/src/tcpsocket.h +++ b/src/tcpsocket.h @@ -10,6 +10,7 @@ #include +#include "bu/config.h" #include "bu/stream.h" #include "bu/fstring.h" #include "bu/exceptionbase.h" @@ -66,18 +67,18 @@ namespace Bu virtual void close(); //virtual void read(); - virtual size_t read( void *pBuf, size_t nBytes ); - virtual size_t read( void *pBuf, size_t nBytes, + virtual size read( void *pBuf, size nBytes ); + virtual size read( void *pBuf, size nBytes, uint32_t nSec, uint32_t nUSec=0 ); - virtual size_t write( const void *pBuf, size_t nBytes ); - virtual size_t write( const void *pBuf, size_t nBytes, + virtual size write( const void *pBuf, size nBytes ); + virtual size write( const void *pBuf, size nBytes, uint32_t nSec, uint32_t nUSec=0 ); using Stream::write; - virtual long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); + virtual size tell(); + virtual void seek( size offset ); + virtual void setPos( size pos ); + virtual void setPosEnd( size pos ); virtual bool isEos(); virtual bool isOpen(); @@ -93,11 +94,15 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( long iSize ); + virtual void setSize( size iSize ); Bu::FString getAddress() const; operator int() const; + virtual size getSize() const; + virtual size getBlockSize() const; + virtual Bu::FString getLocation() const; + private: void setAddress(); -- cgit v1.2.3