From cc4604c1e22e01c31d25923ed76d1518693b7faa Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 2 Apr 2007 15:56:17 +0000 Subject: This constitutes a test version...there's a chance this should have been a branch, but it's so small...I'll just see if bugs arise, and if they do, then we'll fix 'em. --- src/connection.cpp | 21 +++++++++++++-- src/httpget.cpp | 76 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index f042705..9d8b02c 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "exceptions.h" // Read buffer size...maybe fix wierd issues... @@ -137,10 +138,12 @@ bool Connection::isActive() void Connection::close() { + //printf("Close called, socket is: %s\n", bActive?"Active":"Inactive" ); if( bActive ) { fsync( nSocket ); ::close( nSocket ); + //printf("Socket closed.\n"); } bActive = false; //nSocket = -1; @@ -175,8 +178,18 @@ bool Connection::open( const char *sAddr, int nPort ) bActive = false; return false; } + + // These lines set the socket to non-blocking, a good thing? + int flags; + flags = fcntl(nSocket, F_GETFL, 0); + flags |= O_NONBLOCK; + if (fcntl(nSocket, F_SETFL, flags) < 0) + { + return false; + } /* Connect to the server. */ + //printf("Resolving hostname (%s)...\n", sAddr ); { struct hostent *hostinfo; @@ -190,16 +203,20 @@ bool Connection::open( const char *sAddr, int nPort ) xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; } + //printf("Making actual connection..."); + fflush( stdout ); int ret = connect( nSocket, (struct sockaddr *)&xServerName, sizeof(xServerName) ); + //printf("Connected.\n"); + /* if( ret < 0 ) { return false; - } + }*/ bActive = true; bDisconnectMe = false; @@ -220,7 +237,7 @@ int Connection::readInput() nbytes = read( nSocket, buffer, RBS ); if( nbytes < 0 && errno != 0 && errno != EAGAIN ) { - printf("errno: %d, %s\n", errno, strerror( errno ) ); + //printf("errno: %d, %s\n", errno, strerror( errno ) ); /* Read error. */ //perror("readInput"); throw ConnectionException( excodeReadError, "Read error: %s", strerror( errno ) ); diff --git a/src/httpget.cpp b/src/httpget.cpp index 529d814..ee1f29c 100644 --- a/src/httpget.cpp +++ b/src/httpget.cpp @@ -156,8 +156,8 @@ SBuffer *HttpGet::get() //printf("Connection content:\n\n%s\n\n", sData.c_str() ); Connection con; + //printf("Opening connection...\n"); con.open( sHost.c_str(), nPort ); - //printf("Connected, sending request.\n"); { int nSocket = con.getSocket(); fd_set rfds, wfds, efds; @@ -170,50 +170,74 @@ SBuffer *HttpGet::get() FD_ZERO(&efds); FD_SET(nSocket, &efds); - retval = select( nSocket+1, &rfds, &wfds, &efds, NULL ); - printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n", + struct timeval tv; + tv.tv_sec = 4; + tv.tv_usec = 0; + + //printf("Selecting on socket, can we read, write, etc?\n"); + retval = select( nSocket+1, &rfds, &wfds, &efds, &tv ); + /*printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n", nSocket, FD_ISSET( nSocket, &rfds ), FD_ISSET( nSocket, &wfds ), FD_ISSET( nSocket, &efds ), retval - ); + );*/ + + if( retval == 0 ) + { + //printf("Timeout on connection.\n"); + con.close(); + throw ExceptionBase("Connection Timeout on open.\n"); + } } con.appendOutput( sData.c_str(), sData.size() ); + //printf("Writing to socket...\n"); con.writeOutput(); + //printf("Data written...\n"); int nSec = 5; int nUSec = 0; int nLastAmnt = con.getInputAmnt(); try { double dTotTime = 0.0; - while( con.readInput( nSec, nUSec, &nSec, &nUSec ) ) - { - if( nLastAmnt == con.getInputAmnt() ) - { - if( nSec <= 0 && nUSec <= 0 ) - throw ExceptionBase("Connection Timeout.\n"); - if( nSec == 5 && nUSec == 0 ) - break; - } - else + //printf("About to read input...\n"); + while( con.readInput( nSec, nUSec, &nSec, &nUSec ) ) { - dTotTime += (5.0-(nSec+nUSec/1000000.0)); - printf("\rRead %db at %.2fkb/sec", - con.getInputAmnt(), - ((double)(con.getInputAmnt())/1024.0) / dTotTime - ); - fflush( stdout ); - nSec = 5; - nUSec = 0; - nLastAmnt = con.getInputAmnt(); + if( nLastAmnt == con.getInputAmnt() ) + { + if( nSec <= 0 && nUSec <= 0 ) + { + //printf("out of time, closing up.\n"); + con.close(); + throw ExceptionBase("Connection Timeout.\n"); + } + if( nSec == 5 && nUSec == 0 ) + { + //printf("No new data, breaking.\n"); + break; + } + } + else + { + dTotTime += (5.0-(nSec+nUSec/1000000.0)); + printf("\rRead %db at %.2fkb/sec", + con.getInputAmnt(), + ((double)(con.getInputAmnt())/1024.0) / dTotTime + ); + fflush( stdout ); + nSec = 5; + nUSec = 0; + nLastAmnt = con.getInputAmnt(); + } } } - } catch( ConnectionException &e ) { - printf("ConnectionException: %s\n", e.what() ); + //con.close(); + if( strcmp( e.what(), "Connection closed" ) ) + printf("\nConnectionException: %s\n", e.what() ); } int total = con.getInputAmnt(); @@ -232,6 +256,8 @@ SBuffer *HttpGet::get() } con.close(); + //printf("\n\n%s\n\n", dat ); + throw ExceptionBase("Something went wrong, incomplete response? fix this.\n"); } -- cgit v1.2.3