This commit is contained in:
2026-10-08 22:55:35 +03:00
parent 9f8647be8c
commit d14a531dbb

View File

@@ -13,10 +13,15 @@
namespace snplib
{
// default implementation of the "net_repr_proto_result_c"
enum class net_repr_proto_result_flag_e : int { OK, NOT_ENOUGH_DATA, INVALID_DATA };
template <char_range_or_range_of_char_ranges_c OR>
struct net_repr_proto_result_t {
enum class result_flag_t : int { OK, NOT_ENOUGH_DATA, INVALID_DATA };
// enum class result_flag_t : int { OK, NOT_ENOUGH_DATA, INVALID_DATA };
typedef net_repr_proto_result_flag_e result_flag_t;
result_flag_t flag{result_flag_t::INVALID_DATA};
@@ -26,6 +31,8 @@ struct net_repr_proto_result_t {
static_assert(net_repr_proto_result_c<net_repr_proto_result_t<std::span<const char>>>, "!!!");
static_assert(std::ranges::output_range<std::span<char>, char>);
/* TRIVIAL NO-OP PROTOCOL */
struct netproto_noop_t {
@@ -39,7 +46,9 @@ struct netproto_noop_t {
template <input_char_range_c IR>
auto toNetwork(IR&& message) -> std::conditional_t<
std::ranges::contiguous_range<IR>,
net_repr_proto_result_t<std::span<const char>>,
net_repr_proto_result_t<std::conditional_t<std::is_const_v<std::ranges::range_value_t<IR>>,
std::span<const char>,
std::span<char>>>,
net_repr_proto_result_t<std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>>
{
if constexpr (std::ranges::contiguous_range<IR>) {
@@ -101,14 +110,24 @@ struct _netproto_stopseq_t {
using res_el_t = std::ranges::range_value_t<OR>;
net_repr_proto_result_t<OR> res{.flag = net_repr_proto_result_t<OR>::result_flag_t::OK};
std::back_inserter(res.result) = transform_char_range<res_el_t>(std::forward<IR>(message));
std::back_inserter(res.result) = transform_char_range<res_el_t>(NETPROTO_STOP_SEQ);
if constexpr (fixed_size_range_c<OR>) {
static_assert(std::tuple_size_v<OR> > 1, "Invalid range size!!!");
auto it = std::forward<IR>(message).begin();
*it = transform_char_range<res_el_t>(std::forward<IR>(message));
std::ranges::advance(it, 1);
*it = transform_char_range<res_el_t>(NETPROTO_STOP_SEQ);
} else {
std::back_inserter(res.result) = transform_char_range<res_el_t>(std::forward<IR>(message));
std::back_inserter(res.result) = transform_char_range<res_el_t>(NETPROTO_STOP_SEQ);
}
return res;
}
// returns a copy of the input message + tralling stop-sequence
template <output_char_range_c OR, input_char_range_c IR>
requires(!char_view_c<OR>)
net_repr_proto_result_t<OR> toNetwork(IR&& message)
{
net_repr_proto_result_t<OR> res{.flag = net_repr_proto_result_t<OR>::result_flag_t::OK};
@@ -118,9 +137,93 @@ struct _netproto_stopseq_t {
return res;
}
template <input_char_range_c IR>
auto toNetwork(IR&& message) -> std::conditional_t<
std::ranges::contiguous_range<IR>,
net_repr_proto_result_t<std::array<std::span<std::ranges::range_value_t<IR>>, 2>>,
net_repr_proto_result_t<std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>>
{
if constexpr (std::ranges::contiguous_range<IR>) {
return toNetwork<std::span<const char>>(std::forward<IR>(message));
} else {
return toNetwork<std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>(
std::forward<IR>(message));
}
}
template <char_range_c OR, char_range_c IR>
net_repr_proto_result_t<OR> fromNetwork(IR&& bytes)
{
net_repr_proto_result_t<OR> res{.flag = net_repr_proto_result_flag_e::NOT_ENOUGH_DATA};
auto found = std::ranges::search(std::forward<IR>(bytes), NETPROTO_STOP_SEQ);
if (found.empty()) {
return res;
}
res.flag = net_repr_proto_result_flag_e::OK;
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) {
res.result = 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>>) {
res.result = OR(bytes.begin(), found.begin()); // return without stop-sequence!!!
} else if constexpr (std::is_default_constructible_v<OR>) {
std::ranges::copy(
std::forward<IR>(bytes) | std::views::take(std::distance(bytes.begin(), found.begin())),
std::back_inserter(res.result));
} 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>) {
// let's assume a byte sequence was expected here; the length must be strictly adhered to.
// so, if the sequence length exceeds the capacity of the input array, the error
// "invalid data" should be returned
res.flag = net_repr_proto_result_flag_e::INVALID_DATA;
} else {
auto data_it = bytes.begin();
for (auto it = res.result.begin(); it != res.result.end(); std::advance(it, 1)) {
*it = *data_it;
std::advance(data_it, 1);
}
}
}
} else {
static_assert(false, "Unsupported output byte sequence type!");
}
return res;
}
// return a view of the input bytes except trailing stop-sequence
template <input_char_range_c IR>
auto fromNetwork(IR&& bytes) -> std::conditional_t<
std::ranges::contiguous_range<IR>,
net_repr_proto_result_t<std::span<std::ranges::range_value_t<IR>>>,
net_repr_proto_result_t<std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>>
{
return fromNetwork<std::span<std::ranges::range_value_t<IR>>>(std::forward<IR>(bytes));
}
};
static_assert(net_repr_proto_c<_netproto_stopseq_t<>>, "!!!");
template <const char* STOP_SEQ = SNPLIB_DEFAULT_NETPROTO_STOP_SEQ>
requires(char_array_size(STOP_SEQ) > 0)
struct netproto_stopseq_t {
@@ -238,7 +341,7 @@ struct netproto_stopseq_t {
};
static_assert(net_repr_proto_c<netproto_stopseq_t<>>, "!!!");
// static_assert(net_repr_proto_c<netproto_stopseq_t<>>, "!!!");
/* BINARY BLOB PROTOCOL */
@@ -251,6 +354,142 @@ static_assert(net_repr_proto_c<netproto_stopseq_t<>>, "!!!");
(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 <output_char_range_c OR, input_char_range_c IR>
requires(!char_view_c<OR>)
net_repr_proto_result_t<OR> toNetwork(IR&& message)
{
net_repr_proto_result_t<OR> res{.flag = net_repr_proto_result_flag_e::OK};
std::format_to(std::back_inserter(res.result), std::string_view(BLOB_SIZE_FIELD_FORMAT),
std::ranges::size(std::forward<IR>(message)));
std::format_to(std::back_inserter(res.result), "{}", std::forward<IR>(message));
return res;
}
template <input_char_range_c IR>
requires std::ranges::contiguous_range<IR>
auto toNetwork(IR&& message)
{
using char_t = std::ranges::range_value_t<IR>;
// using ret_t = net_repr_proto_result_t<std::array<std::span<char_t>, 2>>;
using ret_t = net_repr_proto_result_t<std::array<std::string_view, 2>>;
_nbytes.emplace_back("");
std::format_to(std::back_inserter(_nbytes.back()), std::string_view(BLOB_SIZE_FIELD_FORMAT),
std::ranges::size(std::forward<IR>(message)));
// return ret_t{.flag = net_repr_proto_result_flag_e::OK,
// .result = std::array{std::span<char_t>{_nbytes.back()}, std::span<char_t>(message)}};
return ret_t{.flag = net_repr_proto_result_flag_e::OK,
.result = std::array{std::string_view{_nbytes.back()}, std::string_view(message)}};
}
template <char_range_c OR, input_char_range_c IR>
net_repr_proto_result_t<OR> 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>>);
net_repr_proto_result_t<OR> res{.flag = net_repr_proto_result_flag_e::NOT_ENOUGH_DATA};
auto bytes_sz = std::ranges::size(std::forward<IR>(bytes));
if (bytes_sz < BLOB_SIZE_FIELD) { // not enough data
return res;
}
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) { // zero-size blob is not an error?!!
res.flag = net_repr_proto_result_flag_e::OK;
return res;
}
bytes_sz -= BLOB_SIZE_FIELD;
if (bytes_sz < blob_sz.value()) { // not enough data
return res;
}
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) {
res.result = 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>>) {
res.result = OR(it_start, it_end); // return without blob size field!!!
} else if constexpr (std::is_default_constructible_v<OR>) {
std::ranges::copy(
std::forward<IR>(bytes) | std::views::drop(BLOB_SIZE_FIELD) | std::views::take(blob_sz.value()),
std::back_inserter(res.result));
} else {
static_assert(false, "Unsupported output byte sequence type!");
}
res.flag = net_repr_proto_result_flag_e::OK;
} else {
if (blob_sz.value() > std::tuple_size_v<OR>) {
// let's assume a byte sequence was expected here; the length must be strictly adhered to.
// so, if the sequence length exceeds the capacity of the input array, the error
// "invalid data" should be returned
res.flag = net_repr_proto_result_flag_e::INVALID_DATA;
} else {
for (auto it = res.result.begin(); it != res.result.end(); std::advance(it, 1)) {
*it = *it_start;
std::advance(it_start, 1);
}
res.flag = net_repr_proto_result_flag_e::OK;
}
}
} else {
static_assert(false, "Unsupported output byte sequence type!");
}
return res;
}
// return a view of the input bytes except starting blob-size field
template <input_char_range_c IR>
auto fromNetwork(IR&& bytes) -> std::conditional_t<
std::ranges::contiguous_range<IR>,
net_repr_proto_result_t<std::span<std::ranges::range_value_t<IR>>>,
net_repr_proto_result_t<std::ranges::subrange<std::ranges::iterator_t<IR>, std::ranges::sentinel_t<IR>>>>
{
return fromNetwork<std::span<std::ranges::range_value_t<IR>>>(std::forward<IR>(bytes));
}
protected:
std::vector<std::string> _nbytes{};
};
static_assert(net_repr_proto_c<_netproto_binary_blob_t<>>, "!!!");
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