VLink  2.3.0
A high-performance communication middleware
node-inl.h
Go to the documentation of this file.
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 #pragma once
25 
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <utility>
30 
31 #include "../base/logger.h"
32 #include "../base/traits.h"
33 #include "../impl/types.h"
34 #include "../node.h"
35 #include "../version.h"
36 
37 namespace vlink {
38 
39 template <typename ImplT, SecurityType SecT>
40 inline bool Node<ImplT, SecT>::init() {
41  if constexpr (SecT == SecurityType::kWithSecurity) {
42  if VUNLIKELY (!impl_->security) {
43  VLOG_F("Node::init(): security node has no usable Security; check Security::Config. url: ", impl_->url);
44  }
45  }
46 
47  bool expected = false;
48 
49  if VUNLIKELY (!has_inited_.compare_exchange_strong(expected, true, std::memory_order_acq_rel,
50  std::memory_order_relaxed)) {
51  return false;
52  }
53 
55 
56  impl_->init_impl_type = impl_->impl_type;
57  impl_->init();
58  impl_->init_ext();
59 
60  is_support_loan_ = impl_->is_support_loan();
61 
62  return true;
63 }
64 
65 template <typename ImplT, SecurityType SecT>
67  bool expected = true;
68 
69  if VUNLIKELY (!has_inited_.compare_exchange_strong(expected, false, std::memory_order_acq_rel,
70  std::memory_order_relaxed)) {
71  return false;
72  }
73 
74  interrupt(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
75 
76  if (quit_mtx_.has_value()) {
77  std::lock_guard quit_lock(quit_mtx_.value());
78  }
79 
80  impl_->deinit();
81  impl_->deinit_ext();
82 
83  if constexpr (VLINK_HAS_MEMBER(ImplT, is_listened)) {
84  impl_->is_listened = false;
85  }
86 
87  return true;
88 }
89 
90 template <typename ImplT, SecurityType SecT>
92  return impl_->interrupt();
93 }
94 
95 template <typename ImplT, SecurityType SecT>
96 inline bool Node<ImplT, SecT>::has_inited() const {
97  return has_inited_.load(std::memory_order_acquire);
98 }
99 
100 template <typename ImplT, SecurityType SecT>
102  return impl_->is_support_loan();
103 }
104 
105 template <typename ImplT, SecurityType SecT>
106 inline Bytes Node<ImplT, SecT>::loan(int64_t size) {
107  Bytes bytes = impl_->loan(size);
108 
109  return bytes;
110 }
111 
112 template <typename ImplT, SecurityType SecT>
113 inline bool Node<ImplT, SecT>::return_loan(const Bytes& bytes) {
114  return impl_->return_loan(bytes);
115 }
116 
117 template <typename ImplT, SecurityType SecT>
119  return impl_->suspend();
120 }
121 
122 template <typename ImplT, SecurityType SecT>
124  return impl_->resume();
125 }
126 
127 template <typename ImplT, SecurityType SecT>
128 inline bool Node<ImplT, SecT>::is_suspend() const {
129  return impl_->is_suspend();
130 }
131 
132 template <typename ImplT, SecurityType SecT>
133 inline bool Node<ImplT, SecT>::attach(class MessageLoop* message_loop) {
134  return impl_->attach(message_loop);
135 }
136 
137 template <typename ImplT, SecurityType SecT>
139  return impl_->detach();
140 }
141 
142 template <typename ImplT, SecurityType SecT>
144  return impl_->get_message_loop();
145 }
146 
147 template <typename ImplT, SecurityType SecT>
149  return impl_->get_abstract_node();
150 }
151 
152 template <typename ImplT, SecurityType SecT>
154  return impl_->get_status(type);
155 }
156 
157 template <typename ImplT, SecurityType SecT>
159  impl_->register_status_handler(std::move(callback));
160 }
161 
162 template <typename ImplT, SecurityType SecT>
163 inline void Node<ImplT, SecT>::set_property(const std::string& prop, const std::string& value) {
164  impl_->set_property(prop, value);
165 }
166 
167 template <typename ImplT, SecurityType SecT>
168 inline std::string Node<ImplT, SecT>::get_property(const std::string& prop) const {
169  return impl_->get_property(prop);
170 }
171 
172 template <typename ImplT, SecurityType SecT>
174  return impl_->transport_type;
175 }
176 
177 template <typename ImplT, SecurityType SecT>
178 inline const std::string& Node<ImplT, SecT>::get_url() const {
179  return impl_->url;
180 }
181 
182 template <typename ImplT, SecurityType SecT>
183 inline void Node<ImplT, SecT>::set_record_path(const std::string& path) {
184  if VUNLIKELY (impl_->transport_type == TransportType::kIntra) {
185  VLOG_F("Node: Intra type does not support record.");
186  return;
187  }
188 
189  impl_->set_record_path(path);
190 }
191 
192 template <typename ImplT, SecurityType SecT>
193 inline void Node<ImplT, SecT>::set_ser_type(const std::string& ser_type, SchemaType schema_type) {
194  auto next_schema_type = impl_->schema_type;
195 
196  if (ser_type.empty()) {
197  next_schema_type = SchemaType::kUnknown;
198  } else if (SchemaData::is_valid_type(schema_type) && schema_type != SchemaType::kUnknown) {
199  next_schema_type = schema_type;
200  } else {
201  const auto inferred_schema_type = SchemaData::infer_ser_type(ser_type);
202 
203  if VLIKELY (inferred_schema_type != SchemaType::kUnknown) {
204  next_schema_type = inferred_schema_type;
205  } else if (impl_->schema_type == SchemaType::kRaw || impl_->schema_type == SchemaType::kZeroCopy) {
206  next_schema_type = SchemaType::kUnknown;
207  }
208  }
209 
210  const bool ser_changed = impl_->ser_type != ser_type;
211  const bool schema_changed = impl_->schema_type != next_schema_type;
212  const bool next_is_cdr_type = next_schema_type == SchemaType::kCdr;
213 
214  if VLIKELY (!ser_changed && !schema_changed) {
215  return;
216  }
217 
218  const bool has_inited = has_inited_.load(std::memory_order_acquire);
219  if VUNLIKELY (has_inited && impl_->transport_type == TransportType::kDds &&
220  (impl_->is_cdr_type != next_is_cdr_type || (impl_->is_cdr_type && ser_changed))) {
221  VLOG_F(
222  "Node: DDS raw/CDR mode and CDR type name cannot be changed while initialised; call "
223  "deinit() before changing serialization metadata.");
224  }
225 
226  if VUNLIKELY (ser_changed && !impl_->ser_type.empty() && !ser_type.empty()) {
227  CLOG_W("Node: Enforce serialization type [%s] => [%s].", impl_->ser_type.c_str(), ser_type.c_str());
228  }
229 
230  if VUNLIKELY (schema_changed && SchemaData::is_real_type(impl_->schema_type) &&
231  SchemaData::is_real_type(next_schema_type)) {
232  CLOG_W("Node: Enforce schema type [%d] => [%d].", static_cast<int>(impl_->schema_type),
233  static_cast<int>(next_schema_type));
234  }
235 
236  if VUNLIKELY (has_inited) {
237  impl_->deinit_ext();
238  }
239 
240  impl_->ser_type = ser_type;
241  impl_->schema_type = next_schema_type;
242 
243  if (impl_->transport_type == TransportType::kDds) {
244  impl_->is_cdr_type = next_is_cdr_type;
245  if constexpr (VLINK_HAS_MEMBER(ImplT, is_resp_cdr_type)) {
246  if (impl_->is_resp_type) {
247  impl_->is_resp_cdr_type = next_is_cdr_type;
248  }
249  }
250  }
251 
252  if VUNLIKELY (has_inited) {
253  impl_->init_ext();
254  }
255 }
256 
257 template <typename ImplT, SecurityType SecT>
258 inline const std::string& Node<ImplT, SecT>::get_ser_type() const {
259  return impl_->ser_type;
260 }
261 
262 template <typename ImplT, SecurityType SecT>
264  return impl_->schema_type;
265 }
266 
267 template <typename ImplT, SecurityType SecT>
269  if VUNLIKELY (has_inited_.load(std::memory_order_acquire)) {
270  impl_->deinit_ext();
271  impl_->set_discovery_enabled(enable);
272  impl_->init_ext();
273  } else {
274  impl_->set_discovery_enabled(enable);
275  }
276 }
277 
278 template <typename ImplT, SecurityType SecT>
280  return impl_->get_discovery_enabled();
281 }
282 
283 template <typename ImplT, SecurityType SecT>
284 inline void Node<ImplT, SecT>::bind_proto_arena(void* proto_arena) {
285  proto_arena_ = proto_arena;
286 }
287 
288 template <typename ImplT, SecurityType SecT>
289 inline double Node<ImplT, SecT>::get_cpu_usage() const {
290  if VUNLIKELY (impl_->profiler) {
291  return impl_->profiler->get();
292  } else {
293  return -1;
294  }
295 }
296 
297 template <typename ImplT, SecurityType SecT>
299  return quit_mtx_.has_value();
300 }
301 
302 template <typename ImplT, SecurityType SecT>
303 inline void Node<ImplT, SecT>::set_safety_quit(bool safety_quit) {
304  if VUNLIKELY (safety_quit) {
305  if (!quit_mtx_.has_value()) {
306  quit_mtx_.emplace();
307  }
308  } else {
309  if (quit_mtx_.has_value()) {
310  quit_mtx_.reset();
311  }
312  }
313 }
314 
315 template <typename ImplT, SecurityType SecT>
316 inline void Node<ImplT, SecT>::set_ssl_options(const SslOptions& options) {
317  impl_->set_ssl_options(options);
318 }
319 
320 template <typename ImplT, SecurityType SecT>
322  static_assert(std::is_base_of_v<NodeImpl, ImplT>, "ImplT must be derived from NodeImpl.");
323 }
324 
325 template <typename ImplT, SecurityType SecT>
327  deinit(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
328 }
329 
330 template <typename ImplT, SecurityType SecT>
332  auto sec_cfg = cfg;
333 
334  return enable_security(std::move(sec_cfg));
335 }
336 
337 template <typename ImplT, SecurityType SecT>
339  static_assert(SecT == SecurityType::kWithSecurity, "Must be security type.");
340 
341  if VUNLIKELY (!impl_) {
342  return false;
343  }
344 
345  if VUNLIKELY (has_inited_.load(std::memory_order_acquire)) {
346  VLOG_W("Node::enable_security(): must run before init(); rejected to avoid live-traffic race.");
347  return false;
348  }
349 
350  return impl_->enable_security(std::move(cfg));
351 }
352 
353 template <typename ImplT, SecurityType SecT>
354 template <typename CallbackT, typename... ArgsT>
355 inline void Node<ImplT, SecT>::invoke_callback(const CallbackT& callback, ArgsT&&... args) {
356  if VUNLIKELY (quit_mtx_.has_value()) {
357  std::lock_guard quit_lock(quit_mtx_.value());
358 
359  if VUNLIKELY (!has_inited_.load(std::memory_order_acquire)) {
360  return;
361  }
362 
363  std::invoke(callback, std::forward<ArgsT>(args)...);
364  } else {
365  std::invoke(callback, std::forward<ArgsT>(args)...);
366  }
367 }
368 
369 template <typename ImplT, SecurityType SecT>
370 template <typename TypeT>
372  if constexpr (Traits::IsSharedPtr<TypeT>()) {
373  return std::make_shared<typename TypeT::element_type>();
374 #ifdef VLINK_HAS_PROTOBUF
375  } else if constexpr (Serializer::is_proto_ptr_type<TypeT>()) {
376  if VLIKELY (this->proto_arena_) {
377  return google::protobuf::Arena::Create<std::remove_pointer_t<TypeT>>(
378  static_cast<google::protobuf::Arena*>(this->proto_arena_));
379  }
380 
381  VLOG_F("Node: Proto arena is not bound, url: ", this->impl_->url, ".");
382 
383  return nullptr;
384 #endif
385  } else if constexpr (std::is_default_constructible_v<TypeT>) {
386  return TypeT{};
387  } else {
388  static_assert(Traits::ExpectFalse<TypeT>(), "TypeT is not default constructible.");
389  return {};
390  }
391 }
392 
393 } // namespace vlink
#define VLOG_F(...)
Definition: logger.h:906
#define VLOG_W(...)
Definition: logger.h:902
#define CLOG_W(...)
Definition: logger.h:914
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_HAS_MEMBER(T, member)
Macro Definitions.
Definition: traits.h:325
#define VLINK_VERSION_PATCH
Definition: version.h:94
#define VLINK_VERSION_MAJOR
Definition: version.h:92
#define VLINK_VERSION_MINOR
Definition: version.h:93