diff options
author | tiptorrent development team <tiptorrent@soleta.eu> | 2021-09-29 16:33:23 +0200 |
---|---|---|
committer | tiptorrent development team <tiptorrent@soleta.eu> | 2021-09-29 16:46:05 +0200 |
commit | 73d546b23ad717c93212ee091a8d118ab34d3fda (patch) | |
tree | bad94403e547a95a63ab8e77bca39d5c6bd544ba /src | |
parent | 7e02f8e7c60cc1bca83550c328925fa1b2a4581c (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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; } |