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 ++++++++++--------- src/connection.h | 2 +- src/exceptions.h | 8 +++++ src/test/clistress/main.cpp | 39 ++++++++++++++++++++ src/test/srvstress/main.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 src/test/clistress/main.cpp create mode 100644 src/test/srvstress/main.cpp (limited to 'src') 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(); diff --git a/src/connection.h b/src/connection.h index 5e86ff4..b5b121b 100644 --- a/src/connection.h +++ b/src/connection.h @@ -266,7 +266,7 @@ public: * readInput function to control blocking time. *@returns True socket is still connected, otherwise false. */ - bool readInput(); + int readInput(); /** * Reads all pending input from the connection, blocking up to nSec diff --git a/src/exceptions.h b/src/exceptions.h index 27aec3c..b4126b7 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -8,4 +8,12 @@ subExceptionDecl( XmlException ) subExceptionDecl( FileException ) subExceptionDecl( ConnectionException ) +enum eConnectionException +{ + excodeReadError, + excodeBadReadError, + excodeConnectionClosed, + excodeSocketTimeout +}; + #endif diff --git a/src/test/clistress/main.cpp b/src/test/clistress/main.cpp new file mode 100644 index 0000000..3f5038b --- /dev/null +++ b/src/test/clistress/main.cpp @@ -0,0 +1,39 @@ +#include "connection.h" + +void _waitForLength( Connection &con, int len) +{ + int rlen = con.getInputAmnt(); + + if (rlen >= len) + return; + + int time_left = 5; + int mic_left = 0; + + while (rlen < len) + { + if (time_left == 0 && mic_left == 0) + { + throw "Socket Timeout"; + } + con.readInput(time_left, mic_left, &time_left, &mic_left); + rlen = con.getInputAmnt(); + } +} + +int main() +{ + Connection c; + + c.open("localhost", 4001 ); + + c.appendOutput("d"); + c.writeOutput(); + + _waitForLength( c, 40 ); + + c.close(); + + return 0; +} + diff --git a/src/test/srvstress/main.cpp b/src/test/srvstress/main.cpp new file mode 100644 index 0000000..c7795e4 --- /dev/null +++ b/src/test/srvstress/main.cpp @@ -0,0 +1,86 @@ +#include "connectionmanager.h" +#include "programlink.h" +#include "linkedlist.h" +#include "protocol.h" + +class StressProtocol : public Protocol +{ +public: + bool onNewData() + { + switch( getConnection()->getInput()[0] ) + { + case 'd': + throw "Hello"; + break; + }; + + return true; + } + + bool onNewConnection() + { + return true; + } +}; + +class StressMonitor : public ConnectionMonitor, public ProgramLink +{ +public: + bool init() + { + return true; + } + + bool deInit() + { + return true; + } + + bool timeSlice() + { + } + + bool onNewConnection( Connection *pCon, int nPort ) + { + StressProtocol *sp = new StressProtocol(); + pCon->setProtocol( sp ); + + printf(" sys: New connection: socket(%d), port(%d)\n", + pCon->getSocket(), nPort ); + + return true; + } + + bool onClosedConnection( Connection *pCon ) + { + printf(" sys: Closed connection: socket(%d)\n", + pCon->getSocket() ); + + return true; + } + + LinkMessage *processIRM( LinkMessage *pMsg ) + { + return NULL; + } +}; + +int main() +{ + printf("Starting server...\n"); + + ConnectionManager srv; + StressMonitor telnet; + + srv.setConnectionMonitor( &telnet ); + + srv.startServer( 4001 ); + + for(;;) + { + srv.scanConnections( 5000, false ); + } + + return 0; +} -- cgit v1.2.3