From 73d546b23ad717c93212ee091a8d118ab34d3fda Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Wed, 29 Sep 2021 16:33:23 +0200 Subject: print progress message Print a progress message to stdout: 0% (0 Mbytes/second) file.img.2 from 192.168.2.179:9999 29% (113 Mbytes/second) file.img.2 from 192.168.2.179:9999 59% (113 Mbytes/second) file.img.2 from 192.168.2.179:9999 88% (114 Mbytes/second) file.img.2 from 192.168.2.179:9999 100% (128 Mbytes/second) file.img.2 from 192.168.2.179:9999 0% (0 Mbytes/second) file.img.3 from 192.168.2.179:9999 29% (113 Mbytes/second) file.img.3 from 192.168.2.179:9999 58% (113 Mbytes/second) file.img.3 from 192.168.2.179:9999 88% (113 Mbytes/second) file.img.3 from 192.168.2.179:9999 100% (128 Mbytes/second) file.img.3 from 192.168.2.179:9999 0% (0 Mbytes/second) file.img.1 from 192.168.2.179:9999 29% (114 Mbytes/second) file.img.1 from 192.168.2.179:9999 59% (114 Mbytes/second) file.img.1 from 192.168.2.179:9999 88% (113 Mbytes/second) file.img.1 from 192.168.2.179:9999 100% (128 Mbytes/second) file.img.1 from 192.168.2.179:9999 0% (0 Mbytes/second) file.img.0 from 192.168.2.179:9999 29% (113 Mbytes/second) file.img.0 from 192.168.2.179:9999 59% (113 Mbytes/second) file.img.0 from 192.168.2.179:9999 88% (113 Mbytes/second) file.img.0 from 192.168.2.179:9999 100% (128 Mbytes/second) file.img.0 from 192.168.2.179:9999 OK. --- src/main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index b2acc28..8e8819f 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,7 @@ struct tip_client { int fd; bool error; bool redirected; + struct timeval tv_start, tv_last; const char *payload; }; @@ -99,6 +100,22 @@ static void tip_client_error(struct tip_client *cli) tip_client_close(cli); } +static void tip_client_progress(struct tip_client *cli, bool now) +{ + struct timeval tv_cur, tv; + + gettimeofday(&tv_cur, NULL); + timersub(&tv_cur, &cli->tv_last, &tv); + if (now || tv.tv_sec >= 1) { + timersub(&tv_cur, &cli->tv_start, &tv); + printf("%3lu%% (%lu Mbytes/second) %s from %s:9999\n", + cli->content_len > 0 ? 100 * cli->data_len / cli->content_len : 0, + tv.tv_sec > 0 ? cli->data_len / 1024000 / tv.tv_sec : cli->data_len / 1024000, + filename, inet_ntoa(cli->addr.sin_addr)); + cli->tv_last = tv_cur; + } +} + static int tip_client_connect(const char *addr); static int tip_client_state_recv_hdr(struct tip_client *cli) @@ -155,6 +172,8 @@ static int tip_client_state_recv_hdr(struct tip_client *cli) if (cli->content_len < 0) return -1; + tip_client_progress(cli, true); + if (cli->content_len == 0) { cli->buf_len = 0; return 1; @@ -177,6 +196,8 @@ static int tip_client_state_recv_hdr(struct tip_client *cli) payload_len = cli->buf_len - header_len; cli->data_len += cli->buf_len; cli->buf_len = 0; + gettimeofday(&cli->tv_start, NULL); + cli->tv_last = cli->tv_start; if (payload_len > 0) { ret = write(cli->fd, payload, payload_len); @@ -208,6 +229,8 @@ static int tip_client_state_recv_payload(struct tip_client *cli) cli->buf_len = 0; + tip_client_progress(cli, false); + if (cli->data_len >= cli->content_len) { if (cli->redirected) { tip_client_close(cli); @@ -459,6 +482,7 @@ int main(int argc, char *argv[]) break; } + tip_client_progress(&_cli, true); file_chunk[k] = true; data_len += _cli.data_len; } -- cgit v1.2.3-18-g5258