...
This commit is contained in:
@@ -212,10 +212,12 @@ concept ret_expected_char_range_c = requires {
|
||||
// result of asynchronous callable
|
||||
|
||||
template <typename, typename = std::void_t<>>
|
||||
struct has_co_await_operator : std::false_type {};
|
||||
struct has_co_await_operator : std::false_type {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct has_co_await_operator<T, std::void_t<decltype(std::declval<T>().operator co_await())>> : std::true_type {};
|
||||
struct has_co_await_operator<T, std::void_t<decltype(std::declval<T>().operator co_await())>> : std::true_type {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr bool has_co_await_operator_v = has_co_await_operator<T>::value;
|
||||
@@ -270,11 +272,14 @@ concept awaitable_c = (!has_co_await_operator_v<T> || requires {
|
||||
|
||||
|
||||
|
||||
// template <typename T, typename RT = void>
|
||||
// concept awaitable_ret_type_c =
|
||||
// (!has_co_await_operator_v<T> ||
|
||||
// std::same_as<decltype(std::declval<decltype(std::declval<T>().operator co_await())>().await_resume()), RT>) &&
|
||||
// (has_co_await_operator_v<T> || std::same_as<decltype(std::declval<T>().await_resume()), RT>);
|
||||
|
||||
template <typename T, typename RT = void>
|
||||
concept awaitable_ret_type_c =
|
||||
(!has_co_await_operator_v<T> ||
|
||||
std::same_as<decltype(std::declval<decltype(std::declval<T>().operator co_await())>().await_resume()), RT>) &&
|
||||
(has_co_await_operator_v<T> || std::same_as<decltype(std::declval<T>().await_resume()), RT>);
|
||||
concept awaitable_ret_type_c = awaitable_c<T> && std::same_as<deduced_awaitable_ret_type_t<T>, RT>;
|
||||
|
||||
|
||||
static_assert(awaitable_ret_type_c<asio::awaitable<void>>, "!!!");
|
||||
|
||||
@@ -20,7 +20,7 @@ concept net_endpoint_c = requires(T t, const T t_const) {
|
||||
{ t_const.proto() } -> std::formattable<char>;
|
||||
{ t_const.host() } -> std::formattable<char>;
|
||||
{ t_const.path() } -> std::formattable<char>;
|
||||
{ t_const.port() } -> std::formattable<char>;
|
||||
{ t_const.port() } -> std::convertible_to<int>; // 'int' is the standard representation of a network port
|
||||
};
|
||||
|
||||
|
||||
@@ -160,6 +160,11 @@ concept net_initiator_role_c = requires {
|
||||
// default library-wide type of network connection initiator role
|
||||
enum class net_initiator_role_e : int { ROLE_SERVER, ROLE_CLIENT };
|
||||
|
||||
// result of the initiator's "accept" and "open" methods:
|
||||
// awaitable_c with returned type of std::expected<net_connection_c, ret_error_c>
|
||||
template <typename T>
|
||||
concept awaitable_net_initiator_result_c = awaitable_c<T> && ret_expected_c<deduced_awaitable_ret_type_t<T>> &&
|
||||
net_connection_c<typename deduced_awaitable_ret_type_t<T>::value_type>;
|
||||
|
||||
template <typename T>
|
||||
concept net_initiator_c =
|
||||
@@ -175,14 +180,16 @@ concept net_initiator_c =
|
||||
{ t_const.endpoint() } -> net_endpoint_c;
|
||||
} && ((T::ROLE != decltype(T::ROLE)::ROLE_SERVER) || requires(T t) { // use of short-circuiting logic!!!
|
||||
// server-role
|
||||
{ t.accept() } -> async_retval_c<typename T::connection_t>;
|
||||
// { t.accept() } -> async_retval_c<typename T::connection_t>;
|
||||
{ t.accept() } -> awaitable_net_initiator_result_c;
|
||||
|
||||
{ t.startListening() } -> ret_error_c;
|
||||
|
||||
t.stopListening();
|
||||
}) && ((T::ROLE != decltype(T::ROLE)::ROLE_CLIENT) || requires(T t) { // use of short-circuiting logic!!!
|
||||
// client-role
|
||||
{ t.open() } -> async_retval_c<typename T::connection_t>;
|
||||
// { t.open() } -> async_retval_c<typename T::connection_t>;
|
||||
{ t.open() } -> awaitable_net_initiator_result_c;
|
||||
t.close();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,19 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <asio/as_tuple.hpp>
|
||||
#include <asio/buffer.hpp>
|
||||
#include <asio/experimental/coro.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 asio_net_connection_t
|
||||
class net_connection_t
|
||||
{
|
||||
public:
|
||||
typedef NETMSG_T message_t;
|
||||
@@ -25,14 +50,121 @@ public:
|
||||
static constexpr std::string_view id{"ASIO-NETCONN"};
|
||||
|
||||
|
||||
::asio::awaitable<error_t> send(asio_net_connection_t::message_t msg) {}
|
||||
::asio::awaitable<error_t> send(net_connection_t::message_t msg) {}
|
||||
|
||||
::asio::awaitable<asio_net_connection_t::recv_result_t> recv() {}
|
||||
::asio::awaitable<net_connection_t::recv_result_t> recv() {}
|
||||
|
||||
::asio::awaitable<error_t> close() {}
|
||||
};
|
||||
|
||||
|
||||
static_assert(net_connection_c<asio_net_connection_t<keyval_netmsg_t<>, netproto_stopseq_t<>>>, "!!!");
|
||||
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
|
||||
Reference in New Issue
Block a user