WIP slight changes to token_reader

This commit is contained in:
Kurt Sassenrath 2023-10-10 15:53:14 -07:00
parent 21b388f0d0
commit 915773f3a8
4 changed files with 40 additions and 29 deletions

View File

@ -15,6 +15,14 @@ cc_library(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
cc_library(
name = "proto",
hdrs = glob(["proto/**/*.h"]),
includes = ["."],
deps = ["//include:path"],
visibility = ["//visibility:public"],
)
cc_library( cc_library(
name = "utility", name = "utility",
hdrs = glob(["utility/**/*.h"]), hdrs = glob(["utility/**/*.h"]),

View File

@ -60,6 +60,21 @@ constexpr inline decltype(auto) make_token(auto bytes) {
// Define in token/type.h instead? Avoid instantiations in the core API. // Define in token/type.h instead? Avoid instantiations in the core API.
constexpr inline format::traits const& traits_lookup(std::byte id) { 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<format::negative_fixint>();
} else if ((id & ~format::positive_fixint::mask)
== format::positive_fixint::marker) {
return format::get_traits<format::positive_fixint>();
} else if ((id & ~format::fixstr::mask) == format::fixstr::marker) {
return format::get_traits<format::fixstr>();
} else if ((id & ~format::fixmap::mask) == format::fixmap::marker) {
return format::get_traits<format::fixmap>();
} else if ((id & ~format::fixarray::mask) == format::fixarray::marker) {
return format::get_traits<format::fixarray>();
}
switch (id) { switch (id) {
case format::uint8::marker: case format::uint8::marker:
return format::get_traits<format::uint8>(); return format::get_traits<format::uint8>();
@ -113,23 +128,6 @@ constexpr inline format::traits const& traits_lookup(std::byte id) {
break; break;
} }
// TODO(ksassenrath): Handle fixtype formats.
if ((id & ~format::negative_fixint::mask) == format::negative_fixint::marker) {
return format::get_traits<format::negative_fixint>();
}
if ((id & ~format::positive_fixint::mask) == format::positive_fixint::marker) {
return format::get_traits<format::positive_fixint>();
}
if ((id & ~format::fixstr::mask) == format::fixstr::marker) {
return format::get_traits<format::fixstr>();
}
if ((id & ~format::fixmap::mask) == format::fixmap::marker) {
return format::get_traits<format::fixmap>();
}
if ((id & ~format::fixarray::mask) == format::fixarray::marker) {
return format::get_traits<format::fixarray>();
}
return format::no_traits; return format::no_traits;
} }
@ -139,15 +137,19 @@ class token_reader {
public: public:
constexpr token_reader(std::span<std::byte const> src) noexcept : constexpr token_reader(std::span<std::byte const> 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 { constexpr auto remaining(auto itr) noexcept {
using dist_type = decltype(std::distance(itr, end_)); using dist_type = decltype(std::distance(itr, data_.end()));
return std::max(dist_type(0), std::distance(itr, end_)); return std::max(dist_type(0), std::distance(itr, data_.end()));
} }
constexpr auto remaining() noexcept { constexpr auto remaining() noexcept {
return remaining(curr_); return remaining(current());
} }
// Read the next token. If the reader currently points to the end of the // 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. // potentially hinting that further buffering is required.
constexpr tl::expected<token, error> read_one() noexcept { constexpr tl::expected<token, error> read_one() noexcept {
token tok; token tok;
if (curr_ >= end_) { if (remaining() == 0) {
return tl::make_unexpected(error::end_of_message); return tl::make_unexpected(error::end_of_message);
} }
// curr_ will be updated after everything succeeds. // curr_ will be updated after everything succeeds.
auto curr = curr_; auto curr = current();
std::size_t var_size = 0; std::size_t var_size = 0;
auto id = *curr++; auto id = *curr++;
auto const& traits = detail::traits_lookup(id); auto const& traits = detail::traits_lookup(id);
@ -240,7 +242,7 @@ public:
return tl::make_unexpected(error::not_implemented); return tl::make_unexpected(error::not_implemented);
} }
curr_ = curr; curr_ = std::distance(data_.begin(), curr);
return {tok}; return {tok};
} }
@ -278,10 +280,8 @@ public:
} }
private: private:
std::span<std::byte const> data_; std::span<std::byte const> data_;
decltype(data_)::iterator curr_; decltype(data_)::iterator::difference_type curr_;
decltype(data_)::iterator end_;
}; };
} // namespace msgpack } // namespace msgpack

View File

@ -15,8 +15,9 @@ cc_binary(
deps = [ deps = [
"headers", "headers",
"//include/parselink:msgpack", "//include/parselink:msgpack",
"//source/logging", "//include/parselink:proto",
"//include/parselink:utility", "//include/parselink:utility",
"//source/logging",
"@boost//:beast", "@boost//:beast",
], ],
) )

View File

@ -21,6 +21,8 @@
#include "parselink/logging.h" #include "parselink/logging.h"
#include "parselink/server.h" #include "parselink/server.h"
#include "parselink/proto/message.h"
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/signal_set.hpp> #include <boost/asio/signal_set.hpp>
#include <boost/asio/redirect_error.hpp> #include <boost/asio/redirect_error.hpp>
@ -151,7 +153,7 @@ monolithic_server::monolithic_server(std::string_view address,
, user_port_{user_port} , user_port_{user_port}
, websocket_port_{websocket_port} { , websocket_port_{websocket_port} {
logger.debug("Creating monolithic_server(address = {}, user_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<void> monolithic_server::user_listen() { awaitable<void> monolithic_server::user_listen() {