192 lines
8.4 KiB
C++
192 lines
8.4 KiB
C++
#pragma once
|
|
|
|
/*
|
|
|
|
ABSTRACT DEVICE COMPONENTS LIBRARY
|
|
|
|
*/
|
|
|
|
#include <chrono>
|
|
#include <concepts>
|
|
|
|
#include "../common/adc_traits.h"
|
|
|
|
/* DEFINITIONS OF NETWORK COMPONENTS INTERFACES */
|
|
|
|
|
|
namespace adc::traits
|
|
{
|
|
|
|
|
|
template <typename T>
|
|
// from https://stackoverflow.com/questions/74383254/concept-that-models-only-the-stdchrono-duration-types
|
|
concept adc_time_duration_c = requires {
|
|
[]<class Rep, class Period>(std::type_identity<std::chrono::duration<Rep, Period>>) {}(std::type_identity<T>());
|
|
};
|
|
|
|
|
|
template <adc_time_duration_c... Ts>
|
|
struct adc_duration_common_type;
|
|
|
|
|
|
template <adc_time_duration_c T1, adc_time_duration_c T2>
|
|
struct adc_duration_common_type<T1, T2> : std::common_type<T1, T2> {
|
|
};
|
|
|
|
|
|
template <adc_time_duration_c T1, adc_time_duration_c T2, adc_time_duration_c... Ts>
|
|
struct adc_duration_common_type<T1, T2, Ts...> : adc_duration_common_type<std::common_type_t<T1, T2>, Ts...> {
|
|
};
|
|
|
|
|
|
template <adc_time_duration_c... Ts>
|
|
using adc_duration_common_type_t = typename adc_duration_common_type<Ts...>::type;
|
|
|
|
|
|
/* all STL helper duration types */
|
|
using adc_common_duration_t = adc_duration_common_type_t<std::chrono::nanoseconds,
|
|
std::chrono::microseconds,
|
|
std::chrono::milliseconds,
|
|
std::chrono::seconds,
|
|
std::chrono::minutes,
|
|
std::chrono::hours,
|
|
std::chrono::days,
|
|
std::chrono::weeks,
|
|
std::chrono::months,
|
|
std::chrono::years>;
|
|
|
|
/*
|
|
struct NetService {
|
|
typedef ImplementationDependentT netservice_ident_t;
|
|
typedef ImplementationDependentT async_ctx_t;
|
|
typedef ImplementationDependentT endpoint_t;
|
|
|
|
template<typename Rep, typename Period>
|
|
auto asyncAccept(const endpoint_t&, async_ctx_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period>
|
|
auto asyncConnect(const endpoint_t&, async_ctx_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period, adc::traits::adc_input_char_range R>
|
|
auto asyncSend(const R&, async_ctx_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period>
|
|
auto asyncReceived(async_ctx_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period>
|
|
auto accept(const endpoint_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period>
|
|
auto connect(const endpoint_t&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<typename Rep, typename Period, adc::traits::adc_input_char_range R>
|
|
auto send(const R&, const std::chrono::duration<Rep, Period>&)
|
|
|
|
template<adc::traits::adc_output_char_range R, typename Rep, typename Period>
|
|
R received(const std::chrono::duration<Rep, Period>&)
|
|
|
|
}
|
|
*/
|
|
template <typename SRVT,
|
|
typename SMSGT = std::string_view, // sending message type
|
|
typename RMSGT = std::string, // receiving message type
|
|
typename DURT = adc_common_duration_t // time duration type
|
|
>
|
|
concept adc_netservice_c = adc_input_char_range<SMSGT> && adc_output_char_range<RMSGT> && adc_time_duration_c<DURT> &&
|
|
requires(SRVT srv, const SRVT srv_const) {
|
|
typename SRVT::netservice_ident_t;
|
|
|
|
// netservice_ident_t ident() const
|
|
{ srv_const.ident() } -> std::same_as<typename SRVT::netservice_ident_t>;
|
|
|
|
typename SRVT::async_ctx_t;
|
|
typename SRVT::endpoint_t;
|
|
|
|
// asynchronous (non-blocking) operations
|
|
asyncAccept(std::declval<const typename SRVT::endpoint_t&>(),
|
|
std::declval<typename SRVT::async_ctx_t&>(), std::declval<const DURT&>());
|
|
|
|
asyncConnect(std::declval<const typename SRVT::endpoint_t&>(),
|
|
std::declval<typename SRVT::async_ctx_t&>(), std::declval<const DURT&>());
|
|
|
|
asyncSend(std::declval<const SMSGT&>(), std::declval<typename SRVT::async_ctx_t&>(),
|
|
std::declval<const DURT&>());
|
|
|
|
asyncReceive(std::declval<typename SRVT::async_ctx_t&>(), std::declval<const DURT&>());
|
|
|
|
// synchronous (blocking) operations
|
|
accept(std::declval<const typename SRVT::endpoint_t&>(), std::declval<const DURT&>());
|
|
|
|
connect(std::declval<const typename SRVT::endpoint_t&>(), std::declval<const DURT&>());
|
|
|
|
send(std::declval<const SMSGT&>(), std::declval<const DURT&>());
|
|
|
|
{ receive(std::declval<const DURT&>()) } -> std::same_as<RMSGT>;
|
|
|
|
// requires requires {
|
|
// [](SRVT& srv_obj, const typename SRVT::endpoint_t& endpoint, const typename
|
|
// SRVT::async_ctx_t& ctx,
|
|
// const DURT& timeout) {
|
|
// srv_obj.asyncAccept(endpoint, ctx, timeout);
|
|
// srv_obj.asyncConnect(endpoint, ctx, timeout);
|
|
|
|
// srv_obj.accept(endpoint, timeout);
|
|
// srv_obj.connect(endpoint, timeout);
|
|
// }(srv, typename SRVT::endpoint_t(), typename SRVT::async_ctx_t(),
|
|
// std::chrono::seconds(1));
|
|
|
|
// []<adc::traits::adc_input_char_range R>(SRVT& srv_obj, const R& msg, const
|
|
// typename SRVT::async_ctx_t& ctx,
|
|
// const DURT& timeout) {
|
|
// srv_obj.asyncSend(msg, ctx, timeout);
|
|
// srv_obj.send(msg, timeout);
|
|
// }(srv, std::span<const char>(), typename SRVT::async_ctx_t(),
|
|
// std::chrono::seconds(1));
|
|
|
|
// []<adc::traits::adc_output_char_range R>(SRVT& srv_obj, R& msg, const typename
|
|
// SRVT::async_ctx_t& ctx,
|
|
// const DURT& timeout) {
|
|
// srv_obj.asyncReceive(ctx, timeout);
|
|
// msg = srv_obj.receive(timeout);
|
|
// }(srv, std::string(), typename SRVT::async_ctx_t(), std::chrono::seconds(1));
|
|
// };
|
|
};
|
|
|
|
|
|
/* NETWORK SESSION */
|
|
|
|
template <typename SESST>
|
|
concept adc_netsession_c =
|
|
std::derived_from<SESST, std::enable_shared_from_this<SESST>> && requires(SESST sess, const SESST sess_const) {
|
|
typename SESST::netsession_ident_t;
|
|
|
|
// netsession_ident_t ident() const
|
|
{ sess_const.ident() } -> std::same_as<typename SESST::netsession_ident_t>;
|
|
|
|
sess.start();
|
|
sess.stop();
|
|
};
|
|
|
|
|
|
|
|
/* NETWORK SESSION-LEVEL PROTOCOL */
|
|
|
|
template <typename SESS_PROTOT>
|
|
concept adc_netsession_proto_c = requires(SESS_PROTOT proto, const SESS_PROTOT proto_const) {
|
|
typename SESS_PROTOT::proto_ident_t;
|
|
|
|
// proto_ident_t ident() const (const method)
|
|
{ proto_const.ident() } -> std::same_as<typename SESS_PROTOT::proto_ident_t>;
|
|
|
|
[]<std::input_iterator IT>(const SESS_PROTOT& proto, IT begin, IT end) -> std::tuple<IT, IT, bool> {
|
|
return proto.parse(begin, end);
|
|
};
|
|
|
|
// must return a view of R-range!
|
|
[]<std::ranges::range R>(SESS_PROTOT obj, const R& r) -> std::ranges::view auto { return obj.from(r); }(
|
|
proto, std::string());
|
|
};
|
|
|
|
|
|
} // namespace adc::traits
|