#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef USE_OPENSSL_WITH_ASIO #include #include #endif #include "../../common/adc_traits.h" #include "../adc_net_concepts.h" #include "../adc_netproto.h" namespace adc::impl { // typedef for ASIO streambuf iterators using asio_streambuff_iter_t = asio::buffers_iterator; // ASIO match condition result typedef using asio_matchcond_result_t = std::pair; template concept adc_asio_transport_proto_c = std::derived_from || std::derived_from || std::derived_from || std::derived_from || std::derived_from; template 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>()); [](std::type_identity>) { }(std::type_identity>{}); }; template concept adc_asio_is_awaitable = requires { [](std::type_identity>) { }(std::type_identity>{}); }; template concept adc_asio_special_comp_token_c = adc_asio_is_future || adc_asio_is_awaitable || std::same_as, asio::deferred_t>; // class adc_asio_async_call_ctx_t // { // }; // template // concept adc_completion_token_c = // std::same_as || // (traits::adc_is_callable && // std::conditional_t, // std::true_type, // std::bool_constant>>::value); template SESSION_PROTOT, traits::adc_output_char_range RMSGT = std::vector> // used only for inner storing of message byte sequence class AdcNetServiceASIOBase : public SESSION_PROTOT { public: // typedefs to satisfy 'adc_netservice_c' concept typedef std::string_view netservice_ident_t; typedef std::vector send_msg_t; // in general, only one of several possible typedef RMSGT recv_msg_t; // in general, only one of several possible (see class template arguments declaration) typedef traits::adc_common_duration_t timeout_t; using endpoint_t = typename TRANSPORT_PROTOT::endpoint; // typedefs for completion tokens (callbacks, required by 'adc_netservice_c' concept) typedef std::function async_accept_callback_t; typedef std::function async_connect_callback_t; typedef std::function async_send_callback_t; typedef std::function async_receive_callback_t; // typedefs from transport protocol using socket_t = typename TRANSPORT_PROTOT::socket; using acceptor_t = std::conditional_t>, std::nullptr_t, // there is no acceptor typename TRANSPORT_PROTOT::acceptor>; // to satisfy 'adc_netservice_c' concept class async_call_ctx_t { std::function _errc_comp_token; std::function _errc_msg_comp_token; public: async_call_ctx_t() = default; async_call_ctx_t(async_call_ctx_t&) = default; async_call_ctx_t(async_call_ctx_t&&) = default; async_call_ctx_t(const async_call_ctx_t&) = default; template TokenT> async_call_ctx_t(TokenT&& token) { _errc_comp_token = std::forward(token); } template TokenT> async_call_ctx_t(TokenT&& token) { _errc_msg_comp_token = std::forward(token); } template TokenT> async_call_ctx_t& operator=(TokenT&& token) { _errc_comp_token = std::forward(token); return *this; } template TokenT> async_call_ctx_t& operator=(TokenT&& token) { _errc_msg_comp_token = std::forward(token); return *this; } auto operator()(std::error_code ec) { return _errc_comp_token(std::move(ec)); } auto operator()(std::error_code ec, RMSGT msg) { return _errc_msg_comp_token(std::move(ec), std::move(msg)); } template TokenT> operator TokenT() const { return _errc_comp_token; } template TokenT> operator TokenT() const { return _errc_msg_comp_token; } }; static constexpr std::chrono::duration DEFAULT_ACCEPT_TIMEOUT = std::chrono::seconds::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); static constexpr std::chrono::duration DEFAULT_RECEIVE_TIMEOUT = std::chrono::seconds(5); AdcNetServiceASIOBase(asio::io_context& ctx) : SESSION_PROTOT(), _ioContext(ctx), _receiveStrand(_ioContext), _receiveQueue(), _acceptor(_ioContext), _socket(_ioContext) { } AdcNetServiceASIOBase(socket_t socket) : SESSION_PROTOT(), _ioContext(socket.get_executor()), _receiveStrand(_ioContext), _receiveQueue(), _socket(std::move(socket)) { } AdcNetServiceASIOBase(AdcNetServiceASIOBase&& other) : _ioContext(other._ioContext), _receiveStrand(std::move(other._receiveStrand)), _receiveQueue(std::move(_receiveQueue)), _acceptor(std::move(other._acceptor)), _socket(std::move(other._socket)), _streamBuffer() { auto bytes = asio::buffer_copy(_streamBuffer.prepare(other._streamBuffer.size()), other._streamBuffer.data()); _streamBuffer.commit(bytes); }; AdcNetServiceASIOBase(const AdcNetServiceASIOBase&) = delete; // no copy constructor! virtual ~AdcNetServiceASIOBase() {} constexpr netservice_ident_t ident() const { return _ident; } /* asynchronuos methods */ template TokenT, traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_ACCEPT_TIMEOUT)> auto asyncAccept(const endpoint_t& endpoint, TokenT&& token, const TimeoutT& timeout = DEFAULT_ACCEPT_TIMEOUT) { // no acceptor for UDP-sockets if constexpr (std::is_null_pointer_v) { static_assert(false, "INVALID TRANSPORT PROTOCOL TYPE!"); } // auto acc = acceptor_t(_ioContext); // auto timer = getDeadlineTimer(acc, timeout); auto timer = getDeadlineTimer(_acceptor, timeout); return asio::async_compose( // [acc = std::move(acc), timer = std::move(timer), start = true, &endpoint, this]( [timer = std::move(timer), start = true, &endpoint, this](auto& self, std::error_code ec = {}) mutable { if (!ec) { if (start) { start = false; try { // acc = acceptor_t(_ioContext, endpoint); if (!_acceptor.is_open() || (_acceptor.local_endpoint() != endpoint)) { _acceptor = acceptor_t(_ioContext, endpoint); } } catch (std::system_error err) { timer->cancel(); self.complete(err.code()); return; } // return acc.async_accept(_socket, std::move(self)); return _acceptor.async_accept(_socket, std::move(self)); } } if (isTimeout(timer, ec)) { ec = std::make_error_code(std::errc::timed_out); } else { // an error occured in async_connect timer->cancel(); } self.complete(ec); }, token, _ioContext); } template TokenT, traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_CONNECT_TIMEOUT)> auto asyncConnect(const endpoint_t& endpoint, TokenT&& token, const TimeoutT& timeout = DEFAULT_CONNECT_TIMEOUT) { auto timer = getDeadlineTimer(_socket, timeout); return asio::async_compose( [start = true, endpoint, timer = std::move(timer), this](auto& self, asio::error_code ec = {}) mutable { if (!ec) { if (start) { start = false; return _socket.async_connect(endpoint, std::move(self)); } } if (isTimeout(timer, ec)) { ec = std::make_error_code(std::errc::timed_out); } else { // an error occured in async_connect timer->cancel(); } self.complete(ec); }, token, _socket); } template TokenT, traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_SEND_TIMEOUT)> auto asyncSend(const MessageT& msg, TokenT&& token, const TimeoutT& timeout = DEFAULT_SEND_TIMEOUT) { // create buffer sequence of sending session protocol representation of the input message std::vector buff_seq; std::ranges::for_each(this->toProto(msg), [&buff_seq](const auto& el) { buff_seq.emplace_back(el); }); auto timer = getDeadlineTimer(_socket, timeout); return asio::async_compose( [start = true, buff_seq = std::move(buff_seq), timer = std::move(timer), this]( auto& self, asio::error_code ec = {}) mutable { if (!ec) { if (start) { start = false; if constexpr (std::derived_from>) { return asio::async_write(_socket, buff_seq, std::move(self)); } else if constexpr (std::derived_from>) { return _socket.async_send(buff_seq, std::move(self)); } else if constexpr (std::derived_from>) { return _socket.async_send(buff_seq, std::move(self)); } else { static_assert(false, "UNKNOWN ASIO-LIBRARY SOCKET TYPE!!!"); } } } if (isTimeout(timer, ec)) { ec = std::make_error_code(std::errc::timed_out); } else { // an error occured in async_write/async_send timer->cancel(); } self.complete(ec); }, token, _socket); } template auto asyncReceive(TokenT&& token, const TimeoutT& timeout = DEFAULT_RECEIVE_TIMEOUT) { constexpr auto is_async_ctx_t = std::same_as, async_call_ctx_t>; // check completion token signature and deduce message type if constexpr (!adc_asio_special_comp_token_c && !is_async_ctx_t) { static_assert(traits::adc_func_traits::arity == 2, "INVALID COMPLETION TOKEN SIGNATURE!"); static_assert(std::is_same_v>, std::error_code>, "INVALID COMPLETION TOKEN SIGNATURE!"); static_assert(traits::adc_output_char_range< std::tuple_element_t<1, typename traits::adc_func_traits::args_t>>, "INVALID COMPLETION TOKEN SIGNATURE!"); } using msg_t = std::conditional_t< adc_asio_special_comp_token_c || is_async_ctx_t, RMSGT, std::remove_cvref_t::args_t>>>; auto out_flags = std::make_shared(); auto timer = getDeadlineTimer(_socket, timeout); return asio::async_compose( [out_flags, do_read = true, timer = std::move(timer), this](auto& self, asio::error_code ec = {}, size_t nbytes = 0) mutable { // RMSGT msg; msg_t msg; if (!ec) { if (do_read) { do_read = false; if (_receiveQueue.size()) { // return message from queue timer->cancel(); auto imsg = _receiveQueue.front(); _receiveQueue.pop(); if constexpr (std::is_same_v) { self.complete(std::error_code(), std::move(imsg)); } else { self.complete(std::error_code(), {imsg.begin(), imsg.end()}); } return; } if constexpr (std::derived_from>) { return asio::async_read(_socket, _streamBuffer, asio::transfer_at_least(1), std::move(self)); } else if constexpr (std::derived_from>) { // datagram, so it should be received at once return _socket.receive(_streamBuffer, std::move(self)); } else if constexpr (std::derived_from>) { // datagram, so it should be received at once return _socket.receive(_streamBuffer, *out_flags, std::move(self)); } else { static_assert(false, "UNKNOWN ASIO-LIBRARY SOCKET TYPE!!!"); } } // if (!nbytes) { // do_read = true; // asio::post(std::move(self)); // initiate consequence socket's read operation // return; // } auto start_ptr = static_cast(_streamBuffer.data().data()); // auto sr = this->search(std::span(start_ptr, _streamBuffer.size())); auto net_pack = this->search(std::span(start_ptr, _streamBuffer.size())); // if (!std::get<2>(sr)) { if (net_pack.empty()) { do_read = true; asio::post(std::move(self)); // initiate consequence socket's read operation return; } timer->cancel(); // there were no errors in the asynchronous read-operation, so stop timer // here one has at least a single message // size_t N = std::distance(std::get<0>(sr), std::get<1>(sr)); // auto net_pack = std::span{start_ptr, N}; std::ranges::copy(this->fromProto(net_pack), std::back_inserter(msg)); _streamBuffer.consume(net_pack.size()); while (_streamBuffer.size()) { // search for possible additional session protocol packets start_ptr = static_cast(_streamBuffer.data().data()); // sr = this->search(std::span(start_ptr, _streamBuffer.size())); net_pack = this->search(std::span(start_ptr, _streamBuffer.size())); if (!net_pack.empty()) { // if (std::get<2>(sr)) { // N = std::distance(std::get<0>(sr), std::get<1>(sr)); // net_pack = std::span{start_ptr, N}; _receiveQueue.emplace(); std::ranges::copy(this->fromProto(net_pack), std::back_inserter(_receiveQueue.back())); _streamBuffer.consume(net_pack.size()); } else { break; // exit and hold remaining bytes in stream buffer } } } if (isTimeout(timer, ec)) { ec = std::make_error_code(std::errc::timed_out); } else { // an error occured in async_* timer->cancel(); } if constexpr (std::is_same_v) { self.complete(ec, std::move(msg)); } else { // msg_t user_msg{msg.begin(), msg.end()}; self.complete(ec, {msg.begin(), msg.end()}); } }, token, _socket); } /* blocking methods */ template auto accept(const endpoint_t& endpoint, const TimeoutT& timeout = DEFAULT_ACCEPT_TIMEOUT) { std::future ftr = asyncAccept(endpoint, asio::use_future, timeout); ftr.get(); } template auto connect(const endpoint_t& endpoint, const TimeoutT& timeout = DEFAULT_CONNECT_TIMEOUT) { std::future ftr = asyncConnect(endpoint, asio::use_future, timeout); ftr.get(); } template auto send(const R& msg, const TimeoutT& timeout = DEFAULT_SEND_TIMEOUT) { std::future ftr = asyncSend(msg, timeout, asio::use_future); ftr.get(); } template auto receive(const TimeoutT& timeout = DEFAULT_RECEIVE_TIMEOUT) { std::future ftr = asyncReceive(timeout, asio::use_future); return ftr.get(); } std::error_code close() { std::error_code ec; _socket.shutdown(_shutdownType, ec); if (!ec) { _socket.close(ec); } return ec; } /* additional ASIO-related methods */ void clear() { // clear receiving messages queue // NOTE: there is no racing condition here since using asio::strand! asio::post(_receiveStrand, [this]() { _receiveQueue = {}; }); } void setShutdownType(asio::socket_base::shutdown_type shutdown_type) { _shutdownType = shutdown_type; } asio::socket_base::shutdown_type getShutdownType() const { return _shutdownType; } protected: static constexpr netservice_ident_t _ident = std::derived_from> ? "STREAM-SOCKET NETWORK SERVICE" : std::derived_from> ? "DATAGRAM-SOCKET NETWORK SERVICE" : std::derived_from> ? "SEQPACKET-SOCKET NETWORK SERVICE" : "UNKNOWN"; asio::io_context& _ioContext; asio::io_context::strand _receiveStrand; socket_t _socket; acceptor_t _acceptor; asio::streambuf _streamBuffer; std::queue> _receiveQueue; asio::socket_base::shutdown_type _shutdownType = asio::socket_base::shutdown_both; template static std::unique_ptr getDeadlineTimer(CancelableT& obj, const TimeoutT& timeout, bool arm = true) { auto timer = std::make_unique(obj.get_executor()); // if (timeout == std::chrono::duration::max()) { // return timer; // do not arm the timer if MAX duration are given // } if (arm) { std::chrono::seconds max_d = std::chrono::duration_cast( std::chrono::steady_clock::time_point::max() - std::chrono::steady_clock::now() - std::chrono::seconds(1)); timer->expires_after(timeout < max_d ? timeout : max_d); // to avoid overflow! // timer->expires_after(timeout); timer->async_wait([&obj](const std::error_code& ec) mutable { if (!ec) { obj.cancel(); } }); } return timer; } template 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); } }; // static_assert(adc::interfaces::adc_netservice_c>>, // ""); static_assert(adc::interfaces::adc_netsession_proto_c>, ""); } // namespace adc::impl