diff --git a/include/snipplib/network/snplib_network_asio.h b/include/snipplib/network/snplib_network_asio.h index 2c1ab86..6a3e06a 100644 --- a/include/snipplib/network/snplib_network_asio.h +++ b/include/snipplib/network/snplib_network_asio.h @@ -56,6 +56,10 @@ public: inline constexpr static std::chrono::seconds defaultSendTimeout = std::chrono::seconds(5); inline constexpr static std::chrono::seconds defaultRecvTimeout = std::chrono::seconds(5); + // maximal size of receive buffer + // (to receive full message multiple the read operation may need to be performed multiple times) + inline constexpr static size_t defaultRecvMaxBufferSize = 1024; + explicit net_connection_t(net_connection_t::socket_t socket) requires std::is_default_constructible_v : _socket(std::move(socket)), _reprProto() @@ -137,14 +141,101 @@ public: ::asio::awaitable recv() { - thread_local static ::asio::steady_timer timeout_timer(_socket.get_executor()); + static char buff[_recvBufferSize]; + std::vector msg; + + std::lock_guard lock(*_recvMutex); + + static ::asio::steady_timer timeout_timer(_socket.get_executor()); + + std::variant op_res; + std::error_code ec{}; + size_t nbytes = 0; + + ::asio::socket_base::message_flags out_flags{0}; auto start_tp = std::chrono::steady_clock::now(); timeout_timer.expires_after(_recvTimeout); + + do { + 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)) || + timeout_timer.async_wait(::asio::use_awaitable)); + } + + if (op_res.index()) { + co_return std::unexpected(std::make_error_code(std::errc::timed_out)); + } + + if (ec) { + co_return std::unexpected(ec); + } + + 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, + ::asio::basic_seq_packet_socket> || + std::derived_from< + net_connection_t::socket_t, + ::asio::basic_datagram_socket>) { + if (!(out_flags & ::asio::socket_base::message_end_of_record)) { // still not full packet/datagram + // re-arm timer + timeout_timer.expires_after(_recvTimeout - std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_tp)); + continue; + } + } + + auto msg_bytes = _reprProto.fromNetwork(std::span(msg)); + 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: + co_return std::make_error_code(std::errc::invalid_argument); + default: + break; + } + + break; + } while (true); } - ::asio::awaitable close() {} + error_t close() + { + std::error_code ec; + + _socket.shutdown(::asio::socket_base::shutdown_both, ec); + if (ec) { + return ec; + } + + _socket.close(ec); + + return ec; + } protected: net_connection_t::socket_t _socket; @@ -152,6 +243,9 @@ protected: std::chrono::microseconds _sendTimeout{defaultSendTimeout}; std::chrono::microseconds _recvTimeout{defaultRecvTimeout}; + + size_t _recvBufferSize{defaultRecvMaxBufferSize}; + std::unique_ptr _recvMutex{std::make_unique()}; };