This commit is contained in:
Timur A. Fatkhullin
2024-06-15 21:23:57 +03:00
parent daf4e1eab9
commit 9a2baa702d
5 changed files with 182 additions and 116 deletions

View File

@@ -1,6 +1,13 @@
#pragma once
/*
ABSTRACT DEVICE COMPONENTS LIBRARY
*/
#include "../common/adc_traits.h"
namespace adc
@@ -13,20 +20,21 @@ namespace adc
* endpoint: proto_mark://host_name:port_num/path
* where "part" is optional for all protocol kinds;
*
* for "local" kind protocol the endpoint string must consists of
* for the "local" kind protocol the endpoint string must consists of
* only "proto_mark" and "host_name" fields
* (e.g.: local://APP_UNIX_SOCKET)
*
* NOTE: "proto_mark" field is parsed as case-insensitive string!
*
*/
class AdcEndpoint
{
protected:
public:
static constexpr std::string_view protoHostDelim = "://";
static constexpr std::string_view hostPortDelim = ":";
static constexpr std::string_view portPathDelim = "/";
public:
enum proto_id_t : uint8_t {
PROTO_ID_LOCAL,
PROTO_ID_TCP,
@@ -47,6 +55,50 @@ public:
static constexpr std::array validProtoMarks = {protoMarkLocal, protoMarkTCP, protoMarkTLS,
protoMarkUDP, protoMarkWS, protoMarkWSS};
// factory methods
template <traits::adc_input_char_range R>
AdcEndpoint createLocal(R&& path)
{
return AdcEndpoint(PROTO_ID_LOCAL, std::string_view(""), -1, std::forward<R>(path));
}
template <traits::adc_input_char_range HT>
AdcEndpoint createTCP(HT&& host, int port)
{
return AdcEndpoint(PROTO_ID_TCP, std::forward<HT>(host), port);
}
#ifdef USE_OPENSSL_WITH_ASIO
template <traits::adc_input_char_range HT>
AdcEndpoint createTLS(HT&& host, int port)
{
return AdcEndpoint(PROTO_ID_TLS, std::forward<HT>(host), port);
}
#endif
template <traits::adc_input_char_range HT>
AdcEndpoint createUDP(HT&& host, int port)
{
return AdcEndpoint(PROTO_ID_UDP, std::forward<HT>(host), port);
}
template <traits::adc_input_char_range HT, traits::adc_input_char_range PTT = std::string_view>
AdcEndpoint createWS(HT&& host, int port, PTT&& path = PTT())
{
return AdcEndpoint(PROTO_ID_WS, std::forward<HT>(host), port, std::forward<PTT>(path));
}
template <traits::adc_input_char_range HT, traits::adc_input_char_range PTT = std::string_view>
AdcEndpoint createWSS(HT&& host, int port, PTT&& path = PTT())
{
return AdcEndpoint(PROTO_ID_WSS, std::forward<HT>(host), port, std::forward<PTT>(path));
}
/* Constructors and destructor */
AdcEndpoint() = default;
@@ -255,8 +307,10 @@ public:
auto sz = std::distance(r.begin(), found.begin());
std::string proto;
std::ranges::copy(r | std::views::take(sz), std::back_inserter(proto));
std::string proto; // case-insensitive!
std::ranges::copy(
r | std::views::take(sz) | std::views::transform([](auto ch) -> char { return std::tolower(ch); }),
std::back_inserter(proto));
std::underlying_type_t<proto_id_t> i = 0;
proto_id_t id = PROTO_ID_UNKNOWN;