ORE Studio 0.0.4
Loading...
Searching...
No Matches
handshake_protocol.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_HANDSHAKE_PROTOCOL_HPP
21#define ORES_COMMS_MESSAGING_HANDSHAKE_PROTOCOL_HPP
22
23#include <string>
24#include <vector>
25#include <cstdint>
26#include <expected>
27#include "ores.comms/messaging/frame.hpp"
28#include "ores.comms/messaging/error_protocol.hpp"
29#include "ores.comms/messaging/message_traits.hpp"
30
31namespace ores::comms::messaging {
32
39constexpr std::uint8_t COMPRESSION_SUPPORT_ZLIB = 0x01;
40constexpr std::uint8_t COMPRESSION_SUPPORT_GZIP = 0x02;
41constexpr std::uint8_t COMPRESSION_SUPPORT_BZIP2 = 0x04;
42constexpr std::uint8_t COMPRESSION_SUPPORT_ALL = 0x07;
43
47struct handshake_request final {
48 std::uint16_t client_version_major;
49 std::uint16_t client_version_minor;
50 std::string client_identifier;
61 std::uint8_t supported_compression = 0;
62
66 static std::vector<std::byte> serialize(handshake_request v);
67
71 static std::expected<handshake_request, ores::utility::serialization::error_code>
72 deserialize(std::span<const std::byte> data);
73};
74
78struct handshake_response final {
79 std::uint16_t server_version_major;
80 std::uint16_t server_version_minor;
81 bool version_compatible;
82 std::string server_identifier;
83 ores::utility::serialization::error_code status;
91 compression_type selected_compression = compression_type::none;
92
96 static std::vector<std::byte> serialize(handshake_response v);
97
101 static std::expected<handshake_response, ores::utility::serialization::error_code>
102 deserialize(std::span<const std::byte> data);
103};
104
108struct handshake_ack final {
109 ores::utility::serialization::error_code status;
110
114 static std::vector<std::byte> serialize(handshake_ack v);
115
119 static std::expected<handshake_ack, ores::utility::serialization::error_code>
120 deserialize(std::span<const std::byte> data);
121};
122
132 std::uint32_t sequence,
133 const std::string& client_identifier,
134 std::uint8_t supported_compression = 0);
135
147 std::uint32_t sequence,
148 bool version_compatible,
149 const std::string& server_identifier,
150 ores::utility::serialization::error_code status = ores::utility::serialization::error_code::none,
151 compression_type selected_compression = compression_type::none);
152
157 std::uint32_t sequence,
158 ores::utility::serialization::error_code status = ores::utility::serialization::error_code::none);
159
169 std::uint32_t sequence,
170 std::uint32_t correlation_id,
171 ores::utility::serialization::error_code code,
172 const std::string& message);
173
185 std::uint8_t supported_compression,
186 compression_type preferred = compression_type::zlib);
187
191template<>
195 static constexpr message_type request_message_type =
196 message_type::handshake_request;
197};
198
199}
200
201#endif
Contains messaging related infrastructure in the comms library.
Definition assets_protocol.hpp:122
frame create_handshake_response_frame(std::uint32_t sequence, bool version_compatible, const std::string &server_identifier, ores::utility::serialization::error_code status=ores::utility::serialization::error_code::none, compression_type selected_compression=compression_type::none)
Create a handshake response frame.
frame create_error_response_frame(std::uint32_t sequence, std::uint32_t correlation_id, ores::utility::serialization::error_code code, const std::string &message)
Create an error response frame.
constexpr std::uint8_t COMPRESSION_SUPPORT_ZLIB
Compression support bitmask values.
Definition handshake_protocol.hpp:39
frame create_handshake_request_frame(std::uint32_t sequence, const std::string &client_identifier, std::uint8_t supported_compression=0)
Create a handshake request frame.
Definition handshake.cpp:124
frame create_handshake_ack_frame(std::uint32_t sequence, ores::utility::serialization::error_code status=ores::utility::serialization::error_code::none)
Create a handshake acknowledgment frame.
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:259
compression_type select_compression(std::uint8_t supported_compression, compression_type preferred=compression_type::zlib)
Select a compression type from the client's supported types.
Definition handshake.cpp:193
Complete frame with header and payload.
Definition frame.hpp:77
Handshake request message sent by client to initiate connection.
Definition handshake_protocol.hpp:47
static std::expected< handshake_request, ores::utility::serialization::error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:53
static std::vector< std::byte > serialize(handshake_request v)
Serialize to frame payload.
Definition handshake.cpp:44
std::uint8_t supported_compression
Bitmask of compression types supported by the client.
Definition handshake_protocol.hpp:61
Handshake response message sent by server to client.
Definition handshake_protocol.hpp:78
static std::vector< std::byte > serialize(handshake_response v)
Serialize to frame payload.
Definition handshake.cpp:64
static std::expected< handshake_response, ores::utility::serialization::error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:73
compression_type selected_compression
Compression type selected for this session.
Definition handshake_protocol.hpp:91
Handshake acknowledgment message sent by client to complete handshake.
Definition handshake_protocol.hpp:108
static std::expected< handshake_ack, ores::utility::serialization::error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:93
static std::vector< std::byte > serialize(handshake_ack v)
Serialize to frame payload.
Definition handshake.cpp:84
Traits template for mapping request types to their response types and message type enum values.
Definition message_traits.hpp:66