From a8e7b44c9981dd5d65b0b97f1d2ee34927a0f2a5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 2 Apr 2007 17:35:41 +0000 Subject: Ok...now you can specify a timeout on the open function, 30 seconds is the default so no programs need to be changed, it seemed like a good default to me... Still needs testing, but it should work just fine, and shouldn't effect any of our servers. --- src/connection.cpp | 38 ++++++++++++++++++++++++++++++++------ src/connection.h | 2 +- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index 9d8b02c..efef144 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -165,7 +165,7 @@ bool Connection::open( int nNewSocket ) return true; } -bool Connection::open( const char *sAddr, int nPort ) +bool Connection::open( const char *sAddr, int nPort, int nSec ) { struct sockaddr_in xServerName; bActive = false; @@ -204,13 +204,42 @@ bool Connection::open( const char *sAddr, int nPort ) } //printf("Making actual connection..."); - fflush( stdout ); - int ret = connect( + //fflush( stdout ); + connect( nSocket, (struct sockaddr *)&xServerName, sizeof(xServerName) ); //printf("Connected.\n"); + + bActive = true; + bDisconnectMe = false; + + if( nSec > 0 ) + { + fd_set rfds, wfds, efds; + int retval; + + FD_ZERO(&rfds); + FD_SET(nSocket, &rfds); + FD_ZERO(&wfds); + FD_SET(nSocket, &wfds); + FD_ZERO(&efds); + FD_SET(nSocket, &efds); + + struct timeval tv; + tv.tv_sec = nSec; + tv.tv_usec = 0; + + retval = select( nSocket+1, &rfds, &wfds, &efds, &tv ); + + if( retval == 0 ) + { + close(); + throw ExceptionBase("Connection timeout.\n"); + } + + } /* if( ret < 0 ) @@ -218,9 +247,6 @@ bool Connection::open( const char *sAddr, int nPort ) return false; }*/ - bActive = true; - bDisconnectMe = false; - return true; } diff --git a/src/connection.h b/src/connection.h index 7e1141d..0e991c7 100644 --- a/src/connection.h +++ b/src/connection.h @@ -49,7 +49,7 @@ public: *@todo Make this function add log entries to a standard MultiLog if * something goes wrong. */ - bool open( const char *sAddr, int nPort ); + bool open( const char *sAddr, int nPort, int nSec=30 ); void ensureCapacity( int nSize ); -- cgit v1.2.3