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
30namespace ores::comms::messaging {
31
38constexpr std::uint8_t COMPRESSION_SUPPORT_ZLIB = 0x01;
39constexpr std::uint8_t COMPRESSION_SUPPORT_GZIP = 0x02;
40constexpr std::uint8_t COMPRESSION_SUPPORT_BZIP2 = 0x04;
41constexpr std::uint8_t COMPRESSION_SUPPORT_ALL = 0x07;
42
46struct handshake_request final {
47 std::uint16_t client_version_major;
48 std::uint16_t client_version_minor;
49 std::string client_identifier;
60 std::uint8_t supported_compression = 0;
61
65 static std::vector<std::byte> serialize(handshake_request v);
66
70 static std::expected<handshake_request, error_code>
71 deserialize(std::span<const std::byte> data);
72};
73
77struct handshake_response final {
78 std::uint16_t server_version_major;
79 std::uint16_t server_version_minor;
80 bool version_compatible;
81 std::string server_identifier;
82 error_code status;
90 compression_type selected_compression = compression_type::none;
91
95 static std::vector<std::byte> serialize(handshake_response v);
96
100 static std::expected<handshake_response, error_code>
101 deserialize(std::span<const std::byte> data);
102};
103
107struct handshake_ack final {
108 error_code status;
109
113 static std::vector<std::byte> serialize(handshake_ack v);
114
118 static std::expected<handshake_ack, error_code>
119 deserialize(std::span<const std::byte> data);
120};
121
131 std::uint32_t sequence,
132 const std::string& client_identifier,
133 std::uint8_t supported_compression = 0);
134
146 std::uint32_t sequence,
147 bool version_compatible,
148 const std::string& server_identifier,
149 error_code status = error_code::none,
150 compression_type selected_compression = compression_type::none);
151
156 std::uint32_t sequence,
157 error_code status = error_code::none);
158
168 std::uint32_t sequence,
169 std::uint32_t correlation_id,
170 error_code code,
171 const std::string& message);
172
184 std::uint8_t supported_compression,
185 compression_type preferred = compression_type::zlib);
186
187}
188
189#endif
Contains messaging related infrastructure in the comms library.
Definition compression.hpp:29
frame create_error_response_frame(std::uint32_t sequence, std::uint32_t correlation_id, error_code code, const std::string &message)
Create an error response frame.
Definition handshake.cpp:178
frame create_handshake_response_frame(std::uint32_t sequence, bool version_compatible, const std::string &server_identifier, error_code status=error_code::none, compression_type selected_compression=compression_type::none)
Create a handshake response frame.
Definition handshake.cpp:146
constexpr std::uint8_t COMPRESSION_SUPPORT_ZLIB
Compression support bitmask values.
Definition handshake_protocol.hpp:38
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:123
frame create_handshake_ack_frame(std::uint32_t sequence, error_code status=error_code::none)
Create a handshake acknowledgment frame.
Definition handshake.cpp:169
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:104
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:192
Complete frame with header and payload.
Definition frame.hpp:77
Handshake request message sent by client to initiate connection.
Definition handshake_protocol.hpp:46
static std::vector< std::byte > serialize(handshake_request v)
Serialize to frame payload.
Definition handshake.cpp:43
static std::expected< handshake_request, error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:52
std::uint8_t supported_compression
Bitmask of compression types supported by the client.
Definition handshake_protocol.hpp:60
Handshake response message sent by server to client.
Definition handshake_protocol.hpp:77
static std::vector< std::byte > serialize(handshake_response v)
Serialize to frame payload.
Definition handshake.cpp:63
compression_type selected_compression
Compression type selected for this session.
Definition handshake_protocol.hpp:90
static std::expected< handshake_response, error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:72
Handshake acknowledgment message sent by client to complete handshake.
Definition handshake_protocol.hpp:107
static std::expected< handshake_ack, error_code > deserialize(std::span< const std::byte > data)
Deserialize from frame payload.
Definition handshake.cpp:92
static std::vector< std::byte > serialize(handshake_ack v)
Serialize to frame payload.
Definition handshake.cpp:83