From 0a700ced28520be170c0965191f2450a2e4a82ac Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 25 Aug 2006 20:54:47 +0000 Subject: Added tests and exception codes, so you're program can tell just how bad things really are. --- src/connection.cpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/connection.cpp') diff --git a/src/connection.cpp b/src/connection.cpp index 0e7f111..045ea17 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -196,7 +196,7 @@ bool Connection::open( const char *sAddr, int nPort ) return true; } -bool Connection::readInput() +int Connection::readInput() { char buffer[2048]; int nbytes; @@ -204,39 +204,33 @@ bool Connection::readInput() for(;;) { - memset( buffer, 0, 2048 ); + //memset( buffer, 0, 2048 ); nbytes = read( nSocket, buffer, 2048 ); if (nbytes < 0) { /* Read error. */ //perror("readInput"); - return false; - } - else if (nbytes == 0) - { - /* End-of-file. */ - //perror("readInput"); - return false; + throw ConnectionException( excodeReadError, "Read error"); } else { nTotalRead += nbytes; appendInput( buffer, nbytes ); /* Data read. */ - if( nbytes < 2047 ) + if( nbytes < 2048 ) { - if( pProtocol != NULL && nTotalRead > 0 ) - { - pProtocol->onNewData(); - } - - return true; + break; } } } - return true; + if( pProtocol != NULL && nTotalRead > 0 ) + { + pProtocol->onNewData(); + } + + return nTotalRead; } bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack ) @@ -259,12 +253,17 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack if( retval == -1 ) { // Oh my god!!! some kind of horrible problem!!!! + throw ConnectionException( excodeBadReadError, "Bad Read error"); return false; } else if( retval ) { // None of them have data, but the connection is still active. - return readInput(); + if( readInput() == 0 ) + { + this->close(); + throw ConnectionException( excodeConnectionClosed, "Connection closed"); + } } else { @@ -283,7 +282,7 @@ void Connection::waitForInput( int nBytesIn, int nSec, int nUSec ) { if( nSec == 0 && nUSec == 0 ) { - throw ConnectionException("Socket Timeout"); + throw ConnectionException( excodeSocketTimeout, "Socket Timeout"); } readInput( nSec, nUSec, &nSec, &nUSec ); rlen = getInputAmnt(); -- cgit v1.2.3