From 4e2ba97629e0daf378b133f53b0b0d9ad25d155e Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Thu, 23 Dec 2021 18:28:09 +0100 Subject: add helper function to connect and send HTTP request --- src/main.c | 56 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 5e0d396..8678cf6 100644 --- a/src/main.c +++ b/src/main.c @@ -418,6 +418,33 @@ static int tip_client_connect(const char *addr) return 0; } +#define MAX_RETRIES 5 +#define WAIT_RETRY 5 /* wait 5 seconds before retrying. */ + +static int tip_client_request_file(struct tip_client *cli, + const char *server, const char *filename) +{ + tip_client_connect(server); + + while (cli->state != TIP_CLIENT_DONE && !cli->error) + ev_loop(tip_main_loop, 0); + + if (cli->error) { + syslog(LOG_ERR, "Failed to fetch file %s\n", filename); + sleep(WAIT_RETRY); + if (cli->num_retries++ >= MAX_RETRIES) { + syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n", + MAX_RETRIES); + return -1; + } + cli->error = false; + return 1; + } + + return 0; +} + + static uint32_t select_file_chunk(bool *file_chunk) { struct timeval tv; @@ -442,9 +469,6 @@ static uint32_t select_file_chunk(bool *file_chunk) static char _filename[PATH_MAX + 1]; -#define MAX_RETRIES 5 -#define WAIT_RETRY 5 /* wait 5 seconds before retrying. */ - int main(int argc, char *argv[]) { struct timeval tv_start, tv_stop, tv; @@ -472,26 +496,12 @@ int main(int argc, char *argv[]) k = select_file_chunk(file_chunk); snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k); filename = _filename; -retry: - syslog(LOG_INFO, "Requesting file %s to server\n", filename); - - tip_client_connect(argv[1]); - _cli.state = TIP_CLIENT_GET_HEADER; - - while (_cli.state != TIP_CLIENT_DONE && !_cli.error) - ev_loop(tip_main_loop, 0); - - if (_cli.error) { - syslog(LOG_ERR, "Failed to fetch file %s\n", filename); - sleep(WAIT_RETRY); - if (_cli.num_retries++ >= MAX_RETRIES) { - syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n", - MAX_RETRIES); - break; - } - _cli.error = false; - goto retry; - } + + do { + syslog(LOG_INFO, "Requesting file %s to server\n", filename); + _cli.state = TIP_CLIENT_GET_HEADER; + + } while (tip_client_request_file(&_cli, addr, filename) > 0); tip_client_progress(&_cli, true); file_chunk[k] = true; -- cgit v1.2.3-18-g5258