- if ((bytes = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
- /* TODO: Insert better connection-loss recovery here */
- if ((in = (*open_fn)(url)) < 0)
- sleep(1);
+ if (error_sleep) {
+ sleep(error_sleep);
+ printf("Reconnecting... ");
+ if ((in = (*open_fn)(url)) < 0) {
+ if (error_sleep < MAX_ERROR_SLEEP)
+ error_sleep *= 2;
+
+ if (error_sleep > MAX_ERROR_SLEEP)
+ error_sleep = MAX_ERROR_SLEEP;
+
+ printf("failed\n");
+ continue;
+ } else {
+ printf("succeeded\n");
+ error_sleep = 0;
+ }
+ }
+
+ FD_ZERO(&rfds);
+ FD_SET(in, &rfds);
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ if ((retval = select(in + 1, &rfds, NULL, NULL, &tv)) == -1) {
+ error_sleep = 1;
+ continue;
+ }
+
+ if (!retval) {
+ gettimeofday(&curr, NULL);
+ continue;
+ }
+
+ if ((recvd = recv(in, buffer, CHUNKSIZE, 0)) < 1) {
+ error_sleep = 1;
+ continue;