From e1c092391540fb59e5873dfdb8809bfd8af7997e Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 20 Jul 2020 22:37:40 +0000 Subject: [PATCH] stream: Add byte counters --- include/stream.h | 5 +++++ src/stream.c | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/stream.h b/include/stream.h index 05a3883..734391b 100644 --- a/include/stream.h +++ b/include/stream.h @@ -18,6 +18,8 @@ #include "sys/queue.h" #include "rcbuf.h" +#include + #ifdef ENABLE_TLS #include #endif @@ -73,6 +75,9 @@ struct stream { #ifdef ENABLE_TLS gnutls_session_t tls_session; #endif + + uint32_t bytes_sent; + uint32_t bytes_received; }; struct stream* stream_new(int fd, stream_event_fn on_event, void* userdata); diff --git a/src/stream.c b/src/stream.c index 84a4518..e1e9c8e 100644 --- a/src/stream.c +++ b/src/stream.c @@ -136,6 +136,8 @@ static int stream__flush_plain(struct stream* self) return bytes_sent; } + self->bytes_sent += bytes_sent; + ssize_t bytes_left = bytes_sent; struct stream_req* tmp; @@ -181,6 +183,8 @@ static int stream__flush_tls(struct stream* self) return -1; } + self->bytes_sent += rc; + ssize_t remaining = req->payload->size - rc; if (remaining > 0) { @@ -336,6 +340,8 @@ static ssize_t stream__read_plain(struct stream* self, void* dst, size_t size) ssize_t rc = read(self->fd, dst, size); if (rc == 0) stream__remote_closed(self); + if (rc > 0) + self->bytes_received += rc; return rc; } @@ -343,8 +349,10 @@ static ssize_t stream__read_plain(struct stream* self, void* dst, size_t size) static ssize_t stream__read_tls(struct stream* self, void* dst, size_t size) { ssize_t rc = gnutls_record_recv(self->tls_session, dst, size); - if (rc >= 0) + if (rc >= 0) { + self->bytes_received += rc; return rc; + } switch (rc) { case GNUTLS_E_INTERRUPTED: