This commit is contained in:
2026-10-18 01:05:59 +03:00
parent f3d7c4fed6
commit 7f317aee67

View File

@@ -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<NET_REPR_PROTO_T>
: _socket(std::move(socket)), _reprProto()
@@ -137,14 +141,101 @@ public:
::asio::awaitable<net_connection_t::recv_result_t> recv()
{
thread_local static ::asio::steady_timer timeout_timer(_socket.get_executor());
static char buff[_recvBufferSize];
std::vector<char> msg;
std::lock_guard lock(*_recvMutex);
static ::asio::steady_timer timeout_timer(_socket.get_executor());
std::variant<size_t, std::monostate> 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<typename net_connection_t::socket_t::protocol_type,
typename net_connection_t::socket_t::executor_type>>) {
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<typename net_connection_t::socket_t::protocol_type,
typename net_connection_t::socket_t::executor_type>>) {
}
if constexpr (std::derived_from<
net_connection_t::socket_t,
::asio::basic_seq_packet_socket<typename net_connection_t::socket_t::protocol_type,
typename net_connection_t::socket_t::executor_type>> ||
std::derived_from<
net_connection_t::socket_t,
::asio::basic_datagram_socket<typename net_connection_t::socket_t::protocol_type,
typename net_connection_t::socket_t::executor_type>>) {
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<decltype(_recvTimeout)>(
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<decltype(_recvTimeout)>(
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<error_t> 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<std::mutex> _recvMutex{std::make_unique<std::mutex>()};
};