#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 OR toNetwork(IR&& message) { OR res; std::ranges::copy(std::forward(message), std::back_inserter(res)); return res; } template auto toNetwork(IR&& message) { if constexpr (std::ranges::contiguous_range) { return std::span{message}; } else { return std::ranges::subrange(message); } } template std::expected fromNetwork(IR&& bytes) { using inp_t = std::remove_cvref_t; // return a view static constexpr bool ret_view = std::ranges::view && std::ranges::viewable_range && (std::constructible_from, std::ranges::iterator_t> || std::constructible_from, std::ranges::sentinel_t>); if constexpr (ret_view) { return OR(bytes.begin(), bytes.end()); } else if constexpr (output_char_range_c) { if constexpr (!fixed_size_range_c) { if constexpr (std::constructible_from, std::ranges::iterator_t>) { return OR(bytes.begin(), bytes.end()); } else if constexpr (std::is_default_constructible_v) { OR res; std::ranges::copy( std::forward(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) { 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 auto fromNetwork(IR&& bytes) requires input_char_range_c> { using or_t = std::conditional_t, std::span, std::ranges::subrange, std::ranges::sentinel_t>>; return fromNetwork(std::forward(bytes)); } // to satisfy 'net_proto_c' concept template requires(std::same_as || std::same_as) std::expected, std::error_code> fromNetwork(std::span bytes) { return fromNetwork>(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 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 requires char_range_c> // range of char ranges OR toNetwork(IR&& message) { OR res; std::back_inserter(res) = std::forward(message); std::back_inserter(res) = std::remove_cvref_t{NETPROTO_STOP_SEQ.begin(), NETPROTO_STOP_SEQ.end()}; return res; } // returns a copy of the input message + tralling stop-sequence template OR toNetwork(IR&& message) { OR res; std::ranges::copy(std::forward(message), std::back_inserter(res)); std::ranges::copy(NETPROTO_STOP_SEQ, std::back_inserter(res)); return res; } template auto toNetwork(IR&& message) { if constexpr (std::ranges::contiguous_range) { return std::array{std::span{message}, std::span{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::expected fromNetwork(IR&& bytes) { auto found = std::ranges::search(std::forward(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; // return a view static constexpr bool ret_view = std::ranges::view && std::ranges::viewable_range && (std::constructible_from, std::ranges::iterator_t> || std::constructible_from, std::ranges::sentinel_t>); if constexpr (ret_view) { return OR(bytes.begin(), found.begin()); // return without stop-sequence!!! } else if constexpr (output_char_range_c) { if constexpr (!fixed_size_range_c) { if constexpr (std::constructible_from, std::ranges::iterator_t>) { return OR(bytes.begin(), found.begin()); // return without stop-sequence!!! } else if constexpr (std::is_default_constructible_v) { OR res; std::ranges::copy( std::forward(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) { 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 auto fromNetwork(IR&& bytes) requires input_char_range_c> { using or_t = std::conditional_t, std::span, std::ranges::subrange, std::ranges::sentinel_t>>; return fromNetwork(std::forward(bytes)); } // to satisfy 'net_proto_c' concept template requires(std::same_as || std::same_as) std::expected, std::error_code> fromNetwork(std::span bytes) { return fromNetwork>(bytes); } }; static_assert(net_repr_proto_c>, "!!!"); /* BINARY BLOB PROTOCOL */ /* a sequence of bytes in format: blob size field: a symbolic representation of a HEXIMAL UNSIGNED INTEGER (e.g. "000000FA", MUST BE IN UPPER-CASE!!!) */ template 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 BLOB_SIZE_FIELD_FORMAT{'{', ':', '0', '0' + BLOB_SIZE_FIELD, 'X', '}'}; template 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(message))); std::format_to(std::back_inserter(Nbytes), std::string_view(BLOB_SIZE_FIELD_FORMAT), std::ranges::size(std::forward(message))); if constexpr (std::ranges::contiguous_range) { return std::array{std::span{Nbytes}, std::span{message}}; } else { return std::make_tuple(std::ranges::subrange(Nbytes), std::ranges::subrange(message)); } } template std::expected fromNetwork(IR&& bytes) { using inp_t = std::remove_cvref_t; // return a view static constexpr bool ret_view = std::ranges::view && std::ranges::viewable_range && (std::constructible_from, std::ranges::iterator_t> || std::constructible_from, std::ranges::sentinel_t>); auto bytes_sz = std::ranges::size(std::forward(bytes)); if (bytes_sz < BLOB_SIZE_FIELD) { // not enough data return std::unexpected(std::make_error_code(std::errc::invalid_seek)); } std::optional blob_sz; if constexpr (std::ranges::contiguous_range) { blob_sz = char_range_to_number(std::span(bytes.begin(), BLOB_SIZE_FIELD), 16); } else { blob_sz = char_range_to_number(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(bytes).begin(); std::advance(it_start, BLOB_SIZE_FIELD); auto it_end = std::forward(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) { if constexpr (!fixed_size_range_c) { if constexpr (std::constructible_from, std::ranges::iterator_t>) { return OR(it_start, it_end); // return without blob size field!!! } else if constexpr (std::is_default_constructible_v) { OR res; std::ranges::copy( std::forward(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) { 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 auto fromNetwork(IR&& bytes) requires input_char_range_c> { using or_t = std::conditional_t, std::span, std::ranges::subrange, std::ranges::sentinel_t>>; return fromNetwork(std::forward(bytes)); } // to satisfy 'net_proto_c' concept template requires(std::same_as || std::same_as) std::expected, std::error_code> fromNetwork(std::span bytes) { return fromNetwork>(bytes); } }; static_assert(net_repr_proto_c>, "!!!"); } // namespace snplib