From ace36ad51010129b172cf68cea9e9df28e4f92aa Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Tue, 8 Feb 2022 16:19:01 +0100 Subject: clean up state on each retry Reset the client object after retrying connection to download file. --- src/main.c | 21 ++++++++++++--------- 1 file 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); -- cgit v1.2.3-18-g5258