From 07b3d693113d3652d19fc07340647eb02ca2bddf Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Fri, 9 Oct 2026 23:33:42 +0300 Subject: [PATCH] rewrite tranport-level protocol classes --- examples/net_exam.cpp | 60 ++-- include/snipplib/network/snplib_netproto.h | 385 +++++---------------- include/snipplib/network/snplib_network.h | 6 +- 3 files changed, 128 insertions(+), 323 deletions(-) diff --git a/examples/net_exam.cpp b/examples/net_exam.cpp index fcc0533..14a204c 100644 --- a/examples/net_exam.cpp +++ b/examples/net_exam.cpp @@ -17,27 +17,27 @@ int main() std::println("BYTES VEC: {}", bytes_vec); - auto msg = proto.fromTransport(bytes_vec); + auto msg = proto.fromNetwork(bytes_vec); - if (msg) { - std::println("MSG (from std::vector): {}", msg.value()); + if (msg.flag == snplib::net_repr_proto_result_flag_e::OK) { + std::println("MSG (from std::vector): {}", msg.result); } else { - std::println("MSG ERR: {}", msg.error().message()); + std::println("MSG ERR: {}", std::to_underlying(msg.flag)); } - auto msg_l = proto.fromTransport(bytes_lst); + auto msg_l = proto.fromNetwork(bytes_lst); - if (msg_l) { - std::println("MSG (from std::list): {}", msg_l.value()); + if (msg_l.flag == snplib::net_repr_proto_result_flag_e::OK) { + std::println("MSG (from std::list): {}", msg_l.result); } else { - std::println("MSG ERR: {}", msg_l.error().message()); + std::println("MSG ERR: {}", std::to_underlying(msg_l.flag)); } - auto vmsg = proto.fromTransport>(bytes_lst); - if (vmsg) { - std::println("VMSG (from std::list): {}", vmsg.value()); + auto vmsg = proto.fromNetwork>(bytes_lst); + if (vmsg.flag == snplib::net_repr_proto_result_flag_e::OK) { + std::println("VMSG (from std::list): {}", vmsg.result); } else { - std::println("VMSG ERR: {}", vmsg.error().message()); + std::println("VMSG ERR: {}", std::to_underlying(vmsg.flag)); } @@ -49,14 +49,14 @@ int main() std::println("MSG: {}", bytes_vec); - auto bs = proto.toTransport(bytes_vec); - std::println("BYTES (from std::vector): {}", bs); + auto bs = proto.toNetwork(bytes_vec); + std::println("BYTES (from std::vector): {}", bs.result); - auto bs1 = proto.toTransport(bytes_lst); - std::println("BYTES (from std::list): {}", bs1); + // auto bs1 = proto.toNetwork(bytes_lst); + // std::println("BYTES (from std::list): {}", bs1.result); - bytes_vec = proto.toTransport(bytes_lst); - std::println("BYTES: {}", bytes_vec); + auto bs11 = proto.toNetwork(bytes_lst); + std::println("BYTES: {}", bs11.result); std::println("\n\n{:*^80}\n", " BINARY-BLOB PROTOCOL "); @@ -71,24 +71,28 @@ int main() std::println("BLOB: {}", bytes); - bs = bb_proto.toTransport(bytes_vec); - std::println("BYTES (from std::vector): {}", bs); + auto bs21 = bb_proto.toNetwork(bytes_vec); + std::println("BYTES (from std::vector): {}", bs21.result); - auto bs2 = bb_proto.toTransport(bytes_lst); - std::println("BYTES (from std::list): {}", bs2); + auto bs2 = bb_proto.toNetwork(bytes_lst); + std::println("BYTES (from std::list): {}", bs2.result); - auto blob = std::vector(std::from_range, bs | std::views::join); + // auto blob = std::vector(std::from_range, bs.result | std::views::join); + // auto blob = std::ranges::views::concat(std::get<0>(bs2.result), std::get<1>(bs2.result)); + std::vector blob; + std::ranges::copy(std::get<0>(bs2.result), std::back_inserter(blob)); + std::ranges::copy(std::get<1>(bs2.result), std::back_inserter(blob)); std::println("\nBYTES: {}", blob); - vmsg = bb_proto.fromTransport>(blob); - if (vmsg) { - std::println("BLOB (from std::vector): {}", vmsg.value()); + vmsg = bb_proto.fromNetwork>(blob); + if (vmsg.flag == snplib::net_repr_proto_result_flag_e::OK) { + std::println("BLOB (from std::vector): {}", vmsg.result); } else { - std::println("VMSG ERR: {}", vmsg.error().message()); + std::println("VMSG ERR: {}", std::to_underlying(vmsg.flag)); } - // std::array arr = bb_proto.toTransport(bytes_lst); + // std::array arr = bb_proto.toNetwork(bytes_lst); return 0; } \ No newline at end of file diff --git a/include/snipplib/network/snplib_netproto.h b/include/snipplib/network/snplib_netproto.h index 2bbf597..8114027 100644 --- a/include/snipplib/network/snplib_netproto.h +++ b/include/snipplib/network/snplib_netproto.h @@ -100,7 +100,7 @@ static constexpr char SNPLIB_DEFAULT_NETPROTO_STOP_SEQ[] = "\n"; template requires(char_array_size(STOP_SEQ) > 0) -struct _netproto_stopseq_t { +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 @@ -114,7 +114,7 @@ struct _netproto_stopseq_t { if constexpr (fixed_size_range_c) { static_assert(std::tuple_size_v > 1, "Invalid range size!!!"); - auto it = std::forward(message).begin(); + auto it = res.result.begin(); *it = transform_char_range(std::forward(message)); std::ranges::advance(it, 1); *it = transform_char_range(NETPROTO_STOP_SEQ); @@ -139,18 +139,33 @@ struct _netproto_stopseq_t { return res; } + // template + // auto toNetwork(IR&& message) -> std::conditional_t< + // std::ranges::contiguous_range, + // net_repr_proto_result_t>, 2>>, + // net_repr_proto_result_t, std::ranges::sentinel_t>>> + // { + // if constexpr (std::ranges::contiguous_range) { + // return toNetwork>(std::forward(message)); + // } else { + // return toNetwork, std::ranges::sentinel_t>>( + // std::forward(message)); + // } + // } + template - auto toNetwork(IR&& message) -> std::conditional_t< - std::ranges::contiguous_range, - net_repr_proto_result_t>, 2>>, - net_repr_proto_result_t, std::ranges::sentinel_t>>> + auto toNetwork(IR&& message) -> net_repr_proto_result_t< + std::array, + std::string_view, + std::ranges::subrange, std::ranges::sentinel_t>>, + 2>> { - if constexpr (std::ranges::contiguous_range) { - return toNetwork>(std::forward(message)); - } else { - return toNetwork, std::ranges::sentinel_t>>( - std::forward(message)); - } + using or_t = std::array< + std::conditional_t, std::string_view, + std::ranges::subrange, std::ranges::sentinel_t>>, + 2>; + + return toNetwork(std::forward(message)); } template @@ -168,11 +183,13 @@ struct _netproto_stopseq_t { 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>); + static constexpr bool ret_view = std::ranges::view && std::ranges::viewable_range; + // 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>); + // static_assert(std::ranges::view> && std::ranges::viewable_range>); if constexpr (ret_view) { res.result = OR(bytes.begin(), found.begin()); // return without stop-sequence!!! @@ -212,137 +229,25 @@ struct _netproto_stopseq_t { // return a view of the input bytes except trailing stop-sequence template - auto fromNetwork(IR&& bytes) -> std::conditional_t< - std::ranges::contiguous_range, - net_repr_proto_result_t>>, - net_repr_proto_result_t, std::ranges::sentinel_t>>> - { - return fromNetwork>>(std::forward(bytes)); - } -}; - - -static_assert(net_repr_proto_c<_netproto_stopseq_t<>>, "!!!"); - - -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) + auto fromNetwork(IR&& bytes) + // -> std::conditional_t< + // std::ranges::contiguous_range, + // net_repr_proto_result_t>>, + // net_repr_proto_result_t, std::ranges::sentinel_t>>> { if constexpr (std::ranges::contiguous_range) { - return std::array{std::span{message}, std::span{NETPROTO_STOP_SEQ}}; + // return fromNetwork>>(std::forward(bytes)); + return fromNetwork(std::forward(bytes)); } 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)); + return fromNetwork, std::ranges::sentinel_t>>( + std::forward(bytes)); } } - - 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>, "!!!"); +static_assert(net_repr_proto_c>, "!!!"); + /* BINARY BLOB PROTOCOL */ @@ -358,11 +263,12 @@ struct netproto_stopseq_t { 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 { +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', '}'}; + // blob size filed and copy of the input message template requires(!char_view_c) net_repr_proto_result_t toNetwork(IR&& message) @@ -376,29 +282,39 @@ struct _netproto_binary_blob_t { return res; } + // template + // requires std::ranges::contiguous_range + // auto toNetwork(IR&& message) + // { + // using ret_t = net_repr_proto_result_t>; + + // return ret_t{.flag = net_repr_proto_result_flag_e::OK, + // .result = std::make_tuple(std::format(std::string_view(BLOB_SIZE_FIELD_FORMAT), + // std::ranges::size(std::forward(message))), + // std::string_view(message))}; + // } + template - requires std::ranges::contiguous_range auto toNetwork(IR&& message) { - using char_t = std::ranges::range_value_t; - // using ret_t = net_repr_proto_result_t, 2>>; - // using ret_t = net_repr_proto_result_t>; - using ret_t = net_repr_proto_result_t>; + // using ret_t = net_repr_proto_result_t>; - // _nbytes.emplace_back(""); + using ret_t = net_repr_proto_result_t, std::string_view, + std::ranges::subrange, std::ranges::sentinel_t>>>>; - // std::format_to(std::back_inserter(_nbytes.back()), std::string_view(BLOB_SIZE_FIELD_FORMAT), - // std::ranges::size(std::forward(message))); - - - // return ret_t{.flag = net_repr_proto_result_flag_e::OK, - // .result = std::array{std::span{_nbytes.back()}, std::span(message)}}; - // return ret_t{.flag = net_repr_proto_result_flag_e::OK, - // .result = std::array{std::string_view{_nbytes.back()}, std::string_view(message)}}; - return ret_t{.flag = net_repr_proto_result_flag_e::OK, - .result = std::make_tuple(std::format(std::string_view(BLOB_SIZE_FIELD_FORMAT), - std::ranges::size(std::forward(message))), - std::string_view(message))}; + if constexpr (std::ranges::contiguous_range) { + return ret_t{.flag = net_repr_proto_result_flag_e::OK, + .result = std::make_tuple(std::format(std::string_view(BLOB_SIZE_FIELD_FORMAT), + std::ranges::size(std::forward(message))), + std::string_view(message))}; + } else { + return ret_t{.flag = net_repr_proto_result_flag_e::OK, + .result = std::make_tuple(std::format(std::string_view(BLOB_SIZE_FIELD_FORMAT), + std::ranges::size(std::forward(message))), + std::ranges::subrange(std::forward(message)))}; + } } template @@ -481,148 +397,33 @@ struct _netproto_binary_blob_t { } // return a view of the input bytes except starting blob-size field - template - auto fromNetwork(IR&& bytes) -> std::conditional_t< - std::ranges::contiguous_range, - net_repr_proto_result_t>>, - net_repr_proto_result_t, std::ranges::sentinel_t>>> - { - return fromNetwork>>(std::forward(bytes)); - } - - // protected: - // std::vector _nbytes{}; -}; - -static_assert(net_repr_proto_c<_netproto_binary_blob_t<>>, "!!!"); - - -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 fromNetwork(IR&& bytes) -> std::conditional_t< + // std::ranges::contiguous_range, + // net_repr_proto_result_t>>, + // net_repr_proto_result_t, std::ranges::sentinel_t>>> + // { + // return fromNetwork>>(std::forward(bytes)); + // } 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> + // -> std::conditional_t< + // std::ranges::contiguous_range, + // net_repr_proto_result_t>>, + // net_repr_proto_result_t, std::ranges::sentinel_t>>> { - 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); + if constexpr (std::ranges::contiguous_range) { + // return fromNetwork>>(std::forward(bytes)); + return fromNetwork(std::forward(bytes)); + } else { + return fromNetwork, std::ranges::sentinel_t>>( + std::forward(bytes)); + } } }; - - static_assert(net_repr_proto_c>, "!!!"); - } // namespace snplib \ No newline at end of file diff --git a/include/snipplib/network/snplib_network.h b/include/snipplib/network/snplib_network.h index 311a70e..2336ece 100644 --- a/include/snipplib/network/snplib_network.h +++ b/include/snipplib/network/snplib_network.h @@ -56,13 +56,13 @@ concept net_repr_proto_result_c = requires(T t) { T::result_flag_t::INVALID_DATA}; }(); - std::same_as; + requires std::same_as; // char_range_or_range_of_char_ranges_c; requires( - requires { char_range_or_range_of_char_ranges_c; } || - requires { char_range_or_tuple_of_char_ranges_c; }); + requires { requires char_range_or_range_of_char_ranges_c; } || + requires { requires char_range_or_tuple_of_char_ranges_c; }); }; // template