Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
IpcChannel.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstddef>
10#include <string>
11#include <utility>
12
13#include "corium/Events.hpp"
14#include "corium/EventSink.hpp"
17
18namespace corium::ipc {
19
25template <
26 typename EventVariant = DefaultEvents,
27 std::size_t Capacity = 256
28>
30 static_assert(std::is_trivially_copyable_v<EventVariant>,
31 "IpcChannel EventVariant must be trivially copyable for zero-copy shared memory IPC.");
32
33public:
35
36 IpcChannel() = default;
37
41 bool create(const std::string& channelName) noexcept
42 {
44 return false;
45 }
46 _queue.bind(_shm.data(), true);
47 return _queue.isValid();
48 }
49
53 bool attach(const std::string& channelName) noexcept
54 {
56 return false;
57 }
58 _queue.bind(_shm.data(), false);
59 return _queue.isValid();
60 }
61
66 bool bindRaw(void* buffer, bool isCreator = true) noexcept
67 {
68 _queue.bind(buffer, isCreator);
69 return _queue.isValid();
70 }
71
77 template <typename EventType>
78 bool post(EventType&& event) noexcept
79 {
80 return _queue.tryPush(EventVariant{std::forward<EventType>(event)});
81 }
82
87 bool tryPop(EventVariant& outEvent) noexcept
88 {
89 return _queue.tryPop(outEvent);
90 }
91
97 template <typename SinkType>
98 std::size_t pumpInto(const SinkType& sink, std::size_t maxEvents = 0)
99 {
100 std::size_t count = 0;
101 EventVariant ev;
102 while (_queue.tryPop(ev)) {
103 sink.post(std::move(ev));
104 count++;
105 if (maxEvents > 0 && count >= maxEvents) {
106 break;
107 }
108 }
109 return count;
110 }
111
113 void unlink() noexcept
114 {
115 if (_shm.isValid()) {
116 std::string name = _shm.name();
117 _shm.close();
119 }
120 }
121
123 [[nodiscard]] bool isValid() const noexcept
124 {
125 return _shm.isValid() && _queue.isValid();
126 }
127
129 [[nodiscard]] bool empty() const noexcept
130 {
131 return _queue.empty();
132 }
133
135 [[nodiscard]] constexpr std::size_t capacity() const noexcept
136 {
137 return Capacity;
138 }
139
140private:
141 SharedMemory _shm;
142 QueueType _queue;
143};
144
145} // namespace corium::ipc
Non-allocating type-erased fat pointer handle for lock-free event posting.
Standard lifecycle events (QuitEvent, ErrorEvent, TimerEvent).
RAII wrapper for POSIX shared memory and Windows file mappings.
Lock-free multi-producer single-consumer queue located in shared memory.
High-level typed inter-process communication channel for Corium events. Encapsulates OS shared memory...
Definition IpcChannel.hpp:29
constexpr std::size_t capacity() const noexcept
Configured capacity of the channel.
Definition IpcChannel.hpp:135
bool attach(const std::string &channelName) noexcept
Attach to an existing shared memory channel as a client process.
Definition IpcChannel.hpp:53
bool empty() const noexcept
Check if channel is empty.
Definition IpcChannel.hpp:129
bool tryPop(EventVariant &outEvent) noexcept
Pop one event from the shared queue. Single-consumer safe.
Definition IpcChannel.hpp:87
bool post(EventType &&event) noexcept
Post an event into the shared memory queue for remote processes. Lock-free, zero-allocation,...
Definition IpcChannel.hpp:78
bool bindRaw(void *buffer, bool isCreator=true) noexcept
Bind channel directly to a raw memory buffer (e.g. multi-core embedded SRAM).
Definition IpcChannel.hpp:66
std::size_t pumpInto(const SinkType &sink, std::size_t maxEvents=0)
Drain incoming shared memory events into a target event sink.
Definition IpcChannel.hpp:98
ShmMpscQueue< EventVariant, Capacity > QueueType
Definition IpcChannel.hpp:34
bool isValid() const noexcept
Check if channel is open and valid.
Definition IpcChannel.hpp:123
bool create(const std::string &channelName) noexcept
Create a new shared memory channel as the host/creator process.
Definition IpcChannel.hpp:41
void unlink() noexcept
Destroy the channel and remove shared memory from OS.
Definition IpcChannel.hpp:113
Cross-platform zero-copy shared memory region wrapper. Manages OS-level shared memory allocation,...
Definition SharedMemory.hpp:60
const std::string & name() const noexcept
Name identifier of shared memory.
Definition SharedMemory.hpp:256
void close() noexcept
Close and unmap the shared memory segment.
Definition SharedMemory.hpp:209
void * data() noexcept
Raw pointer to mapped shared memory buffer.
Definition SharedMemory.hpp:243
bool isValid() const noexcept
Check if mapped region is valid.
Definition SharedMemory.hpp:250
static void unlink(const std::string &name) noexcept
Remove shared memory identifier from OS namespace (POSIX shm_unlink).
Definition SharedMemory.hpp:232
bool open(const std::string &name, std::size_t size, AccessMode mode=AccessMode::CreateOrOpen) noexcept
Create or attach to a shared memory region.
Definition SharedMemory.hpp:126
Lock-free, zero-allocation multi-producer single-consumer ring buffer layout for shared memory....
Definition ShmMpscQueue.hpp:28
void bind(void *mappedAddress, bool initializeMemory=false) noexcept
Bind to a mapped shared memory region.
Definition ShmMpscQueue.hpp:75
bool empty() const noexcept
Check if queue is currently empty.
Definition ShmMpscQueue.hpp:183
bool tryPush(U &&item) noexcept
Lock-free push into shared memory queue (multi-producer safe).
Definition ShmMpscQueue.hpp:114
static constexpr std::size_t requiredMemorySize() noexcept
Required byte size of the shared memory layout.
Definition ShmMpscQueue.hpp:93
bool tryPop(T &outItem) noexcept
Lock-free pop from shared memory queue (single-consumer safe).
Definition ShmMpscQueue.hpp:154
bool isValid() const noexcept
Validate that the mapped shared memory contains a compatible ShmMpscQueue header.
Definition ShmMpscQueue.hpp:99
Definition DomainSocket.hpp:35
std::variant< QuitEvent, TickEvent, UpdateEvent, ErrorEvent, SignalEvent > DefaultEvents
Default variant list of core Corium events.
Definition Events.hpp:62