Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
message_channel.hpp
Go to the documentation of this file.
1#pragma once
2
3// MessageChannel — structured-message layer on top of byte streams.
4//
5// `Stream` and `AsyncStream` deal in raw bytes. MessageChannel deals in
6// whole messages: one `send()` transmits one message, one `on_message`
7// callback fires per received message. This is the right abstraction for
8// WebSocket, OSC, JSON-RPC, and similar request/response protocols where
9// a partial read is always meaningless.
10//
11// Layering:
12// byte transport → Stream / AsyncStream
13// framing → MessageChannel implementations
14// protocol → JsonRpcClient, remote-control, user code
15//
16// All MessageChannel methods are thread-safe unless documented otherwise.
17// Callback dispatch follows the same executor contract as AsyncStream: if
18// a `MessageExecutor` is supplied at construction, callbacks land on that
19// executor; otherwise they run on whichever thread the implementation uses
20// internally (usually its reader thread).
21
22#include <cstddef>
23#include <cstdint>
24#include <functional>
25#include <string>
26#include <string_view>
27#include <vector>
28
29namespace pulp::runtime {
30
33enum class MessageKind : uint8_t {
36};
37
39struct Message {
41 std::vector<std::uint8_t> payload;
42
45 std::string_view as_text() const {
46 return {reinterpret_cast<const char*>(payload.data()), payload.size()};
47 }
48};
49
51using MessageCallback = std::function<void(const Message& message)>;
52
55using ChannelClosedCallback = std::function<void()>;
56
60using ChannelErrorCallback = std::function<void(std::string_view reason)>;
61
64using MessageExecutor = std::function<void(std::function<void()>)>;
65
71public:
72 virtual ~MessageChannel() = default;
73
76
83 virtual bool send(const std::uint8_t* data, std::size_t size) = 0;
84
85 bool send(std::string_view text) {
86 return send_text(text);
87 }
88
89 virtual bool send_text(std::string_view text) {
90 // Default: transports that don't distinguish just send bytes.
91 return send(reinterpret_cast<const std::uint8_t*>(text.data()), text.size());
92 }
93
96 virtual void on_message(MessageCallback callback) = 0;
97 virtual void on_closed(ChannelClosedCallback callback) = 0;
98 virtual void on_error(ChannelErrorCallback callback) = 0;
99
101 virtual void close() = 0;
102
104 virtual bool is_open() const = 0;
105
106protected:
107 MessageChannel() = default;
108};
109
110} // namespace pulp::runtime
virtual void on_message(MessageCallback callback)=0
Install / replace callbacks.
MessageChannel & operator=(const MessageChannel &)=delete
virtual bool send(const std::uint8_t *data, std::size_t size)=0
Send a message.
MessageChannel(const MessageChannel &)=delete
virtual void close()=0
Close the channel. Idempotent. Fires the closed callback.
virtual void on_error(ChannelErrorCallback callback)=0
bool send(std::string_view text)
Definition message_channel.hpp:85
virtual bool send_text(std::string_view text)
Definition message_channel.hpp:89
virtual void on_closed(ChannelClosedCallback callback)=0
virtual bool is_open() const =0
Whether the channel is currently usable.
virtual ~MessageChannel()=default
Definition generated_event_source.hpp:33
std::function< void()> ChannelClosedCallback
Called once when the channel transitions to a closed state — either the peer closed,...
Definition message_channel.hpp:55
std::function< void(const Message &message)> MessageCallback
Called when the channel receives a complete message.
Definition message_channel.hpp:51
MessageKind
Classification of a delivered message.
Definition message_channel.hpp:33
@ Binary
opaque bytes
Definition message_channel.hpp:34
@ Text
UTF-8 text (WebSocket text frames, JSON-RPC envelopes).
Definition message_channel.hpp:35
std::function< void(std::string_view reason)> ChannelErrorCallback
Called when a transport-level error occurs.
Definition message_channel.hpp:60
std::function< void(std::function< void()>)> MessageExecutor
Executor used to dispatch callbacks off whichever worker thread the channel runs internally.
Definition message_channel.hpp:64
A single structured message as delivered by the channel.
Definition message_channel.hpp:39
std::vector< std::uint8_t > payload
Definition message_channel.hpp:41
std::string_view as_text() const
View the payload as a text string.
Definition message_channel.hpp:45
MessageKind kind
Definition message_channel.hpp:40