ORE Studio 0.0.4
Loading...
Searching...
No Matches
message.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 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_NATS_DOMAIN_MESSAGE_HPP
21#define ORES_NATS_DOMAIN_MESSAGE_HPP
22
23#include <functional>
24#include <span>
25#include <string>
26#include <string_view>
27#include <unordered_map>
28#include <vector>
29#include <cstddef>
30
31namespace ores::nats {
32
40struct message {
44 std::string subject;
45
51 std::string reply_subject;
52
58 std::vector<std::byte> data;
59
66 std::unordered_map<std::string, std::string> headers;
67};
68
72using message_handler = std::function<void(message)>;
73
81inline std::span<const std::byte> as_bytes(std::string_view s) noexcept {
82 return {reinterpret_cast<const std::byte*>(s.data()), s.size()};
83}
84
91inline std::string_view as_string_view(const std::vector<std::byte>& data) noexcept {
92 return {reinterpret_cast<const char*>(data.data()), data.size()};
93}
94
95}
96
97#endif
NATS transport layer — external message bus, cross-process connectivity.
Definition service_token_provider.hpp:27
std::string_view as_string_view(const std::vector< std::byte > &data) noexcept
Reinterprets a message's byte payload as a string_view.
Definition message.hpp:91
std::span< const std::byte > as_bytes(std::string_view s) noexcept
Reinterprets a string's character data as a read-only byte span.
Definition message.hpp:81
std::function< void(message)> message_handler
Callback type for incoming NATS messages.
Definition message.hpp:72
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
std::unordered_map< std::string, std::string > headers
NATS message headers (NATS 2.2+).
Definition message.hpp:66
std::string reply_subject
The reply-to subject, empty for one-way publishes.
Definition message.hpp:51
std::vector< std::byte > data
The message payload bytes.
Definition message.hpp:58