diff --git a/include/libp2p/transport/tcp/bytes_counter.hpp b/include/libp2p/transport/tcp/bytes_counter.hpp new file mode 100644 index 00000000..49d2ee30 --- /dev/null +++ b/include/libp2p/transport/tcp/bytes_counter.hpp @@ -0,0 +1,40 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +namespace libp2p::transport { + + class ByteCounter { + private: + std::atomic bytes_read_{0}; + std::atomic bytes_written_{0}; + + ByteCounter() = default; + + public: + ~ByteCounter() = default; + + void incrementBytesRead(uint64_t bytes); + + void incrementBytesWritten(uint64_t bytes); + + uint64_t getBytesRead() const; + + uint64_t getBytesWritten() const; + + static ByteCounter &getInstance(); + + ByteCounter(const ByteCounter &) = delete; + ByteCounter &operator=(const ByteCounter &) = delete; + + ByteCounter(ByteCounter &&) = delete; + ByteCounter &operator=(ByteCounter &&) = delete; + }; + +} // namespace libp2p::transport diff --git a/include/libp2p/transport/tcp/tcp_connection.hpp b/include/libp2p/transport/tcp/tcp_connection.hpp index 28c7735e..57274d07 100644 --- a/include/libp2p/transport/tcp/tcp_connection.hpp +++ b/include/libp2p/transport/tcp/tcp_connection.hpp @@ -91,6 +91,9 @@ namespace libp2p::transport { return debug_str_; } + static uint64_t getBytesRead(); + static uint64_t getBytesWritten(); + private: outcome::result saveMultiaddresses(); diff --git a/src/transport/tcp/CMakeLists.txt b/src/transport/tcp/CMakeLists.txt index bfad59e3..c13e01e2 100644 --- a/src/transport/tcp/CMakeLists.txt +++ b/src/transport/tcp/CMakeLists.txt @@ -4,7 +4,7 @@ # SPDX-License-Identifier: Apache-2.0 # -libp2p_add_library(p2p_tcp_connection tcp_connection.cpp) +libp2p_add_library(p2p_tcp_connection tcp_connection.cpp bytes_counter.cpp) target_link_libraries(p2p_tcp_connection Boost::boost p2p_multiaddress diff --git a/src/transport/tcp/bytes_counter.cpp b/src/transport/tcp/bytes_counter.cpp new file mode 100644 index 00000000..6e67ffb9 --- /dev/null +++ b/src/transport/tcp/bytes_counter.cpp @@ -0,0 +1,32 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +namespace libp2p::transport { + + ByteCounter &ByteCounter::getInstance() { + static ByteCounter instance; + return instance; + } + + void ByteCounter::incrementBytesRead(uint64_t bytes) { + bytes_read_.fetch_add(bytes); + } + + void ByteCounter::incrementBytesWritten(uint64_t bytes) { + bytes_written_.fetch_add(bytes); + } + + uint64_t ByteCounter::getBytesRead() const { + return bytes_read_.load(); + } + + uint64_t ByteCounter::getBytesWritten() const { + return bytes_written_.load(); + } + +} // namespace libp2p::transport diff --git a/src/transport/tcp/tcp_connection.cpp b/src/transport/tcp/tcp_connection.cpp index 04f75d4f..6aa317de 100644 --- a/src/transport/tcp/tcp_connection.cpp +++ b/src/transport/tcp/tcp_connection.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #define TRACE_ENABLED 0 @@ -184,6 +185,7 @@ namespace libp2p::transport { void TcpConnection::readSome(BytesOut out, size_t bytes, TcpConnection::ReadCallbackFunc cb) { + ByteCounter::getInstance().incrementBytesRead(bytes); ambigousSize(out, bytes); TRACE("{} read some up to {}", debug_str_, bytes); socket_.async_read_some(asioBuffer(out), @@ -193,6 +195,7 @@ namespace libp2p::transport { void TcpConnection::writeSome(BytesIn in, size_t bytes, TcpConnection::WriteCallbackFunc cb) { + ByteCounter::getInstance().incrementBytesWritten(bytes); ambigousSize(in, bytes); TRACE("{} write some up to {}", debug_str_, bytes); socket_.async_write_some(asioBuffer(in), @@ -269,4 +272,12 @@ namespace libp2p::transport { return outcome::success(); } + uint64_t TcpConnection::getBytesRead() { + return ByteCounter::getInstance().getBytesRead(); + } + + uint64_t TcpConnection::getBytesWritten() { + return ByteCounter::getInstance().getBytesWritten(); + } + } // namespace libp2p::transport