...
This commit is contained in:
parent
4fd0550de3
commit
ecca565f3f
@ -68,7 +68,6 @@ if (ASIO_LIBRARY)
|
|||||||
|
|
||||||
set(ADC_NETWORK_HEADERS ${ADC_NETWORK_HEADERS}
|
set(ADC_NETWORK_HEADERS ${ADC_NETWORK_HEADERS}
|
||||||
net/asio/adc_netservice_asio.h
|
net/asio/adc_netservice_asio.h
|
||||||
net/asio/adc_netsession_asio.h
|
|
||||||
net/asio/adc_device_netserver_asio.h
|
net/asio/adc_device_netserver_asio.h
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -112,7 +112,6 @@ public:
|
|||||||
|
|
||||||
typedef std::vector<char> message_t;
|
typedef std::vector<char> message_t;
|
||||||
|
|
||||||
// Session(const netsession_ident_t& id, netservice_t srv, AdcDeviceNetServer* srv_ptr)
|
|
||||||
Session(const netsession_ident_t& id, netservice_t srv, netsession_ctx_t ctx)
|
Session(const netsession_ident_t& id, netservice_t srv, netsession_ctx_t ctx)
|
||||||
: _ident(id),
|
: _ident(id),
|
||||||
_netService(std::move(srv)),
|
_netService(std::move(srv)),
|
||||||
@ -189,7 +188,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
ServerResponseType type;
|
ServerResponseType type;
|
||||||
auto attrs = checkserverCall<default_server_resp_t>(key, rmsg, type);
|
auto attrs = checkServerCall<default_server_resp_t>(key, rmsg, type);
|
||||||
|
|
||||||
std::forward<CallbackT>(std::get<0>(wrapper))(type, attrs);
|
std::forward<CallbackT>(std::get<0>(wrapper))(type, attrs);
|
||||||
|
|
||||||
@ -372,7 +371,7 @@ public:
|
|||||||
// helper methods
|
// helper methods
|
||||||
|
|
||||||
template <traits::adc_range_of_output_char_range R>
|
template <traits::adc_range_of_output_char_range R>
|
||||||
R checkserverCall(std::string_view key, const auto& bytes, ServerResponseType& type) const
|
R checkServerCall(std::string_view key, const auto& bytes, ServerResponseType& type) const
|
||||||
{
|
{
|
||||||
AdcDeviceProtoMessage dev_msg(bytes);
|
AdcDeviceProtoMessage dev_msg(bytes);
|
||||||
|
|
||||||
@ -396,7 +395,7 @@ public:
|
|||||||
{
|
{
|
||||||
auto rbytes = sendRecv(bytes);
|
auto rbytes = sendRecv(bytes);
|
||||||
|
|
||||||
return checkserverCall<R>(key, rbytes, type);
|
return checkServerCall<R>(key, rbytes, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include <asio/basic_stream_socket.hpp>
|
#include <asio/basic_stream_socket.hpp>
|
||||||
#include <asio/bind_executor.hpp>
|
#include <asio/bind_executor.hpp>
|
||||||
#include <asio/compose.hpp>
|
#include <asio/compose.hpp>
|
||||||
|
#include <asio/connect.hpp>
|
||||||
#include <asio/deferred.hpp>
|
#include <asio/deferred.hpp>
|
||||||
#include <asio/ip/tcp.hpp>
|
#include <asio/ip/tcp.hpp>
|
||||||
#include <asio/ip/udp.hpp>
|
#include <asio/ip/udp.hpp>
|
||||||
@ -124,6 +125,10 @@ public:
|
|||||||
static constexpr bool isTLS = false;
|
static constexpr bool isTLS = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef TRANSPORT_PROTOT transport_proto_t;
|
||||||
|
typedef SESSION_PROTOT session_proto_t;
|
||||||
|
typedef RMSGT receive_msg_t;
|
||||||
|
|
||||||
// typedefs to satisfy 'adc_netservice_c' concept
|
// typedefs to satisfy 'adc_netservice_c' concept
|
||||||
typedef std::string_view netservice_ident_t;
|
typedef std::string_view netservice_ident_t;
|
||||||
|
|
||||||
@ -793,6 +798,17 @@ public:
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <std::derived_from<AdcBaseNetServiceASIO> ST,
|
||||||
|
typename EptRangeT,
|
||||||
|
asio::completion_token_for<std::error_code, endpoint_t> CallbackT>
|
||||||
|
friend auto async_connect(ST& service, const EptRangeT& ept_range, CallbackT&& token)
|
||||||
|
requires std::ranges::range<EptRangeT> &&
|
||||||
|
std::same_as<std::ranges::range_value_t<EptRangeT>, AdcBaseNetServiceASIO::endpoint_t>
|
||||||
|
{
|
||||||
|
return asio::async_connect(service._socket, ept_range, token);
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static constexpr netservice_ident_t _ident =
|
static constexpr netservice_ident_t _ident =
|
||||||
std::derived_from<socket_t, asio::basic_stream_socket<typename socket_t::protocol_type>>
|
std::derived_from<socket_t, asio::basic_stream_socket<typename socket_t::protocol_type>>
|
||||||
@ -931,7 +947,8 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* */
|
/* PARTIAL AdcBaseNetServiceASIO-CLASS SPECIALIZATIONS */
|
||||||
|
|
||||||
template <
|
template <
|
||||||
adc_asio_transport_proto_c TRANSPORT_PROTOT, // transport-level proto (e.g. asio::ip::tcp)
|
adc_asio_transport_proto_c TRANSPORT_PROTOT, // transport-level proto (e.g. asio::ip::tcp)
|
||||||
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT, // session-level proto (see ../adc_netproto.h)
|
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT, // session-level proto (see ../adc_netproto.h)
|
||||||
|
|||||||
@ -1,198 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "adc_netservice_asio.h"
|
|
||||||
|
|
||||||
namespace adc::impl
|
|
||||||
{
|
|
||||||
|
|
||||||
template <typename SessionContextT,
|
|
||||||
adc_asio_transport_proto_c TRANSPORT_PROTOT,
|
|
||||||
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT,
|
|
||||||
traits::adc_output_char_range RMSGT = std::vector<char>>
|
|
||||||
class AdcGenericNetSessionASIO : public std::enable_shared_from_this<
|
|
||||||
AdcGenericNetSessionASIO<SessionContextT, TRANSPORT_PROTOT, SESSION_PROTOT, RMSGT>>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef std::string netsession_ident_t;
|
|
||||||
|
|
||||||
typedef SessionContextT netsession_ctx_t;
|
|
||||||
|
|
||||||
typedef AdcNetServiceASIOBase<TRANSPORT_PROTOT, SESSION_PROTOT, RMSGT> netservice_t;
|
|
||||||
typedef std::shared_ptr<netservice_t> netservice_sptr_t;
|
|
||||||
|
|
||||||
template <traits::adc_input_char_range R>
|
|
||||||
AdcGenericNetSessionASIO(const R& id, netservice_sptr_t netservice, netsession_ctx_t&& context)
|
|
||||||
: _ident(), _netservice(std::move(netservice)), _sessionContext(std::forward<netsession_ctx_t>(context))
|
|
||||||
{
|
|
||||||
if constexpr (std::is_array_v<R>) {
|
|
||||||
_ident = id;
|
|
||||||
} else {
|
|
||||||
_ident = std::string(id.begin(), id.end());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AdcGenericNetSessionASIO(netservice_sptr_t netservice, netsession_ctx_t&& context)
|
|
||||||
: AdcGenericNetSessionASIO(
|
|
||||||
std::derived_from<TRANSPORT_PROTOT, asio::ip::tcp> ? "ASIO TCP SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::ip::udp> ? "ASIO UDP SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::local::seq_packet_protocol> ? "ASIO UNIX SEQPACKET SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::local::stream_protocol> ? "ASIO UNIX STREAM SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::local::datagram_protocol> ? "ASIO UNIX DATAGRAM SESSION"
|
|
||||||
: "ASIO UNKNOWN",
|
|
||||||
std::move(netservice),
|
|
||||||
std::forward<netsession_ctx_t>(context))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
virtual ~AdcGenericNetSessionASIO()
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
netsession_ident_t ident() const
|
|
||||||
{
|
|
||||||
return _ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
virtual void start() = 0;
|
|
||||||
|
|
||||||
virtual void stop()
|
|
||||||
{
|
|
||||||
_netservice->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <traits::adc_time_duration_c TimeoutT>
|
|
||||||
AdcGenericNetSessionASIO& setDefaultTimeouts(const TimeoutT& send_timeout, const TimeoutT& recv_timeout)
|
|
||||||
{
|
|
||||||
_sendTimeout = send_timeout;
|
|
||||||
_recvTimeout = recv_timeout;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
netsession_ident_t _ident;
|
|
||||||
|
|
||||||
netservice_sptr_t _netservice;
|
|
||||||
|
|
||||||
netsession_ctx_t _sessionContext;
|
|
||||||
|
|
||||||
std::chrono::duration<std::chrono::seconds::rep, std::chrono::seconds::period> _recvTimeout =
|
|
||||||
std::chrono::seconds::max();
|
|
||||||
|
|
||||||
std::chrono::duration<std::chrono::seconds::rep, std::chrono::seconds::period> _sendTimeout =
|
|
||||||
std::chrono::seconds(5);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
class AdcGenericNetSessionASIO
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef std::string netsession_ident_t;
|
|
||||||
|
|
||||||
template <traits::adc_input_char_range R,
|
|
||||||
adc_asio_transport_proto_c TRANSPORT_PROTOT,
|
|
||||||
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT,
|
|
||||||
traits::adc_output_char_range RMSGT = std::vector<char>,
|
|
||||||
traits::adc_is_callable RECV_MSG_TOKENT>
|
|
||||||
AdcGenericNetSessionASIO(const R& id,
|
|
||||||
std::shared_ptr<AdcNetServiceASIOBase<TRANSPORT_PROTOT, SESSION_PROTOT, RMSGT>> netservice,
|
|
||||||
RECV_MSG_TOKENT&& recv_msg_token)
|
|
||||||
: _ident(id.begin(), id.end())
|
|
||||||
{
|
|
||||||
// check receive message completion token signature and deduce message type
|
|
||||||
if constexpr (!adc_asio_special_comp_token_c<RECV_MSG_TOKENT>) {
|
|
||||||
static_assert(traits::adc_func_traits<RECV_MSG_TOKENT>::arity == 2, "INVALID COMPLETION TOKEN SIGNATURE!");
|
|
||||||
static_assert(
|
|
||||||
std::is_same_v<std::remove_cvref_t<traits::adc_func_arg1_t<RECV_MSG_TOKENT>>, std::error_code>,
|
|
||||||
"INVALID COMPLETION TOKEN SIGNATURE!");
|
|
||||||
static_assert(traits::adc_output_char_range<
|
|
||||||
std::tuple_element_t<1, typename traits::adc_func_traits<RECV_MSG_TOKENT>::args_t>>,
|
|
||||||
"INVALID COMPLETION TOKEN SIGNATURE!");
|
|
||||||
}
|
|
||||||
|
|
||||||
using msg_t = std::conditional_t<
|
|
||||||
adc_asio_special_comp_token_c<RECV_MSG_TOKENT>, RMSGT,
|
|
||||||
std::remove_cvref_t<std::tuple_element_t<1, typename traits::adc_func_traits<RECV_MSG_TOKENT>::args_t>>>;
|
|
||||||
|
|
||||||
|
|
||||||
_startFunc = [netservice, wrapper = traits::adc_pf_wrapper(std::forward<RECV_MSG_TOKENT>(recv_msg_token)),
|
|
||||||
this]() {
|
|
||||||
//
|
|
||||||
netservice->asyncReceive(std::get<0>(wrapper), _recvTimeout);
|
|
||||||
};
|
|
||||||
|
|
||||||
_stopFunc = [netservice]() {
|
|
||||||
// stop
|
|
||||||
netservice->close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <adc_asio_transport_proto_c TRANSPORT_PROTOT,
|
|
||||||
interfaces::adc_netsession_proto_c<std::string_view> SESSION_PROTOT,
|
|
||||||
traits::adc_output_char_range RMSGT = std::vector<char>,
|
|
||||||
traits::adc_is_callable RECV_MSG_TOKENT>
|
|
||||||
AdcGenericNetSessionASIO(std::shared_ptr<AdcNetServiceASIOBase<TRANSPORT_PROTOT, SESSION_PROTOT, RMSGT>> netservice,
|
|
||||||
RECV_MSG_TOKENT&& recv_msg_token)
|
|
||||||
: AdcGenericNetSessionASIO(std::derived_from<TRANSPORT_PROTOT, asio::ip::tcp> ? "TCP SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::ip::udp> ? "UDP SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::local::seq_packet_protocol>
|
|
||||||
? "UNIX SEQPACKET SESSION"
|
|
||||||
: std::derived_from<TRANSPORT_PROTOT, asio::local::stream_protocol> ? "UNIX STREAM
|
|
||||||
SESSION" : "UNKNOWN", std::move(netservice), std::forward<RECV_MSG_TOKENT>(recv_msg_token))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
virtual ~AdcGenericNetSessionASIO()
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
netsession_ident_t ident() const
|
|
||||||
{
|
|
||||||
return _ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void start()
|
|
||||||
{
|
|
||||||
_startFunc();
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop()
|
|
||||||
{
|
|
||||||
_stopFunc();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <traits::adc_time_duration_c TimeoutT>
|
|
||||||
AdcGenericNetSessionASIO& setDefaultTimeouts(const TimeoutT& send_timeout, const TimeoutT& recv_timeout)
|
|
||||||
{
|
|
||||||
_sendTimeout = send_timeout;
|
|
||||||
_recvTimeout = recv_timeout;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
netsession_ident_t _ident;
|
|
||||||
|
|
||||||
std::function<void()> _startFunc;
|
|
||||||
std::function<void()> _stopFunc;
|
|
||||||
|
|
||||||
std::chrono::duration<std::chrono::seconds::rep, std::chrono::seconds::period> _recvTimeout =
|
|
||||||
std::chrono::seconds::max();
|
|
||||||
|
|
||||||
std::chrono::duration<std::chrono::seconds::rep, std::chrono::seconds::period> _sendTimeout =
|
|
||||||
std::chrono::seconds(5);
|
|
||||||
};
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
} // namespace adc::impl
|
|
||||||
Loading…
x
Reference in New Issue
Block a user