VLink  2.1.0
A high-performance communication middleware
subscriber-inl.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 #pragma once
25 
26 #include <memory>
27 #include <string>
28 #include <utility>
29 
30 #include "../base/cpu_profiler_guard.h"
31 #include "../base/logger.h"
32 #include "../impl/url.h"
33 #include "../serializer.h"
34 #include "../subscriber.h"
35 
36 namespace vlink {
37 
38 template <typename MsgT, SecurityType SecT>
40  InitType type) {
41  return std::make_unique<Subscriber<MsgT, SecT>>(url_str, type);
42 }
43 
44 template <typename MsgT, SecurityType SecT>
46  InitType type) {
47  return std::make_shared<Subscriber<MsgT, SecT>>(url_str, type);
48 }
49 
50 template <typename MsgT, SecurityType SecT>
51 template <typename ConfT, typename>
52 inline Subscriber<MsgT, SecT>::Subscriber(const ConfT& conf, InitType type) {
53  static_assert(ConfT::get_allow_impl_type() & kImplType, "Conf does not support subscriber mode.");
54 
55  if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
56  VLOG_F(conf, " subscriber configuration is invalid or could not be parsed.");
57  return;
58  }
59 
60  this->impl_ = conf.create_subscriber();
61 
62  if VUNLIKELY (!this->impl_) {
63  VLOG_F(conf, " subscriber implementation not available for this transport.");
64  return;
65  }
66 
67  this->impl_->transport_type = conf.get_transport_type();
68  this->impl_->ser_type = Serializer::get_serialized_type<kMsgType, MsgT>();
69  this->impl_->schema_type = Serializer::get_schema_type<kMsgType, MsgT>();
70  this->impl_->is_cdr_type = Serializer::is_cdr_type<MsgT>();
71 
72  if constexpr (std::is_same_v<ConfT, Url>) {
73  this->impl_->url = conf.get_str();
74  }
75 
76  if constexpr (SecT == SecurityType::kWithSecurity) {
77  this->impl_->is_security_type = true;
78  }
79 
80  if VLIKELY (type == InitType::kWithInit) {
81  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
82  }
83 }
84 
85 template <typename MsgT, SecurityType SecT>
86 inline Subscriber<MsgT, SecT>::Subscriber(const std::string& url_str, InitType type)
87  : Subscriber<MsgT, SecT>(Url(url_str), type) {}
88 
89 template <typename MsgT, SecurityType SecT>
91  if constexpr (Traits::IsSharedPtr<MsgT>()) {
92  if constexpr (std::is_base_of_v<IntraDataType, typename MsgT::element_type>) {
93  static_assert(SecT != SecurityType::kWithSecurity, "IntraData must without security.");
94 
95  if (this->impl_->transport_type == TransportType::kIntra) {
96  return listen_intra(std::move(callback));
97  }
98  }
99  }
100 
101  return listen_bytes([this, callback = std::move(callback)](const Bytes& data) {
102 #ifndef VLINK_DISABLE_PROFILER
103  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
104 #endif
105 
106  if constexpr (std::is_same_v<MsgT, Bytes>) {
107  (void)this;
108 
109  callback(data);
110  } else {
111  auto msg = this->template get_default_value<MsgT>();
112 
113  if VUNLIKELY (!Serializer::deserialize<kMsgType>(data, msg, this->impl_->transport_type)) {
114  VLOG_T("Subscriber deserialize failed, url: ", this->impl_->url, ".");
115  return;
116  }
117 
118  callback(msg);
119  }
120  });
121 }
122 
123 template <typename MsgT, SecurityType SecT>
125  this->impl_->set_latency_and_lost_enabled(enable);
126 }
127 
128 template <typename MsgT, SecurityType SecT>
130  return this->impl_->is_latency_and_lost_enabled();
131 }
132 
133 template <typename MsgT, SecurityType SecT>
134 inline int64_t Subscriber<MsgT, SecT>::get_latency() const {
135  return this->impl_->get_latency();
136 }
137 
138 template <typename MsgT, SecurityType SecT>
140  return this->impl_->get_lost();
141 }
142 
143 template <typename MsgT, SecurityType SecT>
145  if VUNLIKELY (this->has_inited_.load(std::memory_order_acquire)) {
146  this->impl_->deinit_ext();
147  this->impl_->impl_type = kGetter;
148  this->impl_->init_ext();
149  } else {
150  this->impl_->impl_type = kGetter;
151  }
152 }
153 
154 template <typename MsgT, SecurityType SecT>
156  if VUNLIKELY (!this->has_inited_.load(std::memory_order_acquire)) {
157  VLOG_F("Subscriber::listen_bytes() called before init().");
158  return false;
159  }
160 
161  if VUNLIKELY (this->impl_->is_listened) {
162  VLOG_F("Subscriber has already been listened, url: ", this->impl_->url, ".");
163  return false;
164  }
165 
166  bool ret = this->impl_->listen([this, callback = std::move(callback)](const Bytes& data) {
167  if constexpr (SecT == SecurityType::kWithSecurity) {
168  Bytes sec_data;
169 
170  if VUNLIKELY (!this->impl_->security || !this->impl_->security->decrypt(data, sec_data)) {
171  VLOG_T("Subscriber decrypt failed, url: ", this->impl_->url, ".");
172  return;
173  }
174 
175  this->invoke_callback(callback, sec_data);
176  } else {
177  this->impl_->try_record(ActionType::kSubscribe, data);
178 
179  this->invoke_callback(callback, data);
180  }
181  });
182 
183  this->impl_->is_listened = ret;
184 
185  return ret;
186 }
187 
188 template <typename MsgT, SecurityType SecT>
189 inline bool Subscriber<MsgT, SecT>::listen_intra(MsgCallback&& callback) {
190  if VUNLIKELY (!this->has_inited_.load(std::memory_order_acquire)) {
191  VLOG_F("Subscriber::listen_intra() called before init().");
192  return false;
193  }
194 
195  if VUNLIKELY (this->impl_->is_listened) {
196  VLOG_F("Subscriber has already been listened, url: ", this->impl_->url, ".");
197  return false;
198  }
199 
200  bool ret = this->impl_->listen([this, callback = std::move(callback)](const IntraData& intra_data) {
201 #ifndef VLINK_DISABLE_PROFILER
202  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
203 #endif
204 
205  if constexpr (Traits::IsSharedPtr<MsgT>()) {
206 #if defined(NDEBUG) || defined(__ANDROID__)
207  auto intra_msg = std::static_pointer_cast<typename MsgT::element_type>(intra_data);
208 #else
209  auto intra_msg = std::dynamic_pointer_cast<typename MsgT::element_type>(intra_data);
210 #endif
211 
212  if VLIKELY (intra_msg) {
213  MsgT typed_msg(std::move(intra_msg));
214  this->invoke_callback(callback, typed_msg);
215  } else {
216  VLOG_T("Subscriber get intra data failed, url: ", this->impl_->url, ".");
217  }
218  }
219  });
220 
221  this->impl_->is_listened = ret;
222 
223  return ret;
224 }
225 
226 template <typename MsgT>
227 template <typename SecurityConfigT>
229  SecurityConfigT&& sec_cfg,
230  InitType type) {
231  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
232  "SecurityConfigT must be Security::Config.");
233 
234  return std::make_unique<SecuritySubscriber<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
235 }
236 
237 template <typename MsgT>
238 template <typename SecurityConfigT>
240  SecurityConfigT&& sec_cfg,
241  InitType type) {
242  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
243  "SecurityConfigT must be Security::Config.");
244 
245  return std::make_shared<SecuritySubscriber<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
246 }
247 
248 template <typename MsgT>
249 template <typename ConfT, typename SecurityConfigT, typename>
250 inline SecuritySubscriber<MsgT>::SecuritySubscriber(const ConfT& conf, SecurityConfigT&& sec_cfg, InitType type)
252  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
253  "SecurityConfigT must be Security::Config.");
254 
255  this->enable_security(std::forward<SecurityConfigT>(sec_cfg));
256 
257  if VLIKELY (type == InitType::kWithInit) {
258  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
259  }
260 }
261 
262 template <typename MsgT>
263 template <typename SecurityConfigT>
264 inline SecuritySubscriber<MsgT>::SecuritySubscriber(const std::string& url_str, SecurityConfigT&& sec_cfg,
265  InitType type)
267  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
268  "SecurityConfigT must be Security::Config.");
269 
270  this->enable_security(std::forward<SecurityConfigT>(sec_cfg));
271 
272  if VLIKELY (type == InitType::kWithInit) {
273  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
274  }
275 }
276 
277 } // namespace vlink
#define VLOG_F(...)
Definition: logger.h:843
#define VLOG_T(...)
Definition: logger.h:833
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284