From 22914644fb62bc1a9d49eec50a10f2870dde1d0b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 May 2009 16:23:38 +0000 Subject: Added some helpers to the Bu::Cache::Ptr, you can clear them now, and cast to bool to see if they're bound, you still have to use isValid to see if they're valid or not. Also, fixed a bug in libbu++ that's been around for a while, apparently, in the Bu::Socket code. Non-blocking reading wasn't working correctly, when data wasn't waiting on the line, it would return immediately with zero bytes read. That was sure stupid. This should fix a lot of things. --- src/cache.h | 16 ++++++++++++++++ src/socket.cpp | 12 ++++++++---- src/socket.h | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/cache.h b/src/cache.h index 97ed140..c4e1b0b 100644 --- a/src/cache.h +++ b/src/cache.h @@ -100,6 +100,11 @@ namespace Bu return pData != NULL; } + operator bool() const + { + return isBound() && isValid(); + } + const keytype &getKey() const { return kId; @@ -112,6 +117,17 @@ namespace Bu pData = NULL; } + void clear() + { + unbind(); + pCache = NULL; + } + + void unset() + { + clear(); + } + Ptr &operator=( const Ptr &rRhs ) { if( pCache && pData ) diff --git a/src/socket.cpp b/src/socket.cpp index c01c63a..46e6cd7 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -34,7 +34,8 @@ namespace Bu { subExceptionDef( SocketException ) } Bu::Socket::Socket( int nSocket ) : nSocket( nSocket ), - bActive( true ) + bActive( true ), + bBlocking( true ) { #ifdef WIN32 Bu::Winsock2::getInstance(); @@ -42,12 +43,14 @@ Bu::Socket::Socket( int nSocket ) : setAddress(); } -Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) +Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) : + nSocket( 0 ), + bActive( false ), + bBlocking( true ) { #ifdef WIN32 Bu::Winsock2::getInstance(); #endif - bActive = false; /* Create the socket. */ nSocket = bu_socket( PF_INET, SOCK_STREAM, 0 ); @@ -150,7 +153,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) struct timeval tv = {0, 0}; if( bu_select( nSocket+1, &rfds, NULL, NULL, &tv ) < 0 ) throw SocketException( SocketException::cRead, strerror(errno) ); - if( FD_ISSET( nSocket, &rfds ) ) + if( FD_ISSET( nSocket, &rfds ) || bBlocking ) { int nRead = TEMP_FAILURE_RETRY( bu_recv( nSocket, (char *) pBuf, nBytes, 0 ) ); @@ -377,6 +380,7 @@ bool Bu::Socket::isBlocking() void Bu::Socket::setBlocking( bool bBlocking ) { + this->bBlocking = bBlocking; #ifndef WIN32 if( bBlocking ) { diff --git a/src/socket.h b/src/socket.h index f12f79c..bef0e9c 100644 --- a/src/socket.h +++ b/src/socket.h @@ -104,6 +104,7 @@ namespace Bu int nSocket; #endif bool bActive; + bool bBlocking; FString sReadBuf; FString sAddress; }; -- cgit v1.2.3