...
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <asio/as_tuple.hpp>
|
||||
#include <asio/buffer.hpp>
|
||||
// #include <asio/buffers_iterator.hpp>
|
||||
#include <asio/ip/tcp.hpp>
|
||||
#include <asio/ip/udp.hpp>
|
||||
#include <asio/local/datagram_protocol.hpp>
|
||||
@@ -42,19 +43,157 @@ class net_connection_t
|
||||
{
|
||||
public:
|
||||
typedef NETMSG_T message_t;
|
||||
typedef NET_TRANSP_PROTO_T net_transp_proto_t;
|
||||
typedef NET_REPR_PROTO_T net_repr_proto_t;
|
||||
|
||||
typedef typename NET_TRANSP_PROTO_T::socket socket_t;
|
||||
|
||||
typedef std::error_code error_t;
|
||||
typedef std::expected<message_t, error_t> recv_result_t;
|
||||
|
||||
static constexpr std::string_view id{"ASIO-NETCONN"};
|
||||
|
||||
inline constexpr static std::chrono::seconds defaultSendTimeout = std::chrono::seconds(5);
|
||||
inline constexpr static std::chrono::seconds defaultRecvTimeout = std::chrono::seconds(5);
|
||||
|
||||
::asio::awaitable<error_t> send(net_connection_t::message_t msg) {}
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
net_connection_t(net_connection_t::socket_t socket, net_connection_t::net_repr_proto_t repr_proto)
|
||||
: _socket(std::move(socket)), _reprProto(std::move(repr_proto))
|
||||
{
|
||||
}
|
||||
|
||||
::asio::awaitable<error_t> send(net_connection_t::message_t msg)
|
||||
{
|
||||
auto msg_bytes = msg.toBytes();
|
||||
auto net_bytes = _reprProto.toNetwork(msg_bytes);
|
||||
if (net_bytes.flag != net_connection_t::net_repr_proto_t::result_flag_t::OK) {
|
||||
}
|
||||
|
||||
switch (net_bytes.flag) {
|
||||
case net_connection_t::net_repr_proto_t::result_flag_t::NOT_ENOUGH_DATA:
|
||||
co_return std::errc::message_size;
|
||||
case net_connection_t::net_repr_proto_t::result_flag_t::INVALID_DATA:
|
||||
co_return std::errc::invalid_argument;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
std::vector<::asio::const_buffer> buffs;
|
||||
|
||||
using res_t = decltype(net_bytes.result);
|
||||
|
||||
if constexpr (char_range_c<res_t>) {
|
||||
buffs.emplace_back(::asio::buffer(net_bytes.result));
|
||||
} else if constexpr (tuple_c<res_t>) {
|
||||
[&buffs]<size_t... Is>(std::index_sequence<Is...>) {
|
||||
(buffs.emplace_back(std::get<Is>(net_bytes.result)), ...);
|
||||
}(std::make_index_sequence<std::tuple_size_v<res_t>>{});
|
||||
} else { // range of char ranges
|
||||
for (auto const& b : net_bytes.result) {
|
||||
buffs.emplace_back(::asio::buffer(b));
|
||||
}
|
||||
}
|
||||
|
||||
std::variant<size_t, std::monostate> op_res;
|
||||
std::error_code ec{};
|
||||
|
||||
thread_local static ::asio::steady_timer timeout_timer(_socket.get_executor());
|
||||
|
||||
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>>) {
|
||||
// has async_write_some method
|
||||
|
||||
timeout_timer.expires_after(_sendTimeout);
|
||||
|
||||
op_res = ::asio::async_write(_socket, buffs, ::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable);
|
||||
} else {
|
||||
const size_t bytes_sz = ::asio::buffer_size(buffs);
|
||||
size_t nbytes, nb;
|
||||
::asio::socket_base::message_flags flags{0};
|
||||
|
||||
auto tm = _sendTimeout;
|
||||
|
||||
auto start_tp = std::chrono::steady_clock::now();
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
do {
|
||||
nb = 0;
|
||||
auto bf = std::span(static_cast<const unsigned char*>(buffs[i].data()), buffs[i].size());
|
||||
|
||||
do {
|
||||
timeout_timer.expires_after(tm);
|
||||
|
||||
op_res = _socket.async_send(::asio::buffer(bf), flags,
|
||||
::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable);
|
||||
if (op_res.index() == 1 || ec) {
|
||||
break;
|
||||
}
|
||||
|
||||
nb += std::get<0>(op_res);
|
||||
if (nb < buffs[i].size()) {
|
||||
bf = std::span(static_cast<const unsigned char*>(buffs[i].data()) + nb, buffs[i].size() - nb);
|
||||
|
||||
tm = _sendTimeout -
|
||||
std::chrono::
|
||||
duration_cast<decltype(_sendTimeout)>(std::chrono::steady_clock::now() - start_tp);
|
||||
|
||||
if (tm.count() == 0) {
|
||||
ec = std::make_error_code(std::errc::timed_out);
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
if (op_res.index() == 1 || ec) {
|
||||
break;
|
||||
}
|
||||
|
||||
++i;
|
||||
|
||||
if (i == buffs.size()) { // should never be!!!
|
||||
ec = std::make_error_code(std::errc::io_error);
|
||||
break;
|
||||
}
|
||||
|
||||
nbytes += nb;
|
||||
|
||||
} while (nbytes < bytes_sz);
|
||||
}
|
||||
|
||||
if (op_res.index() == 1) {
|
||||
ec = std::make_error_code(std::errc::timed_out);
|
||||
} else {
|
||||
if (!ec) {
|
||||
// log ...
|
||||
}
|
||||
}
|
||||
|
||||
co_return ec;
|
||||
}
|
||||
|
||||
::asio::awaitable<net_connection_t::recv_result_t> recv() {}
|
||||
|
||||
::asio::awaitable<error_t> close() {}
|
||||
|
||||
protected:
|
||||
net_connection_t::socket_t _socket;
|
||||
NET_REPR_PROTO_T _reprProto;
|
||||
|
||||
std::chrono::microseconds _sendTimeout{defaultSendTimeout};
|
||||
std::chrono::microseconds _recvTimeout{defaultRecvTimeout};
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user