diff options
-rw-r--r-- | src/connection.cpp | 24 | ||||
-rw-r--r-- | src/connection.h | 12 | ||||
-rw-r--r-- | src/exceptions.cpp | 1 | ||||
-rw-r--r-- | src/exceptions.h | 1 |
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 | ||
13 | Connection::Connection() | 14 | Connection::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 | ||
275 | void 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 | |||
275 | bool Connection::clearOutput() | 293 | bool 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 | ||
4 | subExceptionDef( XmlException ) | 4 | subExceptionDef( XmlException ) |
5 | subExceptionDef( FileException ) | 5 | subExceptionDef( FileException ) |
6 | subExceptionDef( 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 | ||
7 | subExceptionDecl( XmlException ) | 7 | subExceptionDecl( XmlException ) |
8 | subExceptionDecl( FileException ) | 8 | subExceptionDecl( FileException ) |
9 | subExceptionDecl( ConnectionException ) | ||
9 | 10 | ||
10 | #endif | 11 | #endif |