aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-08-25 20:24:31 +0000
committerMike Buland <eichlan@xagasoft.com>2006-08-25 20:24:31 +0000
commit0c2d075e795858779af102e932a881498e2268ae (patch)
treee4cab130b72997bd76af1cde4d9bbe3f5de343d3
parentec1a4f20eace44b6ffde4c1eacdecf3938942cd4 (diff)
downloadlibbu++-0c2d075e795858779af102e932a881498e2268ae.tar.gz
libbu++-0c2d075e795858779af102e932a881498e2268ae.tar.bz2
libbu++-0c2d075e795858779af102e932a881498e2268ae.tar.xz
libbu++-0c2d075e795858779af102e932a881498e2268ae.zip
Added a new exception for connecitons, and added a new function to the
connection class, that reads at least n bytes in x seconds.
-rw-r--r--src/connection.cpp24
-rw-r--r--src/connection.h12
-rw-r--r--src/exceptions.cpp1
-rw-r--r--src/exceptions.h1
4 files changed, 35 insertions, 3 deletions
diff --git a/src/connection.cpp b/src/connection.cpp
index 5b79dfe..0e7f111 100644
--- a/src/connection.cpp
+++ b/src/connection.cpp
@@ -9,6 +9,7 @@
9#include <netinet/in.h> 9#include <netinet/in.h>
10#include <netdb.h> 10#include <netdb.h>
11#include <arpa/inet.h> 11#include <arpa/inet.h>
12#include "exceptions.h"
12 13
13Connection::Connection() 14Connection::Connection()
14{ 15{
@@ -244,10 +245,9 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
244 struct timeval tv; 245 struct timeval tv;
245 int retval; 246 int retval;
246 247
247 /* Watch stdin (fd 0) to see when it has input. */
248 FD_ZERO(&rfds); 248 FD_ZERO(&rfds);
249 FD_SET(nSocket, &rfds); 249 FD_SET(nSocket, &rfds);
250 /* Wait up to five seconds. */ 250
251 tv.tv_sec = nSec; 251 tv.tv_sec = nSec;
252 tv.tv_usec = nUSec; 252 tv.tv_usec = nUSec;
253 253
@@ -256,7 +256,7 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
256 if( pnSecBack ) (*pnSecBack) = tv.tv_sec; 256 if( pnSecBack ) (*pnSecBack) = tv.tv_sec;
257 if( pnUSecBack ) (*pnUSecBack) = tv.tv_usec; 257 if( pnUSecBack ) (*pnUSecBack) = tv.tv_usec;
258 258
259 if (retval == -1) 259 if( retval == -1 )
260 { 260 {
261 // Oh my god!!! some kind of horrible problem!!!! 261 // Oh my god!!! some kind of horrible problem!!!!
262 return false; 262 return false;
@@ -272,6 +272,24 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
272 } 272 }
273} 273}
274 274
275void Connection::waitForInput( int nBytesIn, int nSec, int nUSec )
276{
277 int rlen = getInputAmnt();
278
279 if( rlen >= nBytesIn )
280 return;
281
282 while( rlen < nBytesIn )
283 {
284 if( nSec == 0 && nUSec == 0 )
285 {
286 throw ConnectionException("Socket Timeout");
287 }
288 readInput( nSec, nUSec, &nSec, &nUSec );
289 rlen = getInputAmnt();
290 }
291}
292
275bool Connection::clearOutput() 293bool Connection::clearOutput()
276{ 294{
277 return xOutputBuf.clearData(); 295 return xOutputBuf.clearData();
diff --git a/src/connection.h b/src/connection.h
index 29fcc25..5e86ff4 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -283,6 +283,18 @@ public:
283 */ 283 */
284 bool readInput( int nSec, int nUSec, int *pnSecBack=NULL, int *pnUSecBack=NULL ); 284 bool readInput( int nSec, int nUSec, int *pnSecBack=NULL, int *pnUSecBack=NULL );
285 285
286 /**
287 * Waits until at least nBytesIn are read into the input buffer and ready
288 * to be used. Wait at most nSec seconds plus nUSec micro seconds.
289 * If the timeout is exceeded, this function throws an exception. If this
290 * function returns normally, you are guranteed to have at least nBytesIn
291 * bytes in your input buffer.
292 *@param nBytesIn Number of bytes to read.
293 *@param nSec The max seconds to wait.
294 *@param sUSec The max microseconds to wait.
295 */
296 void waitForInput( int nBytesIn, int nSec, int nUSec );
297
286 /** Writes all data that is pending to the socket. 298 /** Writes all data that is pending to the socket.
287 *@returns True if all data was written succesfully, false otherwise. 299 *@returns True if all data was written succesfully, false otherwise.
288 */ 300 */
diff --git a/src/exceptions.cpp b/src/exceptions.cpp
index 8d30296..9696af8 100644
--- a/src/exceptions.cpp
+++ b/src/exceptions.cpp
@@ -3,3 +3,4 @@
3 3
4subExceptionDef( XmlException ) 4subExceptionDef( XmlException )
5subExceptionDef( FileException ) 5subExceptionDef( FileException )
6subExceptionDef( ConnectionException )
diff --git a/src/exceptions.h b/src/exceptions.h
index 3bf2e0b..27aec3c 100644
--- a/src/exceptions.h
+++ b/src/exceptions.h
@@ -6,5 +6,6 @@
6 6
7subExceptionDecl( XmlException ) 7subExceptionDecl( XmlException )
8subExceptionDecl( FileException ) 8subExceptionDecl( FileException )
9subExceptionDecl( ConnectionException )
9 10
10#endif 11#endif