From 915773f3a8731159f8238e8f4bf303916290b819 Mon Sep 17 00:00:00 2001 From: Kurt Sassenrath Date: Tue, 10 Oct 2023 15:53:14 -0700 Subject: [PATCH] WIP slight changes to token_reader --- include/parselink/BUILD | 8 ++++ include/parselink/msgpack/token/reader.h | 54 ++++++++++++------------ source/BUILD | 3 +- source/server.cpp | 4 +- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/include/parselink/BUILD b/include/parselink/BUILD index ebcf67b..b93df59 100644 --- a/include/parselink/BUILD +++ b/include/parselink/BUILD @@ -15,6 +15,14 @@ cc_library( visibility = ["//visibility:public"], ) +cc_library( + name = "proto", + hdrs = glob(["proto/**/*.h"]), + includes = ["."], + deps = ["//include:path"], + visibility = ["//visibility:public"], +) + cc_library( name = "utility", hdrs = glob(["utility/**/*.h"]), diff --git a/include/parselink/msgpack/token/reader.h b/include/parselink/msgpack/token/reader.h index 7edd8d0..808878c 100644 --- a/include/parselink/msgpack/token/reader.h +++ b/include/parselink/msgpack/token/reader.h @@ -60,6 +60,21 @@ constexpr inline decltype(auto) make_token(auto bytes) { // Define in token/type.h instead? Avoid instantiations in the core API. constexpr inline format::traits const& traits_lookup(std::byte id) { + // Check for fix formats first. + if ((id & ~format::negative_fixint::mask) + == format::negative_fixint::marker) { + return format::get_traits(); + } else if ((id & ~format::positive_fixint::mask) + == format::positive_fixint::marker) { + return format::get_traits(); + } else if ((id & ~format::fixstr::mask) == format::fixstr::marker) { + return format::get_traits(); + } else if ((id & ~format::fixmap::mask) == format::fixmap::marker) { + return format::get_traits(); + } else if ((id & ~format::fixarray::mask) == format::fixarray::marker) { + return format::get_traits(); + } + switch (id) { case format::uint8::marker: return format::get_traits(); @@ -113,23 +128,6 @@ constexpr inline format::traits const& traits_lookup(std::byte id) { break; } - // TODO(ksassenrath): Handle fixtype formats. - if ((id & ~format::negative_fixint::mask) == format::negative_fixint::marker) { - return format::get_traits(); - } - if ((id & ~format::positive_fixint::mask) == format::positive_fixint::marker) { - return format::get_traits(); - } - if ((id & ~format::fixstr::mask) == format::fixstr::marker) { - return format::get_traits(); - } - if ((id & ~format::fixmap::mask) == format::fixmap::marker) { - return format::get_traits(); - } - if ((id & ~format::fixarray::mask) == format::fixarray::marker) { - return format::get_traits(); - } - return format::no_traits; } @@ -139,15 +137,19 @@ class token_reader { public: constexpr token_reader(std::span src) noexcept : - data_(src), curr_{src.begin()}, end_(src.end()) {} + data_(src), curr_{} {} + + constexpr auto current() const noexcept { + return std::next(data_.begin(), curr_); + } constexpr auto remaining(auto itr) noexcept { - using dist_type = decltype(std::distance(itr, end_)); - return std::max(dist_type(0), std::distance(itr, end_)); + using dist_type = decltype(std::distance(itr, data_.end())); + return std::max(dist_type(0), std::distance(itr, data_.end())); } constexpr auto remaining() noexcept { - return remaining(curr_); + return remaining(current()); } // Read the next token. If the reader currently points to the end of the @@ -156,12 +158,12 @@ public: // potentially hinting that further buffering is required. constexpr tl::expected read_one() noexcept { token tok; - if (curr_ >= end_) { + if (remaining() == 0) { return tl::make_unexpected(error::end_of_message); } // curr_ will be updated after everything succeeds. - auto curr = curr_; + auto curr = current(); std::size_t var_size = 0; auto id = *curr++; auto const& traits = detail::traits_lookup(id); @@ -240,7 +242,7 @@ public: return tl::make_unexpected(error::not_implemented); } - curr_ = curr; + curr_ = std::distance(data_.begin(), curr); return {tok}; } @@ -278,10 +280,8 @@ public: } private: - std::span data_; - decltype(data_)::iterator curr_; - decltype(data_)::iterator end_; + decltype(data_)::iterator::difference_type curr_; }; } // namespace msgpack diff --git a/source/BUILD b/source/BUILD index 8193588..3540a31 100644 --- a/source/BUILD +++ b/source/BUILD @@ -15,8 +15,9 @@ cc_binary( deps = [ "headers", "//include/parselink:msgpack", - "//source/logging", + "//include/parselink:proto", "//include/parselink:utility", + "//source/logging", "@boost//:beast", ], ) diff --git a/source/server.cpp b/source/server.cpp index b44a2ae..dfd1509 100644 --- a/source/server.cpp +++ b/source/server.cpp @@ -21,6 +21,8 @@ #include "parselink/logging.h" #include "parselink/server.h" +#include "parselink/proto/message.h" + #include #include #include @@ -151,7 +153,7 @@ monolithic_server::monolithic_server(std::string_view address, , user_port_{user_port} , websocket_port_{websocket_port} { logger.debug("Creating monolithic_server(address = {}, user_port = {}, " - "websocket_port = {})", address, user_port_, websocket_port_); + "websocket_port = {}, sizeof(message) = {})", address, user_port_, websocket_port_, sizeof(proto::message)); } awaitable monolithic_server::user_listen() {