...
This commit is contained in:
@@ -362,6 +362,8 @@ struct netproto_binary_blob_t {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.bytes_used = bytes_sz + BLOB_SIZE_FIELD;
|
||||||
|
|
||||||
bytes_sz -= BLOB_SIZE_FIELD;
|
bytes_sz -= BLOB_SIZE_FIELD;
|
||||||
|
|
||||||
if (bytes_sz < blob_sz.value()) { // not enough data
|
if (bytes_sz < blob_sz.value()) { // not enough data
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ concept net_repr_proto_c = requires(T t) {
|
|||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept net_connection_c = requires(T t) {
|
concept net_connection_c = std::movable<T> && requires(T t) {
|
||||||
// static constexpr member
|
// static constexpr member
|
||||||
requires std::formattable<decltype(T::id), char>;
|
requires std::formattable<decltype(T::id), char>;
|
||||||
[]() { [[maybe_unused]] static constexpr auto val = T::id; }();
|
[]() { [[maybe_unused]] static constexpr auto val = T::id; }();
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
|
|
||||||
#include <asio/as_tuple.hpp>
|
#include <asio/as_tuple.hpp>
|
||||||
#include <asio/buffer.hpp>
|
#include <asio/buffer.hpp>
|
||||||
// #include <asio/buffers_iterator.hpp>
|
#include <asio/buffers_iterator.hpp>
|
||||||
#include <asio/ip/tcp.hpp>
|
#include <asio/ip/tcp.hpp>
|
||||||
#include <asio/ip/udp.hpp>
|
#include <asio/ip/udp.hpp>
|
||||||
#include <asio/local/datagram_protocol.hpp>
|
#include <asio/local/datagram_protocol.hpp>
|
||||||
#include <asio/local/seq_packet_protocol.hpp>
|
#include <asio/local/seq_packet_protocol.hpp>
|
||||||
#include <asio/local/stream_protocol.hpp>
|
#include <asio/local/stream_protocol.hpp>
|
||||||
#include <asio/redirect_error.hpp>
|
#include <asio/redirect_error.hpp>
|
||||||
|
#include <asio/streambuf.hpp>
|
||||||
#include <asio/use_awaitable.hpp>
|
#include <asio/use_awaitable.hpp>
|
||||||
#include <asio/write.hpp>
|
#include <asio/write.hpp>
|
||||||
|
|
||||||
@@ -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<error_t> send(net_connection_t::message_t msg)
|
::asio::awaitable<error_t> send(net_connection_t::message_t msg)
|
||||||
{
|
{
|
||||||
auto msg_bytes = msg.toBytes();
|
auto msg_bytes = msg.toBytes();
|
||||||
@@ -141,11 +146,11 @@ public:
|
|||||||
|
|
||||||
::asio::awaitable<net_connection_t::recv_result_t> recv()
|
::asio::awaitable<net_connection_t::recv_result_t> recv()
|
||||||
{
|
{
|
||||||
static char buff[_recvBufferSize];
|
|
||||||
std::vector<char> msg;
|
|
||||||
|
|
||||||
std::lock_guard lock(*_recvMutex);
|
std::lock_guard lock(*_recvMutex);
|
||||||
|
|
||||||
|
|
||||||
|
net_connection_t::message_t msg;
|
||||||
|
|
||||||
static ::asio::steady_timer timeout_timer(_socket.get_executor());
|
static ::asio::steady_timer timeout_timer(_socket.get_executor());
|
||||||
|
|
||||||
std::variant<size_t, std::monostate> op_res;
|
std::variant<size_t, std::monostate> op_res;
|
||||||
@@ -159,17 +164,18 @@ public:
|
|||||||
timeout_timer.expires_after(_recvTimeout);
|
timeout_timer.expires_after(_recvTimeout);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
auto buff = _recvStreamBuffer->prepare(_recvBufferSize);
|
||||||
|
|
||||||
if constexpr (std::derived_from<
|
if constexpr (std::derived_from<
|
||||||
net_connection_t::socket_t,
|
net_connection_t::socket_t,
|
||||||
::asio::basic_stream_socket<typename net_connection_t::socket_t::protocol_type,
|
::asio::basic_stream_socket<typename net_connection_t::socket_t::protocol_type,
|
||||||
typename net_connection_t::socket_t::executor_type>>) {
|
typename net_connection_t::socket_t::executor_type>>) {
|
||||||
op_res = co_await (
|
op_res = co_await (_socket.async_receive(buff, ::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||||
_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));
|
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()) {
|
if (op_res.index()) {
|
||||||
@@ -180,16 +186,9 @@ public:
|
|||||||
co_return std::unexpected(ec);
|
co_return std::unexpected(ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_recvStreamBuffer->commit(std::get<0>(op_res));
|
||||||
|
|
||||||
nbytes += 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<typename net_connection_t::socket_t::protocol_type,
|
|
||||||
typename net_connection_t::socket_t::executor_type>>) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if constexpr (std::derived_from<
|
if constexpr (std::derived_from<
|
||||||
net_connection_t::socket_t,
|
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<const char*>(_recvStreamBuffer->data().data()), _recvStreamBuffer->size());
|
||||||
|
|
||||||
|
|
||||||
|
auto msg_bytes = _reprProto.fromNetwork(net_bytes);
|
||||||
switch (msg_bytes.flag) {
|
switch (msg_bytes.flag) {
|
||||||
case net_connection_t::net_repr_proto_t::result_flag_t::NOT_ENOUGH_DATA:
|
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)>(
|
timeout_timer.expires_after(_recvTimeout - std::chrono::duration_cast<decltype(_recvTimeout)>(
|
||||||
std::chrono::steady_clock::now() - start_tp));
|
std::chrono::steady_clock::now() - start_tp));
|
||||||
continue;
|
continue;
|
||||||
case net_connection_t::net_repr_proto_t::result_flag_t::INVALID_DATA:
|
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);
|
co_return std::make_error_code(std::errc::invalid_argument);
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// remove only used bytes
|
||||||
|
_recvStreamBuffer->consume(msg_bytes.bytes_used);
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} while (true);
|
} while (true);
|
||||||
|
|
||||||
|
|
||||||
|
co_return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_t close()
|
error_t close()
|
||||||
@@ -246,6 +260,8 @@ protected:
|
|||||||
|
|
||||||
size_t _recvBufferSize{defaultRecvMaxBufferSize};
|
size_t _recvBufferSize{defaultRecvMaxBufferSize};
|
||||||
std::unique_ptr<std::mutex> _recvMutex{std::make_unique<std::mutex>()};
|
std::unique_ptr<std::mutex> _recvMutex{std::make_unique<std::mutex>()};
|
||||||
|
|
||||||
|
std::unique_ptr<::asio::streambuf> _recvStreamBuffer{std::make_unique<::asio::streambuf>()};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user