Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
memory_message_channel.hpp
Go to the documentation of this file.
1#pragma once
2
3// In-memory MessageChannel pair. Primarily for tests and for local
4// message-driven code that wants to talk to itself (plugins ↔ inspector
5// in the same process, for example) without a socket hop.
6//
7// Usage:
8// auto [a, b] = MemoryMessageChannel::make_pair();
9// a->send(...); // delivered to b->on_message
10// b->send(...); // delivered to a->on_message
11
13
14#include <atomic>
15#include <memory>
16#include <mutex>
17#include <utility>
18
19namespace pulp::runtime {
20
21class MemoryMessageChannel : public MessageChannel {
22public:
25 static std::pair<std::unique_ptr<MemoryMessageChannel>,
26 std::unique_ptr<MemoryMessageChannel>>
28
30
31 // MessageChannel
32 bool send(const std::uint8_t* data, std::size_t size) override;
33 bool send_text(std::string_view text) override;
34 void on_message(MessageCallback callback) override;
35 void on_closed(ChannelClosedCallback callback) override;
36 void on_error(ChannelErrorCallback callback) override;
37 void close() override;
38 bool is_open() const override { return open_.load() && peer_alive(); }
39
40private:
41 MemoryMessageChannel() = default;
42
43 bool peer_alive() const;
44 void deliver(Message msg);
45
46 std::weak_ptr<MemoryMessageChannel*> peer_; // weak-pointer to a raw* so
47 // destruction breaks the link
48 std::shared_ptr<MemoryMessageChannel*> self_;
49 std::atomic<bool> open_{true};
50
51 std::mutex mutex_;
52 MessageCallback on_message_;
53 ChannelClosedCallback on_closed_;
54 ChannelErrorCallback on_error_;
55};
56
57} // namespace pulp::runtime
Definition memory_message_channel.hpp:21
bool send(const std::uint8_t *data, std::size_t size) override
Send a message.
static std::pair< std::unique_ptr< MemoryMessageChannel >, std::unique_ptr< MemoryMessageChannel > > make_pair()
Create a connected pair.
bool is_open() const override
Whether the channel is currently usable.
Definition memory_message_channel.hpp:38
void on_error(ChannelErrorCallback callback) override
void on_closed(ChannelClosedCallback callback) override
void close() override
Close the channel. Idempotent. Fires the closed callback.
bool send_text(std::string_view text) override
void on_message(MessageCallback callback) override
Install / replace callbacks.
MessageChannel(const MessageChannel &)=delete
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
std::function< void(std::string_view reason)> ChannelErrorCallback
Called when a transport-level error occurs.
Definition message_channel.hpp:60
A single structured message as delivered by the channel.
Definition message_channel.hpp:39