diff options
-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; } |