diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-02 15:56:17 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-02 15:56:17 +0000 |
commit | cc4604c1e22e01c31d25923ed76d1518693b7faa (patch) | |
tree | d843e1c18882298a7bad38089a24092c9ceda3cb | |
parent | bebc878b054a06ff8e541db695b1e586fff2b022 (diff) | |
download | libbu++-cc4604c1e22e01c31d25923ed76d1518693b7faa.tar.gz libbu++-cc4604c1e22e01c31d25923ed76d1518693b7faa.tar.bz2 libbu++-cc4604c1e22e01c31d25923ed76d1518693b7faa.tar.xz libbu++-cc4604c1e22e01c31d25923ed76d1518693b7faa.zip |
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.
Diffstat (limited to '')
-rw-r--r-- | src/connection.cpp | 21 | ||||
-rw-r--r-- | 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 @@ | |||
11 | #include <netdb.h> | 11 | #include <netdb.h> |
12 | #include <arpa/inet.h> | 12 | #include <arpa/inet.h> |
13 | #include <errno.h> | 13 | #include <errno.h> |
14 | #include <fcntl.h> | ||
14 | #include "exceptions.h" | 15 | #include "exceptions.h" |
15 | 16 | ||
16 | // Read buffer size...maybe fix wierd issues... | 17 | // Read buffer size...maybe fix wierd issues... |
@@ -137,10 +138,12 @@ bool Connection::isActive() | |||
137 | 138 | ||
138 | void Connection::close() | 139 | void Connection::close() |
139 | { | 140 | { |
141 | //printf("Close called, socket is: %s\n", bActive?"Active":"Inactive" ); | ||
140 | if( bActive ) | 142 | if( bActive ) |
141 | { | 143 | { |
142 | fsync( nSocket ); | 144 | fsync( nSocket ); |
143 | ::close( nSocket ); | 145 | ::close( nSocket ); |
146 | //printf("Socket closed.\n"); | ||
144 | } | 147 | } |
145 | bActive = false; | 148 | bActive = false; |
146 | //nSocket = -1; | 149 | //nSocket = -1; |
@@ -175,8 +178,18 @@ bool Connection::open( const char *sAddr, int nPort ) | |||
175 | bActive = false; | 178 | bActive = false; |
176 | return false; | 179 | return false; |
177 | } | 180 | } |
181 | |||
182 | // These lines set the socket to non-blocking, a good thing? | ||
183 | int flags; | ||
184 | flags = fcntl(nSocket, F_GETFL, 0); | ||
185 | flags |= O_NONBLOCK; | ||
186 | if (fcntl(nSocket, F_SETFL, flags) < 0) | ||
187 | { | ||
188 | return false; | ||
189 | } | ||
178 | 190 | ||
179 | /* Connect to the server. */ | 191 | /* Connect to the server. */ |
192 | //printf("Resolving hostname (%s)...\n", sAddr ); | ||
180 | { | 193 | { |
181 | struct hostent *hostinfo; | 194 | struct hostent *hostinfo; |
182 | 195 | ||
@@ -190,16 +203,20 @@ bool Connection::open( const char *sAddr, int nPort ) | |||
190 | xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; | 203 | xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; |
191 | } | 204 | } |
192 | 205 | ||
206 | //printf("Making actual connection..."); | ||
207 | fflush( stdout ); | ||
193 | int ret = connect( | 208 | int ret = connect( |
194 | nSocket, | 209 | nSocket, |
195 | (struct sockaddr *)&xServerName, | 210 | (struct sockaddr *)&xServerName, |
196 | sizeof(xServerName) | 211 | sizeof(xServerName) |
197 | ); | 212 | ); |
213 | //printf("Connected.\n"); | ||
198 | 214 | ||
215 | /* | ||
199 | if( ret < 0 ) | 216 | if( ret < 0 ) |
200 | { | 217 | { |
201 | return false; | 218 | return false; |
202 | } | 219 | }*/ |
203 | 220 | ||
204 | bActive = true; | 221 | bActive = true; |
205 | bDisconnectMe = false; | 222 | bDisconnectMe = false; |
@@ -220,7 +237,7 @@ int Connection::readInput() | |||
220 | nbytes = read( nSocket, buffer, RBS ); | 237 | nbytes = read( nSocket, buffer, RBS ); |
221 | if( nbytes < 0 && errno != 0 && errno != EAGAIN ) | 238 | if( nbytes < 0 && errno != 0 && errno != EAGAIN ) |
222 | { | 239 | { |
223 | printf("errno: %d, %s\n", errno, strerror( errno ) ); | 240 | //printf("errno: %d, %s\n", errno, strerror( errno ) ); |
224 | /* Read error. */ | 241 | /* Read error. */ |
225 | //perror("readInput"); | 242 | //perror("readInput"); |
226 | throw ConnectionException( excodeReadError, "Read error: %s", strerror( errno ) ); | 243 | 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() | |||
156 | //printf("Connection content:\n\n%s\n\n", sData.c_str() ); | 156 | //printf("Connection content:\n\n%s\n\n", sData.c_str() ); |
157 | 157 | ||
158 | Connection con; | 158 | Connection con; |
159 | //printf("Opening connection...\n"); | ||
159 | con.open( sHost.c_str(), nPort ); | 160 | con.open( sHost.c_str(), nPort ); |
160 | //printf("Connected, sending request.\n"); | ||
161 | { | 161 | { |
162 | int nSocket = con.getSocket(); | 162 | int nSocket = con.getSocket(); |
163 | fd_set rfds, wfds, efds; | 163 | fd_set rfds, wfds, efds; |
@@ -170,50 +170,74 @@ SBuffer *HttpGet::get() | |||
170 | FD_ZERO(&efds); | 170 | FD_ZERO(&efds); |
171 | FD_SET(nSocket, &efds); | 171 | FD_SET(nSocket, &efds); |
172 | 172 | ||
173 | retval = select( nSocket+1, &rfds, &wfds, &efds, NULL ); | 173 | struct timeval tv; |
174 | printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n", | 174 | tv.tv_sec = 4; |
175 | tv.tv_usec = 0; | ||
176 | |||
177 | //printf("Selecting on socket, can we read, write, etc?\n"); | ||
178 | retval = select( nSocket+1, &rfds, &wfds, &efds, &tv ); | ||
179 | /*printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n", | ||
175 | nSocket, | 180 | nSocket, |
176 | FD_ISSET( nSocket, &rfds ), | 181 | FD_ISSET( nSocket, &rfds ), |
177 | FD_ISSET( nSocket, &wfds ), | 182 | FD_ISSET( nSocket, &wfds ), |
178 | FD_ISSET( nSocket, &efds ), | 183 | FD_ISSET( nSocket, &efds ), |
179 | retval | 184 | retval |
180 | ); | 185 | );*/ |
186 | |||
187 | if( retval == 0 ) | ||
188 | { | ||
189 | //printf("Timeout on connection.\n"); | ||
190 | con.close(); | ||
191 | throw ExceptionBase("Connection Timeout on open.\n"); | ||
192 | } | ||
181 | 193 | ||
182 | } | 194 | } |
183 | con.appendOutput( sData.c_str(), sData.size() ); | 195 | con.appendOutput( sData.c_str(), sData.size() ); |
196 | //printf("Writing to socket...\n"); | ||
184 | con.writeOutput(); | 197 | con.writeOutput(); |
198 | //printf("Data written...\n"); | ||
185 | int nSec = 5; | 199 | int nSec = 5; |
186 | int nUSec = 0; | 200 | int nUSec = 0; |
187 | int nLastAmnt = con.getInputAmnt(); | 201 | int nLastAmnt = con.getInputAmnt(); |
188 | try | 202 | try |
189 | { | 203 | { |
190 | double dTotTime = 0.0; | 204 | double dTotTime = 0.0; |
191 | while( con.readInput( nSec, nUSec, &nSec, &nUSec ) ) | 205 | //printf("About to read input...\n"); |
192 | { | 206 | while( con.readInput( nSec, nUSec, &nSec, &nUSec ) ) |
193 | if( nLastAmnt == con.getInputAmnt() ) | ||
194 | { | ||
195 | if( nSec <= 0 && nUSec <= 0 ) | ||
196 | throw ExceptionBase("Connection Timeout.\n"); | ||
197 | if( nSec == 5 && nUSec == 0 ) | ||
198 | break; | ||
199 | } | ||
200 | else | ||
201 | { | 207 | { |
202 | dTotTime += (5.0-(nSec+nUSec/1000000.0)); | 208 | if( nLastAmnt == con.getInputAmnt() ) |
203 | printf("\rRead %db at %.2fkb/sec", | 209 | { |
204 | con.getInputAmnt(), | 210 | if( nSec <= 0 && nUSec <= 0 ) |
205 | ((double)(con.getInputAmnt())/1024.0) / dTotTime | 211 | { |
206 | ); | 212 | //printf("out of time, closing up.\n"); |
207 | fflush( stdout ); | 213 | con.close(); |
208 | nSec = 5; | 214 | throw ExceptionBase("Connection Timeout.\n"); |
209 | nUSec = 0; | 215 | } |
210 | nLastAmnt = con.getInputAmnt(); | 216 | if( nSec == 5 && nUSec == 0 ) |
217 | { | ||
218 | //printf("No new data, breaking.\n"); | ||
219 | break; | ||
220 | } | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | dTotTime += (5.0-(nSec+nUSec/1000000.0)); | ||
225 | printf("\rRead %db at %.2fkb/sec", | ||
226 | con.getInputAmnt(), | ||
227 | ((double)(con.getInputAmnt())/1024.0) / dTotTime | ||
228 | ); | ||
229 | fflush( stdout ); | ||
230 | nSec = 5; | ||
231 | nUSec = 0; | ||
232 | nLastAmnt = con.getInputAmnt(); | ||
233 | } | ||
211 | } | 234 | } |
212 | } | 235 | } |
213 | } | ||
214 | catch( ConnectionException &e ) | 236 | catch( ConnectionException &e ) |
215 | { | 237 | { |
216 | printf("ConnectionException: %s\n", e.what() ); | 238 | //con.close(); |
239 | if( strcmp( e.what(), "Connection closed" ) ) | ||
240 | printf("\nConnectionException: %s\n", e.what() ); | ||
217 | } | 241 | } |
218 | 242 | ||
219 | int total = con.getInputAmnt(); | 243 | int total = con.getInputAmnt(); |
@@ -232,6 +256,8 @@ SBuffer *HttpGet::get() | |||
232 | } | 256 | } |
233 | con.close(); | 257 | con.close(); |
234 | 258 | ||
259 | //printf("\n\n%s\n\n", dat ); | ||
260 | |||
235 | throw ExceptionBase("Something went wrong, incomplete response? fix this.\n"); | 261 | throw ExceptionBase("Something went wrong, incomplete response? fix this.\n"); |
236 | } | 262 | } |
237 | 263 | ||