VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
shm_conf.h
浏览该文件的文档.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file shm_conf.h
26 * @brief Transport configuration for the @c shm:// Iceoryx shared-memory backend.
27 *
28 * @details
29 * @c ShmConf configures the Iceoryx-based shared-memory transport, which provides
30 * zero-copy inter-process communication between processes on the same machine.
31 * It requires the Iceoryx RouDi daemon to be running (either externally started or
32 * initialised in-process via @c init_roudi()).
33 *
34 * @par Supported Node Types
35 * @c shm:// supports all six node types: @c kPublisher, @c kSubscriber, @c kServer,
36 * @c kClient, @c kSetter, and @c kGetter.
37 *
38 * @par URL Format
39 * @code
40 * shm://<address>[?event=<name>&domain=<N>&depth=<N>&history=<N>&wait=<0|1>]
41 * @endcode
42 *
43 * | Component | Description |
44 * | ---------- | -------------------------------------------------------------------- |
45 * | @c address | Service/topic name; formed from @c host + @c "/" + @c path |
46 * | @c event | Optional secondary event name (@c ?event=) |
47 * | @c domain | Iceoryx domain ID (@c ?domain=, default 0) |
48 * | @c depth | History buffer depth (@c ?depth=, default 0) |
49 * | @c history | History count (@c ?history=); default 0 for pub/sub, 1 for field |
50 * | @c wait | Blocking-wait mode for pub/sub (@c ?wait=1); not valid for RPC/field |
51 *
52 * @par RouDi Lifecycle
53 * RouDi is the Iceoryx memory manager daemon. It can be started externally
54 * (recommended for production) or embedded in the same process:
55 * @code
56 * // Option A: in-process RouDi (single process only):
57 * vlink::ShmConf::init_roudi();
58 *
59 * // Option B: connect to external RouDi:
60 * vlink::ShmConf::init_runtime("my_app");
61 *
62 * // ... use SHM nodes ...
63 *
64 * vlink::ShmConf::deinit_runtime();
65 * @endcode
66 *
67 * @note This header is compiled only when @c VLINK_SUPPORT_SHM is defined.
68 * @note The @c address and @c event strings must not exceed 80 characters each.
69 * @note The @c wait mode is only valid for @c kPublisher / @c kSubscriber nodes;
70 * using it with RPC or field nodes causes @c parse_protocol() to return @c false.
71 */
72
73#pragma once
74
75#ifdef VLINK_SUPPORT_SHM
76
77#include <cstdint>
78#include <string>
79
80#include "../impl/conf.h"
81
82namespace vlink {
83
84/**
85 * @struct ShmConf
86 * @brief Configuration for the @c shm:// Iceoryx shared-memory transport.
87 *
88 * @details
89 * Can be constructed directly or parsed from a URL string via @c Url.
90 * All string fields are capped at 80 characters by Iceoryx naming constraints.
91 */
92struct VLINK_EXPORT ShmConf final : public Conf {
93 std::string address; ///< Service/topic address (host + "/" + path from URL); max 80 characters.
94 std::string event; ///< Optional secondary event name; max 80 characters.
95 int32_t domain{0}; ///< Iceoryx domain identifier (non-negative).
96 int32_t depth{0}; ///< History buffer depth; 0 means no buffering.
97 int32_t history{0}; ///< History count; defaults to 0 for pub/sub and 1 for setter/getter.
98 int32_t wait{0}; ///< Non-zero enables blocking-wait mode (pub/sub only).
99
100 /**
101 * @brief Constructs a @c ShmConf with explicit parameters.
102 *
103 * @param _address Service/topic address string; max 80 characters.
104 * @param _event Optional event name; max 80 characters; empty by default.
105 * @param _domain Domain identifier; default 0.
106 * @param _depth Buffer depth; default 0.
107 * @param _history History count; default 0.
108 * @param _wait Blocking-wait flag; default 0 (disabled).
109 */
110 explicit ShmConf(const std::string& _address, const std::string& _event = "", int32_t _domain = 0, int32_t _depth = 0,
111 int32_t _history = 0, int32_t _wait = 0);
112
113 /**
114 * @brief Returns @c true if all fields equal those of @p conf.
115 *
116 * @param conf Configuration to compare.
117 * @return @c true if all six fields match.
118 */
119 [[nodiscard]] bool operator==(const ShmConf& conf) const noexcept;
120
121 /**
122 * @brief Returns @c true if any field differs from @p conf.
123 *
124 * @param conf Configuration to compare.
125 * @return Logical negation of @c operator==.
126 */
127 [[nodiscard]] bool operator!=(const ShmConf& conf) const noexcept;
128
129 /**
130 * @brief Returns @c TransportType::kShm identifying this transport.
131 *
132 * @return @c TransportType::kShm.
133 */
134 [[nodiscard]] TransportType get_transport_type() const override;
135
136 /**
137 * @brief Returns @c true if the in-process RouDi daemon has been initialised.
138 *
139 * @details
140 * Delegates to @c ShmFactory::has_roudi_inited(). Only meaningful when
141 * @c init_roudi() has been called from within this process.
142 *
143 * @return @c true if RouDi is running in-process.
144 */
145 [[nodiscard]] static bool has_roudi_inited();
146
147 /**
148 * @brief Returns @c true if the Iceoryx runtime has been initialised for this process.
149 *
150 * @details
151 * Delegates to @c ShmFactory::has_runtime_inited(). Returns @c true after a
152 * successful call to @c init_runtime() or after the @c global_init() path
153 * initialises the runtime automatically.
154 *
155 * @return @c true if the Iceoryx runtime is ready.
156 */
157 [[nodiscard]] static bool has_runtime_inited();
158
159 /**
160 * @brief Cheap probe for whether an Iceoryx RouDi daemon is reachable.
161 *
162 * @details
163 * Non-invasive check intended for CLI diagnostics, bench preflight, and UI
164 * pre-launch hints. Does NOT create an Iceoryx runtime, does NOT open any
165 * port, does NOT change any global state, and is safe to call repeatedly
166 * from any thread.
167 *
168 * @return @c true if RouDi (or a process embedding RouDi such as
169 * @c vlink-proxy) is currently running on this host, otherwise
170 * @c false.
171 *
172 * @see ShmFactory::has_roudi_running()
173 */
174 [[nodiscard]] static bool has_roudi_running();
175
176 /**
177 * @brief One-line preflight: ensure RouDi is available for @c shm:// nodes.
178 *
179 * @details
180 * POSIX: starts an in-process RouDi if none is running. Windows: returns
181 * @c false when no external RouDi is detected (in-process RouDi is not
182 * supported there). Result is cached across calls.
183 *
184 * @note Does NOT register @c init_runtime(); that happens lazily on first
185 * node creation.
186 *
187 * @param same_process_from_roudi @c true when the RouDi runs in the same
188 * process (see @c init_roudi()).
189 *
190 * @return @c true if RouDi is ready, @c false otherwise.
191 *
192 * @see ShmFactory::auto_init_roudi()
193 */
194 [[nodiscard]] static bool auto_init_roudi(bool same_process_from_roudi = false);
195
196 /**
197 * @brief Starts an embedded Iceoryx RouDi daemon in the current process.
198 *
199 * @details
200 * Use this when no external RouDi process is running and a single process
201 * needs to act as both the memory manager and a participant. Must be called
202 * before any @c shm:// nodes are created.
203 *
204 * @param config_path Path to a RouDi XML configuration file; empty for defaults.
205 * @param memory_strategy Memory pooling strategy (0 = default).
206 * @param monitoring_enable @c true to enable process-monitoring in RouDi.
207 */
208 static void init_roudi(const std::string& config_path = "", int memory_strategy = 0, bool monitoring_enable = true);
209
210 /**
211 * @brief Registers this process with the Iceoryx RouDi daemon.
212 *
213 * @details
214 * Must be called once before creating any @c shm:// nodes when connecting
215 * to an external RouDi. The @p name must be unique across all processes
216 * connecting to the same RouDi instance and must not exceed 80 characters.
217 *
218 * @param name Process registration name; empty uses a generated name.
219 * @param same_process_from_roudi @c true when the RouDi runs in the same process
220 * (see @c init_roudi()).
221 */
222 static void init_runtime(const std::string& name = "", bool same_process_from_roudi = false);
223
224 /**
225 * @brief Unregisters this process from the Iceoryx RouDi daemon.
226 *
227 * @details
228 * Releases all Iceoryx resources associated with the current process.
229 * Must be called before process exit when @c init_runtime() was used.
230 */
231 static void deinit_runtime();
232
233#ifndef VLINK_ENABLE_C_INTERFACE
235#endif
236 VLINK_ALLOW_IMPL_TYPE(kServer | kClient | kPublisher | kSubscriber | kSetter | kGetter)
237 VLINK_CONF_IMPL(ShmConf)
238};
239
240////////////////////////////////////////////////////////////////
241/// Details
242////////////////////////////////////////////////////////////////
243
244inline ShmConf::ShmConf(const std::string& _address, const std::string& _event, int32_t _domain, int32_t _depth,
245 int32_t _history, int32_t _wait)
246 : address(_address), event(_event), domain(_domain), depth(_depth), history(_history), wait(_wait) {}
247
248inline bool ShmConf::operator==(const ShmConf& conf) const noexcept {
249 return address == conf.address && event == conf.event && domain == conf.domain && depth == conf.depth &&
250 history == conf.history && wait == conf.wait;
251}
252
253inline bool ShmConf::operator!=(const ShmConf& conf) const noexcept { return !(*this == conf); }
254
255inline TransportType ShmConf::get_transport_type() const { return TransportType::kShm; }
256
257} // namespace vlink
258
259#endif
Abstract transport configuration base class and associated helper macros.
#define VLINK_CONF_IMPL(classname)
Standard boilerplate for concrete Conf subclass declarations.
定义 conf.h:227
#define VLINK_DECLARE_GLOBAL_PROPERTY()
Declares per-transport global state: thread count and property storage.
定义 conf.h:292
#define VLINK_ALLOW_IMPL_TYPE(type)
Declares a static constexpr bitmask of supported ImplType values.
定义 conf.h:268
#define VLINK_EXPORT
定义 macros.h:85