net/adc_netproto.h: add AdcStartStopSeqSessionProto class

This commit is contained in:
Timur A. Fatkhullin 2025-01-24 09:16:54 +03:00
parent 8bbba2e236
commit 434521a435

View File

@ -21,6 +21,7 @@ concept adc_netproto_c = adc_range_of_view_char_range<R> && requires(T t, const
namespace constants
{
static constexpr char ADC_DEFAULT_NETPROTO_STARTSEQ[] = "\v";
static constexpr char ADC_DEFAULT_NETPROTO_STOPSEQ[] = "\n";
static constexpr char ADC_DEFAULT_NETPROTO_STARTMARK[] = "\n\t\r\v\r\t\n";
@ -39,7 +40,10 @@ struct AdcStopSeqSessionProto {
typedef std::string proto_ident_t;
proto_ident_t ident() const { return "STOP SEQUENCE PROTO"; }
proto_ident_t ident() const
{
return "STOP SEQUENCE PROTO";
}
// template <traits::adc_input_char_range R>
// auto search(const R& r)
@ -142,6 +146,105 @@ struct AdcStopSeqSessionProto {
};
template <const char* STARTSEQ = constants::ADC_DEFAULT_NETPROTO_STARTSEQ,
const char* STOPSEQ = constants::ADC_DEFAULT_NETPROTO_STOPSEQ>
struct AdcStartStopSeqSessionProto {
static constexpr std::string_view START_SEQ{STARTSEQ};
static constexpr size_t START_SEQ_SIZE = START_SEQ.size();
static constexpr std::string_view STOP_SEQ{STOPSEQ};
static constexpr size_t STOP_SEQ_SIZE = STOP_SEQ.size();
static_assert(START_SEQ_SIZE, "START BYTE SEQUENCE MUST NOT BE AN EMPTY ONE!!!");
static_assert(STOP_SEQ_SIZE, "STOP BYTE SEQUENCE MUST NOT BE AN EMPTY ONE!!!");
typedef std::string proto_ident_t;
proto_ident_t ident() const
{
return "STOP SEQUENCE PROTO";
}
template <traits::adc_input_char_range R>
auto search(const R& r)
{
auto func = [&r](auto& result) {
if (std::ranges::size(r) < (START_SEQ_SIZE + STOP_SEQ_SIZE)) {
return;
}
auto found_start = std::ranges::search(r, START_SEQ);
if (found_start.empty()) {
return;
}
auto found_stop = std::ranges::search(r, STOP_SEQ);
if (found_stop.empty()) {
return;
}
if (found_start.begin() > found_stop.begin()) {
return;
}
if constexpr (std::ranges::viewable_range<R>) {
result = std::span(found_start.begin(), found_stop.end());
} else {
std::ranges::copy(found_start.begin(), found_stop.end(), std::back_inserter(result));
}
};
if constexpr (std::ranges::viewable_range<R>) {
auto res = std::span(r.begin(), r.begin()); // empty std::span
func(res);
return res;
} else {
auto res = std::vector<char>();
func(res);
return res;
}
}
template <traits::adc_input_char_range R>
auto toProto(const R& r)
{
// return 3-element array with the first element - a view of the start sequence, the second one - as a view of
// the input range and the third one - a view of the stop sequence
if constexpr (std::ranges::viewable_range<R>) {
auto res = std::array{std::span(std::span(START_SEQ.begin(), START_SEQ.end()), r.begin(), r.end()),
std::span(STOP_SEQ.begin(), STOP_SEQ.end())};
return res;
} else { // return a copy of input range with added to the fron start sequence and appended stop sequence
std::decay_t<decltype(r)> res;
std::ranges::copy(START_SEQ, std::back_inserter(res));
std::ranges::copy(r, std::back_inserter(res));
std::ranges::copy(STOP_SEQ, std::back_inserter(res));
return res;
}
}
template <traits::adc_input_char_range R>
auto fromProto(const R& r)
{
size_t N = std::distance(r.begin(), r.end());
if (N < (START_SEQ_SIZE + STOP_SEQ_SIZE)) { // one must ensure for input range size correctness
return std::span(r.begin(), r.begin());
}
if constexpr (std::ranges::viewable_range<R>) {
return std::span(r.begin() + START_SEQ_SIZE, r.end() - STOP_SEQ_SIZE);
} else {
R res;
std::ranges::copy(r | std::views::drop(START_SEQ_SIZE) | std::views::take(N - STOP_SEQ_SIZE),
std::back_inserter(res));
return res;
}
}
};
// session protocol for datagram-kind message (e.g. UDP, seqpacket)
// The protocol does not contain any special characters, so to/fromProto methods
// return just a copy/one-element-array-of-vew/view of input byte
@ -149,7 +252,10 @@ struct AdcStopSeqSessionProto {
struct AdcDatagramSessionProto {
typedef std::string proto_ident_t;
proto_ident_t ident() const { return "DATAGRAM PROTO"; }
proto_ident_t ident() const
{
return "DATAGRAM PROTO";
}
template <traits::adc_input_char_range R>
auto search(const R& r)
@ -201,7 +307,10 @@ struct AdcBinaryBlobSessionProto {
typedef std::string proto_ident_t;
proto_ident_t ident() const { return "BINARY BLOB PROTO"; }
proto_ident_t ident() const
{
return "BINARY BLOB PROTO";
}
template <traits::adc_input_char_range R>
auto search(const R& r)