From: michael Date: Sat, 1 Jul 2006 22:51:40 +0000 (+0000) Subject: better reconnect-logic X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/record-dvb/commitdiff_plain/34d3d2846a69a1ae8c108e57e2539ac461b19328 better reconnect-logic --- diff --git a/record-dvb.c b/record-dvb.c index 02fcd51..38609a4 100644 --- a/record-dvb.c +++ b/record-dvb.c @@ -20,6 +20,7 @@ void record(int(*open_fn)(char *), char *url, char *outfile, int duration) { struct timeval start, curr; int bytes, recvd, written, count = 0; + int error_sleep = 0; char buffer[CHUNKSIZE]; int in, out; @@ -39,13 +40,25 @@ void record(int(*open_fn)(char *), char *url, char *outfile, int duration) 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) {