From 95f5b2da6b34e430102de3f10c770f954e344cb8 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Sun, 18 Oct 2026 23:54:22 +0300 Subject: [PATCH] ... --- include/snipplib/network/snplib_netproto.h | 2 + include/snipplib/network/snplib_network.h | 2 +- .../snipplib/network/snplib_network_asio.h | 56 ++++++++++++------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/include/snipplib/network/snplib_netproto.h b/include/snipplib/network/snplib_netproto.h index 06c42a8..1c99a49 100644 --- a/include/snipplib/network/snplib_netproto.h +++ b/include/snipplib/network/snplib_netproto.h @@ -362,6 +362,8 @@ struct netproto_binary_blob_t { return res; } + res.bytes_used = bytes_sz + BLOB_SIZE_FIELD; + bytes_sz -= BLOB_SIZE_FIELD; if (bytes_sz < blob_sz.value()) { // not enough data diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 37bc2d5..bdf6493 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -129,7 +129,7 @@ concept net_repr_proto_c = requires(T t) { template -concept net_connection_c = requires(T t) { +concept net_connection_c = std::movable && requires(T t) { // static constexpr member requires std::formattable; []() { [[maybe_unused]] static constexpr auto val = T::id; }(); diff --git a/include/snipplib/network/snplib_network_asio.h b/include/snipplib/network/snplib_network_asio.h index 6a3e06a..9fab18e 100644 --- a/include/snipplib/network/snplib_network_asio.h +++ b/include/snipplib/network/snplib_network_asio.h @@ -2,13 +2,14 @@ #include #include -// #include +#include #include #include #include #include #include #include +#include #include #include @@ -71,6 +72,10 @@ public: { } + // net_connection_t(const net_connection_t&) = delete; + net_connection_t(net_connection_t&&) = default; + net_connection_t& operator=(net_connection_t&&) = default; + ::asio::awaitable send(net_connection_t::message_t msg) { auto msg_bytes = msg.toBytes(); @@ -141,11 +146,11 @@ public: ::asio::awaitable recv() { - static char buff[_recvBufferSize]; - std::vector msg; - std::lock_guard lock(*_recvMutex); + + net_connection_t::message_t msg; + static ::asio::steady_timer timeout_timer(_socket.get_executor()); std::variant op_res; @@ -159,17 +164,18 @@ public: timeout_timer.expires_after(_recvTimeout); do { + auto buff = _recvStreamBuffer->prepare(_recvBufferSize); + if constexpr (std::derived_from< net_connection_t::socket_t, ::asio::basic_stream_socket>) { - op_res = co_await ( - _socket.async_receive(::asio::buffer(buff), ::asio::redirect_error(::asio::use_awaitable, ec)) || - timeout_timer.async_wait(::asio::use_awaitable)); - } else { - op_res = co_await (_socket.async_receive(::asio::buffer(buff), out_flags, - ::asio::redirect_error(::asio::use_awaitable, ec)) || + op_res = co_await (_socket.async_receive(buff, ::asio::redirect_error(::asio::use_awaitable, ec)) || timeout_timer.async_wait(::asio::use_awaitable)); + } else { + op_res = co_await ( + _socket.async_receive(buff, out_flags, ::asio::redirect_error(::asio::use_awaitable, ec)) || + timeout_timer.async_wait(::asio::use_awaitable)); } if (op_res.index()) { @@ -180,16 +186,9 @@ public: co_return std::unexpected(ec); } + _recvStreamBuffer->commit(std::get<0>(op_res)); + nbytes += std::get<0>(op_res); - msg.insert(msg.end(), std::begin(buff), std::begin(buff) + nbytes); - - if constexpr (std::derived_from< - net_connection_t::socket_t, - ::asio::basic_stream_socket>) { - } - - if constexpr (std::derived_from< net_connection_t::socket_t, @@ -207,20 +206,35 @@ public: } } - auto msg_bytes = _reprProto.fromNetwork(std::span(msg)); + auto net_bytes = + std::span(static_cast(_recvStreamBuffer->data().data()), _recvStreamBuffer->size()); + + + auto msg_bytes = _reprProto.fromNetwork(net_bytes); switch (msg_bytes.flag) { case net_connection_t::net_repr_proto_t::result_flag_t::NOT_ENOUGH_DATA: timeout_timer.expires_after(_recvTimeout - std::chrono::duration_cast( std::chrono::steady_clock::now() - start_tp)); continue; case net_connection_t::net_repr_proto_t::result_flag_t::INVALID_DATA: + // delete the bytes from the stream!!! + _recvStreamBuffer->consume(_recvStreamBuffer->size()); co_return std::make_error_code(std::errc::invalid_argument); default: break; } + + + // remove only used bytes + _recvStreamBuffer->consume(msg_bytes.bytes_used); + + break; } while (true); + + + co_return msg; } error_t close() @@ -246,6 +260,8 @@ protected: size_t _recvBufferSize{defaultRecvMaxBufferSize}; std::unique_ptr _recvMutex{std::make_unique()}; + + std::unique_ptr<::asio::streambuf> _recvStreamBuffer{std::make_unique<::asio::streambuf>()}; };