diff --git a/include/snipplib/concepts/snplib_concepts.h b/include/snipplib/concepts/snplib_concepts.h index 55a5789..3edfcff 100644 --- a/include/snipplib/concepts/snplib_concepts.h +++ b/include/snipplib/concepts/snplib_concepts.h @@ -212,10 +212,12 @@ concept ret_expected_char_range_c = requires { // result of asynchronous callable template > -struct has_co_await_operator : std::false_type {}; +struct has_co_await_operator : std::false_type { +}; template -struct has_co_await_operator().operator co_await())>> : std::true_type {}; +struct has_co_await_operator().operator co_await())>> : std::true_type { +}; template constexpr bool has_co_await_operator_v = has_co_await_operator::value; @@ -270,11 +272,14 @@ concept awaitable_c = (!has_co_await_operator_v || requires { +// template +// concept awaitable_ret_type_c = +// (!has_co_await_operator_v || +// std::same_as().operator co_await())>().await_resume()), RT>) && +// (has_co_await_operator_v || std::same_as().await_resume()), RT>); + template -concept awaitable_ret_type_c = - (!has_co_await_operator_v || - std::same_as().operator co_await())>().await_resume()), RT>) && - (has_co_await_operator_v || std::same_as().await_resume()), RT>); +concept awaitable_ret_type_c = awaitable_c && std::same_as, RT>; static_assert(awaitable_ret_type_c>, "!!!"); diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 3fd21ef..f86ad76 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -20,7 +20,7 @@ concept net_endpoint_c = requires(T t, const T t_const) { { t_const.proto() } -> std::formattable; { t_const.host() } -> std::formattable; { t_const.path() } -> std::formattable; - { t_const.port() } -> std::formattable; + { t_const.port() } -> std::convertible_to; // '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 +template +concept awaitable_net_initiator_result_c = awaitable_c && ret_expected_c> && + net_connection_c::value_type>; template 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; + // { t.accept() } -> async_retval_c; + { 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; + // { t.open() } -> async_retval_c; + { t.open() } -> awaitable_net_initiator_result_c; t.close(); }); diff --git a/include/snipplib/network/snplib_network_asio.h b/include/snipplib/network/snplib_network_asio.h index dfd700d..ecbc06f 100644 --- a/include/snipplib/network/snplib_network_asio.h +++ b/include/snipplib/network/snplib_network_asio.h @@ -1,19 +1,44 @@ #pragma once +#include #include -#include #include +#include +#include +#include +#include +#include #include #include +#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 +concept transport_proto_c = + std::derived_from || std::derived_from || + std::derived_from || std::derived_from || + std::derived_from; + +template +concept transport_local_proto_c = + std::derived_from || std::derived_from || + std::derived_from; + +template +concept transport_dgram_proto_c = + std::derived_from || std::derived_from; + + + template -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 send(asio_net_connection_t::message_t msg) {} + ::asio::awaitable send(net_connection_t::message_t msg) {} - ::asio::awaitable recv() {} + ::asio::awaitable recv() {} ::asio::awaitable close() {} }; -static_assert(net_connection_c, netproto_stopseq_t<>>>, "!!!"); +template +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, 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, void, typename PROTO_T::resolver> resolver_t; + typedef std::conditional_t, void, typename PROTO_T::resolver::results_type> + resolver_result_t; + + ::asio::awaitable> resolveAddress() + requires(!std::is_void_v) + { + net_initiator_t::resolver_t resolver{_ioContext}; + + resolver_result_t result; + std::error_code ec; + + if constexpr (std::derived_from) { + 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(_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, netproto_stopseq_t<>>>, "!!!"); } // namespace snplib::asio \ No newline at end of file