{
struct timeval start, curr;
int bytes, recvd, written, count = 0;
+ int error_sleep = 0;
char buffer[CHUNKSIZE];
int in, out;
curr = start;
do {
- if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
- /* TODO: Insert better connection-loss recovery here */
+ if (error_sleep) {
+ sleep(error_sleep);
+ printf("Reconnecting... ");
if ((in = (*open_fn)(url)) < 0) {
- sleep(1);
+ if (error_sleep < 60)
+ error_sleep *= 2;
+
+ printf("failed\n");
continue;
+ } else {
+ printf("succeeded\n");
+ error_sleep = 0;
}
}
+
+ if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
+ error_sleep = 1;
+ continue;
+ }
written = 0;
do {
if ((bytes = write(out, buffer, recvd-written)) < 0) {