170 lines
5.3 KiB
C++
170 lines
5.3 KiB
C++
#pragma once
|
|
|
|
#include <asio/as_tuple.hpp>
|
|
#include <asio/buffer.hpp>
|
|
#include <asio/ip/tcp.hpp>
|
|
#include <asio/ip/udp.hpp>
|
|
#include <asio/local/datagram_protocol.hpp>
|
|
#include <asio/local/seq_packet_protocol.hpp>
|
|
#include <asio/local/stream_protocol.hpp>
|
|
#include <asio/redirect_error.hpp>
|
|
#include <asio/use_awaitable.hpp>
|
|
#include <asio/write.hpp>
|
|
|
|
#include "snplib_endpoint.h"
|
|
#include "snplib_netmsg.h"
|
|
#include "snplib_netproto.h"
|
|
#include "snplib_network.h"
|
|
|
|
namespace snplib::asio
|
|
{
|
|
|
|
// supported ASIO-library transport protocols
|
|
template <typename T>
|
|
concept transport_proto_c =
|
|
std::derived_from<T, ::asio::ip::tcp> || std::derived_from<T, ::asio::ip::udp> ||
|
|
std::derived_from<T, ::asio::local::stream_protocol> || std::derived_from<T, ::asio::local::datagram_protocol> ||
|
|
std::derived_from<T, ::asio::local::seq_packet_protocol>;
|
|
|
|
template <typename T>
|
|
concept transport_local_proto_c =
|
|
std::derived_from<T, ::asio::local::stream_protocol> || std::derived_from<T, ::asio::local::datagram_protocol> ||
|
|
std::derived_from<T, ::asio::local::seq_packet_protocol>;
|
|
|
|
template <typename T>
|
|
concept transport_dgram_proto_c =
|
|
std::derived_from<T, ::asio::ip::udp> || std::derived_from<T, ::asio::local::datagram_protocol>;
|
|
|
|
|
|
|
|
template <net_message_c NETMSG_T, net_connection_proto_c NETPROTO_T>
|
|
class net_connection_t
|
|
{
|
|
public:
|
|
typedef NETMSG_T message_t;
|
|
typedef NETPROTO_T netproto_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"};
|
|
|
|
|
|
::asio::awaitable<error_t> send(net_connection_t::message_t msg) {}
|
|
|
|
::asio::awaitable<net_connection_t::recv_result_t> recv() {}
|
|
|
|
::asio::awaitable<error_t> close() {}
|
|
};
|
|
|
|
|
|
template <net_initiator_role_e INI_ROLE, transport_proto_c PROTO_T, net_endpoint_c ENDPOINT_T = net_endpoint_t>
|
|
class net_initiator_t
|
|
{
|
|
public:
|
|
static constexpr net_initiator_role_e ROLE{INI_ROLE};
|
|
|
|
typedef std::chrono::milliseconds timeout_t; // inner timeout duration type
|
|
|
|
typedef std::error_code error_t;
|
|
|
|
typedef PROTO_T proto_t;
|
|
typedef ENDPOINT_T endpoint_t;
|
|
|
|
typedef std::conditional_t<net_initiator_t::ROLE == net_initiator_role_e::ROLE_SERVER,
|
|
std::conditional_t<transport_dgram_proto_c<PROTO_T>, void, typename PROTO_T::acceptor>,
|
|
void>
|
|
acceptor_t;
|
|
|
|
|
|
// typedef net_connection_t connection_t;
|
|
|
|
net_initiator_t(::asio::io_context& io_context, time_duration_c auto const& timeout) : _ioContext(io_context)
|
|
{
|
|
setTimeout(timeout);
|
|
}
|
|
|
|
net_initiator_t(::asio::io_context& io_context, ENDPOINT_T endpoint, time_duration_c auto const& timeout)
|
|
: net_initiator_t(io_context, timeout)
|
|
{
|
|
_endpoint = endpoint;
|
|
}
|
|
|
|
auto accept()
|
|
requires(net_initiator_t::ROLE == net_initiator_role_e::ROLE_SERVER)
|
|
{
|
|
auto addr = co_await resolveAddress();
|
|
|
|
if (addr) {
|
|
if (addr.value.size() == 1) {
|
|
auto [ec, socket] =
|
|
co_await _acceptor.async_accept(*addr.value.begin(), ::asio::as_tuple(::asio::use_awaitable));
|
|
|
|
if (ec) {
|
|
co_return std::unexpected(ec);
|
|
}
|
|
}
|
|
} else {
|
|
co_return std::unexpected(addr.error());
|
|
}
|
|
}
|
|
|
|
auto open()
|
|
requires(net_initiator_t::ROLE == net_initiator_role_e::ROLE_CLIENT)
|
|
{
|
|
}
|
|
|
|
void setTimeout(time_duration_c auto const& timeout)
|
|
{
|
|
_timeout = std::chrono::duration_cast<>(timeout);
|
|
|
|
if (_timeout.count() == 0) { // '0' means forever
|
|
_timeout = std::chrono::milliseconds::max();
|
|
}
|
|
}
|
|
|
|
protected:
|
|
::asio::io_context& _ioContext;
|
|
|
|
ENDPOINT_T _endpoint{};
|
|
|
|
net_initiator_t::acceptor_t _acceptor{_ioContext};
|
|
|
|
net_initiator_t::timeout_t _timeout{};
|
|
|
|
typedef std::conditional_t<transport_local_proto_c<PROTO_T>, void, typename PROTO_T::resolver> resolver_t;
|
|
typedef std::conditional_t<transport_local_proto_c<PROTO_T>, void, typename PROTO_T::resolver::results_type>
|
|
resolver_result_t;
|
|
|
|
::asio::awaitable<std::expected<resolver_result_t, std::error_code>> resolveAddress()
|
|
requires(!std::is_void_v<net_initiator_t::resolver_t>)
|
|
{
|
|
net_initiator_t::resolver_t resolver{_ioContext};
|
|
|
|
resolver_result_t result;
|
|
std::error_code ec;
|
|
|
|
if constexpr (std::derived_from<ENDPOINT_T, net_endpoint_t>) {
|
|
result = co_await resolver.async_resolve(_endpoint.host(), _endpoint.portView(),
|
|
::asio::redirect_error(::asio::use_awaitable, ec));
|
|
} else { // general case
|
|
std::string host = std::format("{}", _endpoint.host());
|
|
std::string port = std::format("{}", static_cast<int>(_endpoint.port()));
|
|
|
|
result = co_await resolver.async_resolve(std::string_view(host), std::string_view(port),
|
|
::asio::redirect_error(::asio::use_awaitable, ec));
|
|
}
|
|
|
|
if (ec) {
|
|
co_return std::unexpected(ec);
|
|
}
|
|
|
|
co_return result;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
static_assert(net_connection_c<asio::net_connection_t<keyval_netmsg_t<>, netproto_stopseq_t<>>>, "!!!");
|
|
|
|
} // namespace snplib::asio
|