add helper function to connect and send HTTP request

master
tiptorrent development team 2021-12-23 18:28:09 +01:00
parent e15e2364f7
commit 4e2ba97629
1 changed files with 31 additions and 21 deletions

View File

@ -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;
do {
syslog(LOG_INFO, "Requesting file %s to server\n", filename);
_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;
}
} while (tip_client_request_file(&_cli, addr, filename) > 0);
tip_client_progress(&_cli, true);
file_chunk[k] = true;