diff --git a/net/adc_net_concepts.h b/net/adc_net_concepts.h index 4ff1da9..010ee1d 100644 --- a/net/adc_net_concepts.h +++ b/net/adc_net_concepts.h @@ -50,20 +50,20 @@ using adc_common_duration_t = adc_duration_common_type_t - auto asyncAccept(const endpoint_t&, async_ctx_t&, const std::chrono::duration&) + auto asyncAccept(const endpoint_t&, async_call_ctx_t&, const std::chrono::duration&) template - auto asyncConnect(const endpoint_t&, async_ctx_t&, const std::chrono::duration&) + auto asyncConnect(const endpoint_t&, async_call_ctx_t&, const std::chrono::duration&) template - auto asyncSend(const R&, async_ctx_t&, const std::chrono::duration&) + auto asyncSend(const R&, async_call_ctx_t&, const std::chrono::duration&) template - auto asyncReceived(async_ctx_t&, const std::chrono::duration&) + auto asyncReceived(async_call_ctx_t&, const std::chrono::duration&) template auto accept(const endpoint_t&, const std::chrono::duration&) @@ -92,20 +92,20 @@ concept adc_netservice_c = // netservice_ident_t ident() const { srv_const.ident() } -> std::same_as; - typename SRVT::async_ctx_t; + typename SRVT::async_call_ctx_t; typename SRVT::endpoint_t; // asynchronous (non-blocking) operations - srv.asyncAccept(std::declval(), std::declval(), - std::declval()); + srv.asyncAccept(std::declval(), + std::declval(), std::declval()); - srv.asyncConnect(std::declval(), std::declval(), - std::declval()); + srv.asyncConnect(std::declval(), + std::declval(), std::declval()); - srv.asyncSend(std::declval(), std::declval(), + srv.asyncSend(std::declval(), std::declval(), std::declval()); - srv.asyncReceive(std::declval(), std::declval()); + srv.asyncReceive(std::declval(), std::declval()); // synchronous (blocking) operations srv.accept(std::declval(), std::declval()); diff --git a/net/asio/adc_netservice_asio.h b/net/asio/adc_netservice_asio.h index a57ca31..8c1d6e0 100644 --- a/net/asio/adc_netservice_asio.h +++ b/net/asio/adc_netservice_asio.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include #include @@ -48,6 +50,26 @@ concept adc_asio_stream_transport_proto_c = std::derived_from || std::derived_from; +template +concept adc_asio_is_future = requires { + [](std::type_identity>) { + }(std::type_identity>()); +}; + +template +concept adc_asio_is_awaitable = requires { + [](std::type_identity>) { + }(std::type_identity>()); +}; + +struct adc_asio_async_call_ctx_t { +}; + +template +concept adc_completion_token_c = traits::adc_is_callable || std::same_as || + std::same_as || adc_asio_is_future || adc_asio_is_awaitable; + + template SESSION_PROTOT, traits::adc_output_char_range RMSGT = std::vector> @@ -67,6 +89,9 @@ public: std::function receive_comp_token; }; + // to satisfy 'adc_netservice_c' concept + using async_call_ctx_t = adc_asio_async_call_ctx_t; + static constexpr std::chrono::duration DEFAULT_ACCEPT_TIMEOUT = std::chrono::years::max(); static constexpr std::chrono::duration DEFAULT_CONNECT_TIMEOUT = std::chrono::seconds(10); static constexpr std::chrono::duration DEFAULT_SEND_TIMEOUT = std::chrono::seconds(5); @@ -81,7 +106,10 @@ public: virtual ~AdcNetServiceASIOBase() {} - netservice_ident_t ident() const { return _ident; } + netservice_ident_t ident() const + { + return _ident; + } void clear() @@ -92,39 +120,31 @@ public: } - template - auto asyncConnect(const endpoint_t& endpoint, - asio_async_ctx_t& ctx, - const TimeoutT& timeout = DEFAULT_CONNECT_TIMEOUT) + template + auto asyncConnect(const endpoint_t& endpoint, TokenT&& token, const TimeoutT& timeout = DEFAULT_CONNECT_TIMEOUT) { + static_assert(std::is_same_v, "'async_call_ctx_t'-TYPE MUST NOT BE USED!"); + auto timer = getDeadlineTimer(timeout); - auto comp_token = [&ctx, timer = std::move(timer), this](std::error_code ec) { + auto comp_token = [wrapper = traits::adc_pf_wrapper(std::forward(token)), + timer = std::move(timer)](std::error_code ec) { if (isTimeout(timer, ec)) { ec = std::make_error_code(std::errc::timed_out); } else { timer->cancel(); } - if (!ctx.use_future) { - ctx.connect_comp_token(ec); + + if constexpr (!adc_asio_is_future) { + std::get<0>(wrapper)(ec); } }; - if (ctx.use_future) { - comp_token = asio::use_future(comp_token); + if constexpr (!adc_asio_is_future) { + return _socket.async_connect(endpoint, asio::use_future(std::move(comp_token))); + } else { + return _socket.async_connect(endpoint, std::move(comp_token)); } - - return _socket.async_connect(endpoint, comp_token); - - // if (ctx.use_future) { - // return _socket.async_connect( - // endpoint, asio::use_future([&ctx, timer = std::move(timer)](std::error_code) { timer->cancel(); })); - // } else { - // return _socket.async_connect(endpoint, [&ctx, timer = std::move(timer)](std::error_code ec) { - // timer->cancel(); - // ctx.connect_comp_token(ec); - // }); - // } } @@ -170,7 +190,7 @@ public: auto timer = getDeadlineTimer(timeout); - auto comp_token = [&ctx, timer = std::move(timer)](std::error_code ec, size_t) { + auto comp_token = [&ctx, buff_seq, timer = std::move(timer)](std::error_code ec, size_t) { timer->cancel(); if (!ctx.use_future) { @@ -364,9 +384,15 @@ public: return ftr.get(); } - void setShutdownType(asio::socket_base::shutdown_type shutdown_type) { _shutdownType = shutdown_type; } + void setShutdownType(asio::socket_base::shutdown_type shutdown_type) + { + _shutdownType = shutdown_type; + } - asio::socket_base::shutdown_type getShutdownType() const { return _shutdownType; } + asio::socket_base::shutdown_type getShutdownType() const + { + return _shutdownType; + } std::error_code close() { @@ -415,7 +441,7 @@ protected: } template - bool isTimeout(const std::unique_ptr& timer, const std::error_code& ec) + static bool isTimeout(const std::unique_ptr& timer, const std::error_code& ec) { auto exp_time = timer->expiry(); return (exp_time < std::chrono::steady_clock::now()) && (ec == asio::error::operation_aborted); diff --git a/tests/adc_netservice_test.cpp b/tests/adc_netservice_test.cpp index d4d513b..a63e3ef 100644 --- a/tests/adc_netservice_test.cpp +++ b/tests/adc_netservice_test.cpp @@ -18,7 +18,9 @@ int main() }; // srv.asyncAccept(ept_s, srv_ctx, std::chrono::seconds(120)); - srv.asyncConnect(ept_c, srv_ctx); + srv.asyncConnect(ept_c, [](std::error_code ec) { + + }); ctx.run();