summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortiptorrent development team <tiptorrent@soleta.eu>2022-02-08 16:19:01 +0100
committertiptorrent development team <tiptorrent@soleta.eu>2022-02-08 16:24:15 +0100
commitace36ad51010129b172cf68cea9e9df28e4f92aa (patch)
tree66617c409d3ada2404c528355e765f896f55ffaf
parent93c13ba67cf3ab43ffe1cc154caa802890f3a5f6 (diff)
clean up state on each retry
Reset the client object after retrying connection to download file.
-rw-r--r--src/main.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index 3d6f50a..6b2b8c0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -525,6 +525,14 @@ static uint32_t select_file_chunk(bool *file_chunk)
return k;
}
+static void tip_client_reset_state(struct tip_client *cli, int fd,
+ uint64_t chunk_offset)
+{
+ memset(cli, 0, sizeof(*cli));
+ cli->chunk_offset = chunk_offset;
+ cli->fd = fd;
+}
+
static char _filename[PATH_MAX + 1];
int main(int argc, char *argv[])
@@ -551,6 +559,7 @@ int main(int argc, char *argv[])
do {
filename = argv[2];
+ memset(&_cli, 0, sizeof(_cli));
_cli.state = TIP_CLIENT_HEAD_HEADER;
ret = tip_client_request_file(&_cli, addr, filename);
} while (ret > 0);
@@ -565,28 +574,22 @@ int main(int argc, char *argv[])
file_size = _cli.content_len;
for (i = 0; i < MAX_CHUNKS; i++) {
- memset(&_cli, 0, sizeof(_cli));
-
k = select_file_chunk(file_chunk);
snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k);
filename = _filename;
chunk_size = file_size / MAX_CHUNKS;
- _cli.chunk_offset = chunk_size * k;
- _cli.fd = fd;
do {
+ tip_client_reset_state(&_cli, fd, chunk_size * k);
syslog(LOG_INFO, "Requesting file %s to server\n", filename);
_cli.state = TIP_CLIENT_GET_HEADER;
ret = tip_client_request_file(&_cli, addr, filename);
} while (ret > 0);
if (ret < 0) {
- memset(&_cli, 0, sizeof(_cli));
- _cli.chunk_offset = chunk_size * k;
- _cli.fd = fd;
- _cli.server_only = true;
-
do {
+ tip_client_reset_state(&_cli, fd, chunk_size * k);
+ _cli.server_only = true;
syslog(LOG_INFO, "Requesting file %s to server only\n", filename);
_cli.state = TIP_CLIENT_GET_HEADER;
ret = tip_client_request_file(&_cli, addr, filename);