381 lines
14 KiB
C++
381 lines
14 KiB
C++
#pragma once
|
|
|
|
#include "../concepts/snplib_concepts.h"
|
|
#include "../utils/snplib_string.h"
|
|
#include "snipplib/network/snplib_network.h"
|
|
|
|
/*
|
|
|
|
Various simple network session protocols
|
|
|
|
*/
|
|
|
|
namespace snplib
|
|
{
|
|
/* TRIVIAL NO-OP PROTOCOL */
|
|
|
|
struct netproto_noop_t {
|
|
// copy
|
|
template <output_char_range_c OR, input_char_range_c IR>
|
|
OR toNetwork(IR&& message)
|
|
{
|
|
OR res;
|
|
|
|
std::ranges::copy(std::forward<IR>(message), std::back_inserter(res));
|
|
|
|
return res;
|
|
}
|
|
|
|
template <input_char_range_c IR>
|
|
auto toNetwork(IR&& message)
|
|
{
|
|
if constexpr (std::ranges::contiguous_range<IR>) {
|
|
return std::span<const char>{message};
|
|
} else {
|
|
return std::ranges::subrange(message);
|
|
}
|
|
}
|
|
|
|
|
|
template <std::ranges::range OR, input_char_range_c IR>
|
|
std::expected<OR, std::error_code> fromNetwork(IR&& bytes)
|
|
{
|
|
using inp_t = std::remove_cvref_t<IR>;
|
|
|
|
// return a view
|
|
static constexpr bool ret_view =
|
|
std::ranges::view<OR> && std::ranges::viewable_range<inp_t> &&
|
|
(std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::iterator_t<inp_t>> ||
|
|
std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::sentinel_t<inp_t>>);
|
|
|
|
|
|
if constexpr (ret_view) {
|
|
return OR(bytes.begin(), bytes.end());
|
|
} else if constexpr (output_char_range_c<OR>) {
|
|
if constexpr (!fixed_size_range_c<OR>) {
|
|
if constexpr (std::constructible_from<OR, std::ranges::iterator_t<inp_t>,
|
|
std::ranges::iterator_t<inp_t>>) {
|
|
return OR(bytes.begin(), bytes.end());
|
|
} else if constexpr (std::is_default_constructible_v<OR>) {
|
|
OR res;
|
|
std::ranges::copy(
|
|
std::forward<IR>(bytes) | std::views::take(std::distance(bytes.begin(), bytes.end())),
|
|
std::back_inserter(res));
|
|
|
|
return res;
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
} else {
|
|
auto Nelem = std::distance(bytes.begin(), bytes.end());
|
|
if (Nelem > std::tuple_size_v<OR>) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_seek));
|
|
}
|
|
|
|
OR res;
|
|
auto data_it = bytes.begin();
|
|
for (auto it = res.begin(); it != res.end(); std::advance(it, 1)) {
|
|
*it = *data_it;
|
|
std::advance(data_it, 1);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
}
|
|
|
|
// deduces a view-type from the input range-type
|
|
template <typename IR>
|
|
auto fromNetwork(IR&& bytes)
|
|
requires input_char_range_c<std::remove_cvref_t<IR>>
|
|
{
|
|
using or_t =
|
|
std::conditional_t<std::ranges::contiguous_range<IR>, std::span<const char>,
|
|
std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>;
|
|
|
|
return fromNetwork<or_t>(std::forward<IR>(bytes));
|
|
}
|
|
|
|
// to satisfy 'net_proto_c' concept
|
|
template <typename CharT = char>
|
|
requires(std::same_as<CharT, char> || std::same_as<CharT, const char>)
|
|
std::expected<std::span<CharT>, std::error_code> fromNetwork(std::span<CharT> bytes)
|
|
{
|
|
return fromNetwork<std::span<CharT>>(bytes);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
/* SIMPLE STOP-SEQUENCE PROTOCOL */
|
|
|
|
/*
|
|
A valid record consists of a byte-sequence ending with given stop-sequence of bytes
|
|
e.g.:
|
|
SET ATTR 2\n\r\n
|
|
where "\n\r\n" - the stop-sequence
|
|
*/
|
|
|
|
static constexpr char SNPLIB_DEFAULT_NETPROTO_STOP_SEQ[] = "\n";
|
|
|
|
template <const char* STOP_SEQ = SNPLIB_DEFAULT_NETPROTO_STOP_SEQ>
|
|
requires(char_array_size(STOP_SEQ) > 0)
|
|
struct netproto_stopseq_t {
|
|
static constexpr std::string_view NETPROTO_STOP_SEQ{STOP_SEQ};
|
|
|
|
// returns 2-element range of the input messsage and stop-sequence
|
|
template <std::ranges::range OR, input_char_range_c IR>
|
|
requires char_range_c<std::ranges::range_value_t<OR>> // range of char ranges
|
|
OR toNetwork(IR&& message)
|
|
{
|
|
OR res;
|
|
|
|
std::back_inserter(res) = std::forward<IR>(message);
|
|
std::back_inserter(res) = std::remove_cvref_t<IR>{NETPROTO_STOP_SEQ.begin(), NETPROTO_STOP_SEQ.end()};
|
|
|
|
return res;
|
|
}
|
|
|
|
// returns a copy of the input message + tralling stop-sequence
|
|
template <output_char_range_c OR, input_char_range_c IR>
|
|
OR toNetwork(IR&& message)
|
|
{
|
|
OR res;
|
|
|
|
std::ranges::copy(std::forward<IR>(message), std::back_inserter(res));
|
|
std::ranges::copy(NETPROTO_STOP_SEQ, std::back_inserter(res));
|
|
|
|
return res;
|
|
}
|
|
|
|
template <input_char_range_c IR>
|
|
auto toNetwork(IR&& message)
|
|
{
|
|
if constexpr (std::ranges::contiguous_range<IR>) {
|
|
return std::array{std::span<const char>{message}, std::span<const char>{NETPROTO_STOP_SEQ}};
|
|
} else {
|
|
// return std::array{std::ranges::subrange(message), std::ranges::subrange(NETPROTO_STOP_SEQ)};
|
|
return std::make_tuple(std::ranges::subrange(message), std::ranges::subrange(NETPROTO_STOP_SEQ));
|
|
}
|
|
}
|
|
|
|
template <std::ranges::range OR, input_char_range_c IR>
|
|
std::expected<OR, std::error_code> fromNetwork(IR&& bytes)
|
|
{
|
|
auto found = std::ranges::search(std::forward<IR>(bytes), NETPROTO_STOP_SEQ);
|
|
if (found.empty()) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_argument));
|
|
}
|
|
|
|
using inp_t = std::remove_cvref_t<IR>;
|
|
|
|
// return a view
|
|
static constexpr bool ret_view =
|
|
std::ranges::view<OR> && std::ranges::viewable_range<inp_t> &&
|
|
(std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::iterator_t<inp_t>> ||
|
|
std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::sentinel_t<inp_t>>);
|
|
|
|
|
|
if constexpr (ret_view) {
|
|
return OR(bytes.begin(), found.begin()); // return without stop-sequence!!!
|
|
} else if constexpr (output_char_range_c<OR>) {
|
|
if constexpr (!fixed_size_range_c<OR>) {
|
|
if constexpr (std::constructible_from<OR, std::ranges::iterator_t<inp_t>,
|
|
std::ranges::iterator_t<inp_t>>) {
|
|
return OR(bytes.begin(), found.begin()); // return without stop-sequence!!!
|
|
} else if constexpr (std::is_default_constructible_v<OR>) {
|
|
OR res;
|
|
std::ranges::copy(
|
|
std::forward<IR>(bytes) | std::views::take(std::distance(bytes.begin(), found.begin())),
|
|
std::back_inserter(res));
|
|
|
|
return res;
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
} else {
|
|
auto Nelem = std::distance(bytes.begin(), found.begin());
|
|
if (Nelem > std::tuple_size_v<OR>) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_seek));
|
|
}
|
|
|
|
OR res;
|
|
auto data_it = bytes.begin();
|
|
for (auto it = res.begin(); it != res.end(); std::advance(it, 1)) {
|
|
*it = *data_it;
|
|
std::advance(data_it, 1);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
}
|
|
|
|
// deduces a view-type from the input range-type
|
|
template <typename IR>
|
|
auto fromNetwork(IR&& bytes)
|
|
requires input_char_range_c<std::remove_cvref_t<IR>>
|
|
{
|
|
using or_t =
|
|
std::conditional_t<std::ranges::contiguous_range<IR>, std::span<const char>,
|
|
std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>;
|
|
|
|
return fromNetwork<or_t>(std::forward<IR>(bytes));
|
|
}
|
|
|
|
// to satisfy 'net_proto_c' concept
|
|
template <typename CharT = char>
|
|
requires(std::same_as<CharT, char> || std::same_as<CharT, const char>)
|
|
std::expected<std::span<CharT>, std::error_code> fromNetwork(std::span<CharT> bytes)
|
|
{
|
|
return fromNetwork<std::span<CharT>>(bytes);
|
|
}
|
|
};
|
|
|
|
|
|
static_assert(net_repr_proto_c<netproto_stopseq_t<>>, "!!!");
|
|
|
|
|
|
/* BINARY BLOB PROTOCOL */
|
|
|
|
/*
|
|
a sequence of bytes in format:
|
|
<blob size field><binary blob bytes>
|
|
|
|
blob size field: a symbolic representation of a HEXIMAL UNSIGNED INTEGER
|
|
(e.g. "000000FA", MUST BE IN UPPER-CASE!!!)
|
|
*/
|
|
|
|
template <size_t SZ_FIELD_WIDTH = 8>
|
|
requires(SZ_FIELD_WIDTH > 0 &&
|
|
SZ_FIELD_WIDTH <= 16) // not greater than allowed for 64-bit unsigned integer representation
|
|
struct netproto_binary_blob_t {
|
|
static constexpr size_t BLOB_SIZE_FIELD{SZ_FIELD_WIDTH};
|
|
|
|
static constexpr std::array<char, 6> BLOB_SIZE_FIELD_FORMAT{'{', ':', '0', '0' + BLOB_SIZE_FIELD, 'X', '}'};
|
|
|
|
template <input_char_range_c IR>
|
|
auto toNetwork(IR&& message)
|
|
{
|
|
static thread_local std::string Nbytes{};
|
|
|
|
Nbytes.clear();
|
|
// std::format_to(std::back_inserter(Nbytes), "{:08X}", std::ranges::size(std::forward<IR>(message)));
|
|
std::format_to(std::back_inserter(Nbytes), std::string_view(BLOB_SIZE_FIELD_FORMAT),
|
|
std::ranges::size(std::forward<IR>(message)));
|
|
|
|
if constexpr (std::ranges::contiguous_range<IR>) {
|
|
return std::array{std::span<const char>{Nbytes}, std::span<const char>{message}};
|
|
} else {
|
|
return std::make_tuple(std::ranges::subrange(Nbytes), std::ranges::subrange(message));
|
|
}
|
|
}
|
|
|
|
|
|
template <char_range_c OR, input_char_range_c IR>
|
|
std::expected<OR, std::error_code> fromNetwork(IR&& bytes)
|
|
{
|
|
using inp_t = std::remove_cvref_t<IR>;
|
|
|
|
// return a view
|
|
static constexpr bool ret_view =
|
|
std::ranges::view<OR> && std::ranges::viewable_range<inp_t> &&
|
|
(std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::iterator_t<inp_t>> ||
|
|
std::constructible_from<OR, std::ranges::iterator_t<inp_t>, std::ranges::sentinel_t<inp_t>>);
|
|
|
|
|
|
auto bytes_sz = std::ranges::size(std::forward<IR>(bytes));
|
|
|
|
if (bytes_sz < BLOB_SIZE_FIELD) { // not enough data
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_seek));
|
|
}
|
|
|
|
std::optional<size_t> blob_sz;
|
|
if constexpr (std::ranges::contiguous_range<IR>) {
|
|
blob_sz = char_range_to_number<size_t>(std::span(bytes.begin(), BLOB_SIZE_FIELD), 16);
|
|
} else {
|
|
blob_sz = char_range_to_number<size_t>(bytes | std::views::take(BLOB_SIZE_FIELD), 16);
|
|
}
|
|
|
|
if (!blob_sz) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_argument));
|
|
}
|
|
|
|
bytes_sz -= BLOB_SIZE_FIELD;
|
|
|
|
if (bytes_sz < blob_sz.value()) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_seek));
|
|
}
|
|
|
|
auto it_start = std::forward<IR>(bytes).begin();
|
|
std::advance(it_start, BLOB_SIZE_FIELD);
|
|
auto it_end = std::forward<IR>(bytes).begin();
|
|
std::advance(it_end, BLOB_SIZE_FIELD + blob_sz.value());
|
|
|
|
if constexpr (ret_view) {
|
|
return OR(it_start, it_end); // return without blob size field
|
|
} else if constexpr (output_char_range_c<OR>) {
|
|
if constexpr (!fixed_size_range_c<OR>) {
|
|
if constexpr (std::constructible_from<OR, std::ranges::iterator_t<inp_t>,
|
|
std::ranges::iterator_t<inp_t>>) {
|
|
return OR(it_start, it_end); // return without blob size field!!!
|
|
} else if constexpr (std::is_default_constructible_v<OR>) {
|
|
OR res;
|
|
std::ranges::copy(
|
|
std::forward<IR>(bytes) | std::views::drop(BLOB_SIZE_FIELD) | std::views::take(blob_sz.value()),
|
|
std::back_inserter(res));
|
|
|
|
return res;
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
} else {
|
|
if (blob_sz.value() > std::tuple_size_v<OR>) {
|
|
return std::unexpected(std::make_error_code(std::errc::invalid_seek));
|
|
}
|
|
|
|
OR res;
|
|
for (auto it = res.begin(); it != res.end(); std::advance(it, 1)) {
|
|
*it = *it_start;
|
|
std::advance(it_start, 1);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
} else {
|
|
static_assert(false, "Unsupported output byte sequence type!");
|
|
}
|
|
}
|
|
|
|
// deduces a view-type from the input range-type
|
|
template <typename IR>
|
|
auto fromNetwork(IR&& bytes)
|
|
requires input_char_range_c<std::remove_cvref_t<IR>>
|
|
{
|
|
using or_t =
|
|
std::conditional_t<std::ranges::contiguous_range<IR>, std::span<const char>,
|
|
std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>;
|
|
|
|
return fromNetwork<or_t>(std::forward<IR>(bytes));
|
|
}
|
|
|
|
// to satisfy 'net_proto_c' concept
|
|
template <typename CharT = char>
|
|
requires(std::same_as<CharT, char> || std::same_as<CharT, const char>)
|
|
std::expected<std::span<CharT>, std::error_code> fromNetwork(std::span<CharT> bytes)
|
|
{
|
|
return fromNetwork<std::span<CharT>>(bytes);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
static_assert(net_repr_proto_c<netproto_binary_blob_t<>>, "!!!");
|
|
|
|
|
|
|
|
} // namespace snplib
|