From 34d3d2846a69a1ae8c108e57e2539ac461b19328 Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 1 Jul 2006 22:51:40 +0000 Subject: [PATCH] better reconnect-logic --- record-dvb.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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) { -- 2.39.2