WIP: Message definitions.

This commit is contained in:
Kurt Sassenrath 2023-10-08 08:20:23 -07:00
parent a80b9d0fc6
commit 21b388f0d0
3 changed files with 100 additions and 10 deletions

View File

@ -273,6 +273,10 @@ public:
return token_buffer.subspan(0, parsed);
}
template <typename T>
constexpr tl::expected<T, error> read() {
}
private:
std::span<std::byte const> data_;

View File

@ -7,9 +7,9 @@
//
//-----------------------------------------------------------------------------
// Author: Kurt Sassenrath
// Module: Message
// Module: Proto
//
// Message types.
// Message type definitions for the parselink protocol.
//
// Copyright (c) 2023 Kurt Sassenrath.
//
@ -19,18 +19,14 @@
#define message_0c61530748b9f966
#include <span>
#include <string_view>
#include <cstdint>
#include <tl/expected.hpp>
#include <variant>
namespace parselink {
namespace message {
enum class error {
bad_magic, // Did not get the message magic expected
too_large, // The message size was too large.
unknown_type, // The message type is not known
};
namespace proto {
// Parselink messages are encoded with MessagePack and take the form of:
// | magic | size | content |
@ -38,6 +34,55 @@ enum class error {
// - [size] is the size of the message, not including this header.
// - [content] is [size] bytes of MessagePack data, including the
// specific type of message presented.
// This may be revised in the future. The header could remain as msgpack, or
// switch to something hand-crafted for saving bits.
struct error_message {
std::uint32_t code; // An error code
std::string_view what; // A string
};
// C->S: Request to (re)connect.
struct connect_message {
std::uint32_t user_id; // The user id.
std::uint32_t version; // The version of the client.
std::span<std::byte> session_token; // An optional existing session token.
};
// S->C: Challenge to authenticate client as user_id
struct challenge_message {
std::uint32_t version;
std::span<std::byte> challenge;
};
// C->S: Calculated response to a challenge.
struct response_message {
std::span<std::byte> response;
};
// S->C: Session token.
struct session_established_message {
std::span<std::byte> session_token;
};
struct parser_data_message {
std::string_view opts;
std::span<std::byte> payload;
};
using message = std::variant<
error_message,
connect_message,
challenge_message,
response_message,
session_established_message,
parser_data_message>;
enum class error {
bad_magic, // Did not get the message magic expected
too_large, // The message size was too large.
unknown_type, // The message type is not known
};
// This class is responsible for consuming buffer data and yielding a message
// instance when complete. Will throw an error if data is incorrect.

View File

@ -1,6 +1,9 @@
#include "parselink/msgpack/token.h"
//#include "parselink/proto/message.h"
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <magic_enum.hpp>
#include <chrono>
#include <cstdlib>
@ -9,6 +12,11 @@
using namespace std::chrono_literals;
struct any {
template <typename T>
operator T() {}
};
auto read_file(char const* filename) {
std::vector<std::byte> data;
auto fd = open(filename, O_RDONLY);
@ -30,7 +38,7 @@ auto read_file(char const* filename) {
int main(int argc, char** argv) {
std::array<msgpack::token, 4096> buf;
std::array<msgpack::token, 20> buf;
auto data = read_file(argc < 2 ? "output.bin" : argv[1]);
@ -41,6 +49,39 @@ int main(int argc, char** argv) {
auto after = std::chrono::steady_clock::now();
test.map([&](auto sp){
fmt::print("Read {} tokens\n", sp.size());
for (auto token : sp) {
fmt::print("token type: {} value: ", magic_enum::enum_name(token.type()));
switch (token.type()) {
case msgpack::format::type::unsigned_int:
fmt::print("{}", *(token.template get<std::uint64_t>()));
break;
case msgpack::format::type::signed_int:
fmt::print("{}", *(token.template get<std::int64_t>()));
break;
case msgpack::format::type::boolean:
fmt::print("{}", *(token.template get<bool>()));
break;
case msgpack::format::type::string:
fmt::print("{}", *(token.template get<std::string_view>()));
break;
case msgpack::format::type::binary:
fmt::print("{}", *(token.template get<std::span<std::byte const>>()));
break;
case msgpack::format::type::map:
fmt::print("map ({} entries)", token.template get<msgpack::map_desc>()->count);
break;
case msgpack::format::type::array:
fmt::print("array ({} entries)", token.template get<msgpack::array_desc>()->count);
break;
case msgpack::format::type::nil:
fmt::print("(nil)");
break;
case msgpack::format::type::invalid:
fmt::print("(invalid)");
break;
}
fmt::print("\n");
}
}).map_error([&](auto err){
fmt::print("Failed to read tokens: {}\n", (int)err);
});