From 4a2cacc7975b635b32c7cd7c6ac274639477ed8c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 26 Sep 2006 19:53:41 +0000 Subject: Fixed some whackiness in the connection class, -1 from readInput means nothing there, but there might be later. 0 means death. --- src/connection.cpp | 8 +++++--- src/test/connect/main.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/test/connect/main.cpp diff --git a/src/connection.cpp b/src/connection.cpp index d66ff97..3d3c094 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "exceptions.h" Connection::Connection() @@ -208,15 +209,16 @@ int Connection::readInput() //memset( buffer, 0, 2048 ); nbytes = read( nSocket, buffer, 2048 ); - if (nbytes < 0) + if( nbytes < 0 && errno != 0 && errno != EAGAIN ) { + printf("errno: %d, %s\n", errno, strerror( errno ) ); /* Read error. */ //perror("readInput"); - throw ConnectionException( excodeReadError, "Read error"); + throw ConnectionException( excodeReadError, "Read error: %s", strerror( errno ) ); } else { - if( nbytes == 0 ) + if( nbytes <= 0 ) break; nTotalRead += nbytes; appendInput( buffer, nbytes ); diff --git a/src/test/connect/main.cpp b/src/test/connect/main.cpp new file mode 100644 index 0000000..a9fca64 --- /dev/null +++ b/src/test/connect/main.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include "connection.h" + +int main() +{ + Connection c; + c.open("127.0.0.1", 12457 ); + + { + int newSocket = c.getSocket(); + int flags; + + flags = fcntl(newSocket, F_GETFL, 0); + flags |= O_NONBLOCK; + if (fcntl(newSocket, F_SETFL, flags) < 0) + { + return false; + } + } + + for( int i = 0; i < 50; i++ ) + { + usleep( 100000 ); + int nbytes = c.readInput(); + if( nbytes == 0 ) + printf("0 bytes, EOF?\n"); + else + printf("Got %d bytes, whacky...\n", nbytes ); + } + + c.close(); + + return 0; +} + -- cgit v1.2.3