ORE Studio 0.0.4
Loading...
Searching...
No Matches
frame.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_MESSAGING_FRAME_HPP
21#define ORES_COMMS_MESSAGING_FRAME_HPP
22
23#include <span>
24#include <array>
25#include <vector>
26#include <iosfwd>
27#include <cstdint>
28#include <expected>
29#include "ores.utility/log/make_logger.hpp"
30#include "ores.comms/messaging/message_types.hpp"
31
32namespace ores::comms::messaging {
33
34constexpr size_t MAX_PAYLOAD_SIZE = 1'000'000;
35
58struct frame_header final {
59 std::uint32_t magic;
60 std::uint16_t version_major;
61 std::uint16_t version_minor;
62 message_type type;
63 compression_type compression;
64 std::uint8_t compression_flags;
65 std::uint32_t payload_size;
66 std::uint32_t sequence;
67 std::uint32_t crc;
68 std::uint32_t correlation_id;
69 std::array<std::uint8_t, 4> reserved2;
70
71 static constexpr size_t size = 32;
72};
73
77class frame final {
78private:
79 inline static std::string_view logger_name = "ores.comms.messaging.frame";
80
81 static auto& lg() {
82 using namespace ores::utility::log;
83 static auto instance = make_logger(logger_name);
84 return instance;
85 }
86
87public:
88 frame();
89 frame(message_type type, std::uint32_t sequence, std::vector<std::byte> payload,
90 compression_type compression = compression_type::none);
91 frame(message_type type, std::uint32_t sequence, std::uint32_t correlation_id,
92 std::vector<std::byte> payload,
93 compression_type compression = compression_type::none);
94
98 std::uint32_t correlation_id() const { return header_.correlation_id; }
99
103 const frame_header& header() const { return header_; }
104
108 const std::vector<std::byte>& payload() const { return payload_; }
109
113 compression_type compression() const { return header_.compression; }
114
121 std::expected<std::vector<std::byte>, error_code> decompressed_payload() const;
122
129 std::vector<std::byte> serialize() const;
130
143 static std::expected<frame_header, error_code> deserialize_header(
144 std::span<const std::byte> data, bool skip_version_check = false);
145
153 static std::expected<frame, error_code>
154 deserialize(const frame_header& header, std::span<const std::byte> data);
155
161 std::expected<void, error_code> validate() const;
162
163private:
164 frame_header header_;
165 std::vector<std::byte> payload_;
166
172 std::uint32_t calculate_crc() const;
173
177 void serialize_header(frame_header header, std::span<std::byte> buffer) const;
178
186 void init_payload(std::vector<std::byte> payload, compression_type compression);
187};
188
189std::ostream& operator<<(std::ostream& s, const frame_header& v);
190
191}
192
193#endif
Contains messaging related infrastructure in the comms library.
Definition compression.hpp:29
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:91
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Frame header structure for the ORES protocol.
Definition frame.hpp:58
Complete frame with header and payload.
Definition frame.hpp:77
const frame_header & header() const
Get the frame header.
Definition frame.hpp:103
static std::expected< frame_header, error_code > deserialize_header(std::span< const std::byte > data, bool skip_version_check=false)
Deserialize and validate header from bytes.
Definition frame.cpp:204
std::vector< std::byte > serialize() const
Serialize frame to bytes.
Definition frame.cpp:187
std::uint32_t correlation_id() const
Get the correlation ID for request/response matching.
Definition frame.hpp:98
compression_type compression() const
Get the compression type used for the payload.
Definition frame.hpp:113
std::expected< std::vector< std::byte >, error_code > decompressed_payload() const
Decompress and return the payload.
Definition frame.cpp:360
const std::vector< std::byte > & payload() const
Get the raw payload (compressed if compression is enabled).
Definition frame.hpp:108
std::expected< void, error_code > validate() const
Validate frame integrity.
Definition frame.cpp:321
static std::expected< frame, error_code > deserialize(const frame_header &header, std::span< const std::byte > data)
Deserialize complete frame using a pre-parsed header.
Definition frame.cpp:289