diff options
author | tiptorrent development team <tiptorrent@soleta.eu> | 2021-12-23 18:28:09 +0100 |
---|---|---|
committer | tiptorrent development team <tiptorrent@soleta.eu> | 2021-12-23 18:29:24 +0100 |
commit | 4e2ba97629e0daf378b133f53b0b0d9ad25d155e (patch) | |
tree | fe614d70ed177255b593e34e7633c4423489b87f /src | |
parent | e15e2364f71e3a657f873c1fc4adc3e2411ec340 (diff) |
add helper function to connect and send HTTP request
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 56 |
1 files changed, 33 insertions, 23 deletions
@@ -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; |