move implementations for ASIO-library to net/asio subdirectory

This commit is contained in:
2024-09-14 16:21:03 +03:00
parent 9818b5f2b8
commit a7626bfe5e
3 changed files with 460 additions and 255 deletions

View File

@@ -40,7 +40,6 @@
#include "adc_netmsg.h"
#include "adc_net_concepts.h"
namespace adc::traits
@@ -72,215 +71,6 @@ concept adc_asio_inet_stream_proto_c = requires(T t, asio_streambuff_iter_t begi
namespace adc::impl
{
template <typename T>
concept adc_asio_transport_proto_c =
std::derived_from<T, asio::ip::tcp> || std::derived_from<T, asio::ip::udp> ||
std::derived_from<T, asio::local::seq_packet_protocol> || std::derived_from<T, asio::local::stream_protocol>;
template <typename T>
concept adc_asio_stream_transport_proto_c =
std::derived_from<T, asio::ip::tcp> || std::derived_from<T, asio::local::stream_protocol>;
template <adc_asio_transport_proto_c TRANSPORT_PROTOT,
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT>
class AdcNetServiceASIOBase : public TRANSPORT_PROTOT, public SESSION_PROTOT
{
public:
typedef std::string netservice_ident_t;
using socket_t = typename TRANSPORT_PROTOT::socket;
using endpoint_t = typename TRANSPORT_PROTOT::endpoint;
struct asio_async_ctx_t {
bool use_future = false;
std::function<void(std::error_code)> accept_comp_token;
std::function<void(std::error_code)> connect_comp_token;
std::function<void(std::error_code)> send_comp_token;
template <traits::adc_output_char_range R>
static std::unordered_map<const asio_async_ctx_t*, std::function<void(std::error_code, const R&)>>
receive_comp_token;
};
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);
static constexpr std::chrono::duration DEFAULT_RECEIVE_TIMEOUT = std::chrono::seconds(5);
netservice_ident_t ident() const
{
return _ident;
}
template <traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_CONNECT_TIMEOUT)>
auto asyncConnect(const endpoint_t& endpoint,
asio_async_ctx_t& ctx,
const TimeoutT& timeout = DEFAULT_CONNECT_TIMEOUT)
{
auto timer = getDeadlineTimer(timeout);
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);
});
}
}
template <traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_ACCEPT_TIMEOUT)>
auto asyncAccept(const endpoint_t& endpoint,
asio_async_ctx_t& ctx,
const TimeoutT& timeout = DEFAULT_ACCEPT_TIMEOUT)
{
if constexpr (std::derived_from<socket_t, asio::basic_datagram_socket<typename socket_t::protocol_type>>) {
return; // there is no acceptor for UDP protocol
}
typename TRANSPORT_PROTOT::acceptor acceptor;
try {
acceptor = typename TRANSPORT_PROTOT::acceptor(_ioContext, endpoint);
} catch (std::system_error err) {
if (ctx.use_future) { // emulation of asio::use_future behaivior?!
throw;
}
ctx.accept_comp_token(err.code());
return;
}
auto timer = getDeadlineTimer(timeout);
if (ctx.use_future) {
return _socket.async_accept(
endpoint, asio::use_future([&ctx, timer = std::move(timer)](std::error_code) { timer->cancel(); }));
} else {
return _socket.async_accept(endpoint, [&ctx, timer = std::move(timer)](std::error_code ec) {
timer->cancel();
ctx.accept_comp_token(ec);
});
}
}
template <traits::adc_output_char_range R, traits::adc_time_duration_c TimeoutT = decltype(DEFAULT_RECEIVE_TIMEOUT)>
auto asyncReceive(asio_async_ctx_t& ctx, const TimeoutT& timeout = DEFAULT_RECEIVE_TIMEOUT)
{
auto timer = getDeadlineTimer(timeout);
auto s_res = std::make_shared<std::invoke_result_t<decltype(SESSION_PROTOT::search), R>>();
if constexpr (std::derived_from<socket_t, asio::basic_stream_socket<typename socket_t::protocol_type>>) {
return asio::async_read_until(
_socket, _streamBuffer,
[s_res, this]<typename IT>(IT begin, IT end) {
*s_res = this->search(std::span(begin, end));
return std::make_tuple(std::get<1>(*s_res), std::get<2>(*s_res));
},
[&ctx, s_res, timer = std::move(timer), this](std::error_code ec, size_t) {
timer->cancel();
R msg;
if (!ec) {
std::string_view net_pack{std::get<0>(*s_res), std::get<1>(*s_res)};
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
auto begin_it = (const char*)traits::asio_streambuff_iter_t::begin(_streamBuffer.data());
auto end_it = (const char*)traits::asio_streambuff_iter_t::end(_streamBuffer.data());
// static_cast<std::ranges::iterator_t<std::string_view>>(_streamBuffer.data().data());
// auto end_it = begin_it + _streamBuffer.data().size();
*s_res = this->search(std::span(begin_it, end_it));
if (std::get<2>(*s_res)) {
net_pack = std::string_view{std::get<0>(*s_res), std::get<1>(*s_res)};
std::ranges::copy(this->fromProto(net_pack), std::back_inserter(msg));
_streamBuffer.consume(net_pack.size());
// TODO: insert to queue
} else {
break;
}
}
}
ctx.accept_comp_token(ec, std::move(msg));
});
}
}
template <traits::adc_time_duration_c TimeoutT>
auto accept(const endpoint_t& endpoint, const TimeoutT& timeout)
{
asio_async_ctx_t ctx = {.use_future = true};
std::future<void> ftr = asyncAcept(endpoint, ctx, timeout);
ftr.get();
}
template <traits::adc_time_duration_c TimeoutT>
auto connect(const endpoint_t& endpoint, const TimeoutT& timeout)
{
asio_async_ctx_t ctx = {.use_future = true};
std::future<void> ftr = asyncConnect(endpoint, ctx, timeout);
ftr.get();
}
template <traits::adc_input_char_range R, traits::adc_time_duration_c TimeoutT>
auto send(const R& msg, const TimeoutT& timeout)
{
std::future<void> ftr = asyncSend(msg, timeout, asio::use_future);
ftr.get();
}
template <traits::adc_input_char_range R, traits::adc_time_duration_c TimeoutT>
auto receive(const TimeoutT& timeout)
{
std::future<R> ftr = asyncReceive(timeout, asio::use_future);
return ftr.get();
}
protected:
netservice_ident_t _ident;
asio::io_context& _ioContext;
socket_t _socket;
// acceptor_t _acceptor;
asio::streambuf _streamBuffer;
template <traits::adc_time_duration_c TimeoutT>
std::unique_ptr<asio::steady_timer> getDeadlineTimer(const TimeoutT& timeout, bool arm = true)
{
std::unique_ptr<asio::steady_timer> timer(_socket.get_executor());
if (arm) {
timer->expires_after(timeout);
timer->async_wait([this](const std::error_code& ec) {
if (!ec) {
_socket.cancel(std::make_error_code(std::errc::timed_out));
}
});
}
return timer;
}
};
template <traits::adc_asio_inet_proto_c InetProtoT>
class AdcNetServiceASIO : public InetProtoT
{