VLink  2.3.0
A high-performance communication middleware
client-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 "../base/memory_resource.h"
33 #include "../client.h"
34 #include "../impl/url.h"
35 #include "../serializer.h"
36 #include "../zerocopy/audio_frame.h"
37 #include "../zerocopy/camera_frame.h"
38 #include "../zerocopy/object_array.h"
39 #include "../zerocopy/occupancy_grid.h"
40 #include "../zerocopy/point_cloud.h"
41 #include "../zerocopy/proxy_data.h"
42 #include "../zerocopy/raw_data.h"
43 #include "../zerocopy/tensor.h"
44 
45 namespace vlink {
46 
47 template <typename ReqT, typename RespT, SecurityType SecT>
49  const std::string& url_str, InitType type) {
50  return std::make_unique<Client<ReqT, RespT, SecT>>(url_str, type);
51 }
52 
53 template <typename ReqT, typename RespT, SecurityType SecT>
55  const std::string& url_str, InitType type) {
56  return std::make_shared<Client<ReqT, RespT, SecT>>(url_str, type);
57 }
58 
59 template <typename ReqT, typename RespT, SecurityType SecT>
60 template <typename ConfT, typename>
61 inline Client<ReqT, RespT, SecT>::Client(const ConfT& conf, InitType type) {
62  static_assert(ConfT::get_allow_impl_type() & kImplType, "Conf does not support client mode.");
63 
64  if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
65  VLOG_F(conf, " client configuration is invalid or could not be parsed.");
66  return;
67  }
68 
69  this->impl_ = conf.create_client();
70 
71  if VUNLIKELY (!this->impl_) {
72  VLOG_F(conf, " client implementation not available for this transport.");
73  return;
74  }
75 
76  this->impl_->transport_type = conf.get_transport_type();
77  this->impl_->ser_type = Serializer::get_serialized_type<kReqType, ReqT>();
78 
79  if constexpr (kHasResp) {
80  const auto resp_ser_type = Serializer::get_serialized_type<kRespType, RespT>();
81 
82  if (!this->impl_->ser_type.empty() || !resp_ser_type.empty()) {
83  this->impl_->ser_type += "|" + resp_ser_type;
84  }
85  }
86 
87  {
88  constexpr auto kReqSchemaType = Serializer::get_schema_type<kReqType, ReqT>();
89  constexpr auto kRespSchemaType = Serializer::get_schema_type<kRespType, RespT>();
90 
91  if constexpr (kHasResp && kReqSchemaType != kRespSchemaType) {
92  this->impl_->schema_type = SchemaType::kUnknown;
93  } else {
94  this->impl_->schema_type = kReqSchemaType;
95  }
96  }
97 
98  this->impl_->is_cdr_type = Serializer::is_cdr_type<ReqT>();
99  this->impl_->is_resp_type = kHasResp;
100 
101  if constexpr (kHasResp) {
102  this->impl_->is_resp_cdr_type = Serializer::is_cdr_type<RespT>();
103  }
104 
105  if constexpr (std::is_same_v<ConfT, Url>) {
106  this->impl_->url = conf.get_str();
107  }
108 
109  if constexpr (SecT == SecurityType::kWithSecurity) {
110  this->impl_->is_security_type = true;
111  }
112 
113  if VLIKELY (type == InitType::kWithInit) {
114  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
115  }
116 }
117 
118 template <typename ReqT, typename RespT, SecurityType SecT>
119 inline Client<ReqT, RespT, SecT>::Client(const std::string& url_str, InitType type)
120  : Client<ReqT, RespT, SecT>(Url(url_str), type) {}
121 
122 template <typename ReqT, typename RespT, SecurityType SecT>
124  {
125  std::lock_guard lock(future_mtx_);
126  future_map_.clear();
127  }
128 
129  // NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
130  this->deinit();
131 }
132 
133 template <typename ReqT, typename RespT, SecurityType SecT>
135  this->impl_->detect_connected(std::move(callback));
136 }
137 
138 template <typename ReqT, typename RespT, SecurityType SecT>
139 inline bool Client<ReqT, RespT, SecT>::wait_for_connected(std::chrono::milliseconds timeout) {
140  if VUNLIKELY (timeout.count() == 0) {
141  VLOG_W("Client: Timeout value is 0, using infinite wait instead.");
142  timeout = Timeout::kInfinite;
143  }
144 
145  return this->impl_->wait_for_connected(timeout);
146 }
147 
148 template <typename ReqT, typename RespT, SecurityType SecT>
150  return this->impl_->is_connected();
151 }
152 
153 template <typename ReqT, typename RespT, SecurityType SecT>
154 inline bool Client<ReqT, RespT, SecT>::invoke(const ReqT& req, RespT& resp, std::chrono::milliseconds timeout) {
155  if VUNLIKELY (timeout.count() == 0) {
156  VLOG_W("Client: Timeout value is 0, using infinite wait instead.");
157  timeout = Timeout::kInfinite;
158  }
159 
160 #ifndef VLINK_DISABLE_PROFILER
161  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
162 #endif
163 
164  static_assert(kHasResp, "Invoke requires a response type.");
165 
166  bool ret = false;
167 
168  if constexpr (std::is_same_v<ReqT, Bytes> && std::is_same_v<RespT, Bytes>) {
169  bool received = false;
170  ret = call_bytes(
171  req,
172  [&resp, &received](const Bytes& resp_data) {
173  resp = resp_data;
174  received = true;
175  },
176  timeout);
177  ret = ret && received;
178  } else {
179  Bytes req_data;
180  const bool use_loan = SecT != SecurityType::kWithSecurity && this->is_support_loan_;
181 
182  if constexpr (std::is_same_v<ReqT, Bytes>) {
183  req_data =
184  req.is_loaned() ? Bytes::loan_internal(req.data(), req.size()) : Bytes::shallow_copy(req.data(), req.size());
185  } else if VUNLIKELY (!Serializer::serialize_to_transport<kReqType>(
186  req, req_data, this->impl_->transport_type, use_loan,
187  [this](size_t size) { return this->impl_->loan(size); })) {
188  VLOG_T("Client serialize failed, url: ", this->impl_->url, ".");
189 
190  if constexpr (SecT != SecurityType::kWithSecurity) {
191  if (this->is_support_loan_) {
192  this->impl_->return_loan(req_data);
193  }
194  }
195 
196  return false;
197  }
198 
199  bool deserialize_success = false;
200 
201  ret = call_bytes(
202  req_data,
203  [this, &resp, &deserialize_success](const Bytes& resp_data) {
204  if VLIKELY (deserialize_response(resp_data, resp)) {
205  deserialize_success = true;
206  } else {
207  VLOG_T("Client deserialize failed, url: ", this->impl_->url, ".");
208  }
209  },
210  timeout);
211 
212  ret = ret && deserialize_success;
213  }
214 
215  return ret;
216 }
217 
218 template <typename ReqT, typename RespT, SecurityType SecT>
219 inline std::optional<RespT> Client<ReqT, RespT, SecT>::invoke(const ReqT& req, std::chrono::milliseconds timeout) {
220  if VUNLIKELY (timeout.count() == 0) {
221  VLOG_W("Client: Timeout value is 0, using infinite wait instead.");
222  timeout = Timeout::kInfinite;
223  }
224 
225 #ifndef VLINK_DISABLE_PROFILER
226  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
227 #endif
228 
229  auto resp = this->template get_default_value<RespT>();
230 
231  if VLIKELY (invoke(req, resp, timeout)) {
232  return std::make_optional<RespT>(std::move(resp));
233  }
234 
235  return std::nullopt;
236 }
237 
238 template <typename ReqT, typename RespT, SecurityType SecT>
239 inline bool Client<ReqT, RespT, SecT>::invoke(const ReqT& req, RespCallback&& callback) {
240 #ifndef VLINK_DISABLE_PROFILER
241  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
242 #endif
243 
244  static_assert(kHasResp, "Invoke requires a response type.");
245 
246  bool ret = false;
247 
248  if constexpr (std::is_same_v<ReqT, Bytes> && std::is_same_v<RespT, Bytes>) {
249  ret = call_bytes(req, [callback = std::move(callback)](const Bytes& resp_data) { callback(resp_data); });
250  } else {
251  Bytes req_data;
252  const bool use_loan = SecT != SecurityType::kWithSecurity && this->is_support_loan_;
253 
254  if constexpr (std::is_same_v<ReqT, Bytes>) {
255  req_data =
256  req.is_loaned() ? Bytes::loan_internal(req.data(), req.size()) : Bytes::shallow_copy(req.data(), req.size());
257  } else if VUNLIKELY (!Serializer::serialize_to_transport<kReqType>(
258  req, req_data, this->impl_->transport_type, use_loan,
259  [this](size_t size) { return this->impl_->loan(size); })) {
260  VLOG_T("Client serialize failed, url: ", this->impl_->url, ".");
261 
262  if constexpr (SecT != SecurityType::kWithSecurity) {
263  if (this->is_support_loan_) {
264  this->impl_->return_loan(req_data);
265  }
266  }
267 
268  return false;
269  }
270 
271  ret = call_bytes(req_data, [this, callback = std::move(callback)](const Bytes& resp_data) {
272  // NOLINTNEXTLINE(readability-qualified-auto)
273  auto resp = this->template get_default_value<RespT>();
274 
275  if VUNLIKELY (!Serializer::deserialize<kRespType>(resp_data, resp, this->impl_->transport_type)) {
276  VLOG_T("Client deserialize failed, url: ", this->impl_->url, ".");
277  return;
278  }
279 
280  callback(resp);
281  });
282  }
283 
284  return ret;
285 }
286 
287 template <typename ReqT, typename RespT, SecurityType SecT>
288 inline std::future<RespT> Client<ReqT, RespT, SecT>::async_invoke(const ReqT& req) {
289 #ifndef VLINK_DISABLE_PROFILER
290  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
291 #endif
292 
293  static_assert(kHasResp, "async_invoke requires a response type.");
294 
295  auto pro = MemoryResource::make_shared<std::promise<RespT>>();
296  auto future = pro->get_future();
297 
298  bool ret = false;
299  int64_t target_seq = 0;
300 
301  {
302  std::lock_guard lock(future_mtx_);
303  target_seq = future_seq_++;
304  future_map_.emplace(target_seq, pro);
305  }
306 
307  auto cleanup_on_error = [this, target_seq, pro](const std::string& error_str) {
308  std::lock_guard lock(future_mtx_);
309  future_map_.erase(target_seq);
310 
311  try {
312  throw Exception::RuntimeError(error_str);
313  } catch (std::exception&) {
314  pro->set_exception(std::current_exception());
315  }
316  };
317 
318  if constexpr (std::is_same_v<ReqT, Bytes> && std::is_same_v<RespT, Bytes>) {
319  ret = call_bytes(req, [this, target_seq](const Bytes& resp_data) {
320  std::lock_guard lock(future_mtx_);
321  auto it = future_map_.find(target_seq);
322 
323  if VLIKELY (it != future_map_.end()) {
324  it->second->set_value(resp_data);
325  future_map_.erase(it);
326  }
327  });
328  } else {
329  Bytes req_data;
330  const bool use_loan = SecT != SecurityType::kWithSecurity && this->is_support_loan_;
331 
332  if constexpr (std::is_same_v<ReqT, Bytes>) {
333  req_data =
334  req.is_loaned() ? Bytes::loan_internal(req.data(), req.size()) : Bytes::shallow_copy(req.data(), req.size());
335  } else if VUNLIKELY (!Serializer::serialize_to_transport<kReqType>(
336  req, req_data, this->impl_->transport_type, use_loan,
337  [this](size_t size) { return this->impl_->loan(size); })) {
338  VLOG_T("Client serialize failed, url: ", this->impl_->url, ".");
339 
340  if constexpr (SecT != SecurityType::kWithSecurity) {
341  if (this->is_support_loan_) {
342  this->impl_->return_loan(req_data);
343  }
344  }
345 
346  cleanup_on_error("Client async_invoke error (Failed to serialize req)");
347  return future;
348  }
349 
350  ret = call_bytes(req_data, [this, target_seq](const Bytes& resp_data) {
351  bool convert_success = false;
352 
353  auto resp = this->template get_default_value<RespT>();
354 
355  if VLIKELY (deserialize_response(resp_data, resp)) {
356  convert_success = true;
357  } else {
358  VLOG_T("Client deserialize failed, url: ", this->impl_->url, ".");
359  }
360 
361  std::lock_guard lock(future_mtx_);
362 
363  auto it = future_map_.find(target_seq);
364 
365  if VLIKELY (it != future_map_.end()) {
366  if VLIKELY (convert_success) {
367  it->second->set_value(std::move(resp));
368  } else {
369  try {
370  throw Exception::RuntimeError("Client async_invoke error (Failed to deserialize resp)");
371  } catch (std::exception&) {
372  it->second->set_exception(std::current_exception());
373  }
374  }
375 
376  future_map_.erase(it);
377  }
378  });
379  }
380 
381  if VUNLIKELY (!ret) {
382  cleanup_on_error("Client async_invoke error (Failed to call)");
383  }
384 
385  return future;
386 }
387 
388 template <typename ReqT, typename RespT, SecurityType SecT>
389 inline bool Client<ReqT, RespT, SecT>::send(const ReqT& req) {
390 #ifndef VLINK_DISABLE_PROFILER
391  CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
392 #endif
393 
394  static_assert(!kHasResp, "Send not supported; use invoke() for request-response.");
395 
396  bool ret = false;
397 
398  if constexpr (std::is_same_v<ReqT, Bytes>) {
399  ret = call_bytes(req);
400  } else {
401  Bytes req_data;
402  const bool use_loan = SecT != SecurityType::kWithSecurity && this->is_support_loan_;
403 
404  if VUNLIKELY (!Serializer::serialize_to_transport<kReqType>(
405  req, req_data, this->impl_->transport_type, use_loan,
406  [this](size_t size) { return this->impl_->loan(size); })) {
407  VLOG_T("Client serialize failed, url: ", this->impl_->url, ".");
408 
409  if constexpr (SecT != SecurityType::kWithSecurity) {
410  if (this->is_support_loan_) {
411  this->impl_->return_loan(req_data);
412  }
413  }
414 
415  return false;
416  }
417 
418  ret = call_bytes(req_data);
419  }
420 
421  return ret;
422 }
423 
424 template <typename ReqT, typename RespT, SecurityType SecT>
425 inline bool Client<ReqT, RespT, SecT>::deserialize_response(const Bytes& data, RespT& response) {
426  using RealType = typename Traits::RemoveSharedPtr<RespT>::Type;
427 
428  if constexpr (std::is_same_v<RealType, zerocopy::RawData> || std::is_same_v<RealType, zerocopy::CameraFrame> ||
429  std::is_same_v<RealType, zerocopy::AudioFrame> || std::is_same_v<RealType, zerocopy::PointCloud> ||
430  std::is_same_v<RealType, zerocopy::Tensor> || std::is_same_v<RealType, zerocopy::OccupancyGrid> ||
431  std::is_same_v<RealType, zerocopy::ObjectArray> || std::is_same_v<RealType, zerocopy::ProxyData>) {
432  if constexpr (Traits::IsSharedPtr<RespT>()) {
433  if VUNLIKELY (!response) {
434  return false;
435  }
436  }
437 
438  RealType decoded;
439 
440  if VUNLIKELY (!Serializer::deserialize<kRespType>(data, decoded, this->impl_->transport_type)) {
441  return false;
442  }
443 
444  auto& output = Serializer::deref(response);
445 
446  if (decoded.is_owner()) {
447  output = std::move(decoded);
448  return true;
449  }
450 
451  return output.deep_copy(decoded);
452  } else {
453  return Serializer::deserialize<kRespType>(data, response, this->impl_->transport_type);
454  }
455 }
456 
457 template <typename ReqT, typename RespT, SecurityType SecT>
458 inline bool Client<ReqT, RespT, SecT>::call_bytes(const Bytes& req_data, NodeImpl::MsgCallback&& callback,
459  std::chrono::milliseconds timeout) {
460  if constexpr (SecT == SecurityType::kWithSecurity) {
461  Bytes req_sec_data;
462 
463  if VUNLIKELY (!this->impl_->security || !this->impl_->security->encrypt(req_data, req_sec_data)) {
464  VLOG_T("Client encrypt failed, url: ", this->impl_->url, ".");
465  return false;
466  }
467 
468  if VUNLIKELY (!callback) {
469  return this->impl_->call(req_sec_data, nullptr, timeout);
470  }
471 
472  return this->impl_->call(
473  req_sec_data,
474  [this, callback = std::move(callback)](const Bytes& resp_data) {
475  Bytes resp_sec_data;
476 
477  if VUNLIKELY (!this->impl_->security || !this->impl_->security->decrypt(resp_data, resp_sec_data)) {
478  VLOG_T("Client decrypt failed, url: ", this->impl_->url, ".");
479  return;
480  }
481 
482  this->invoke_callback(callback, resp_sec_data);
483  },
484  timeout);
485  } else {
486  this->impl_->try_record(ActionType::kClientRequest, req_data);
487 
488  if VUNLIKELY (!callback) {
489  return this->impl_->call(req_data, nullptr, timeout);
490  }
491 
492  return this->impl_->call(
493  req_data,
494  [this, callback = std::move(callback)](const Bytes& resp_data) {
495  this->impl_->try_record(ActionType::kClientResponse, resp_data);
496 
497  this->invoke_callback(callback, resp_data);
498  },
499  timeout);
500  }
501 }
502 
503 template <typename ReqT, typename RespT>
504 template <typename SecurityConfigT>
506  const std::string& url_str, SecurityConfigT&& sec_cfg, InitType type) {
507  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
508  "SecurityConfigT must be Security::Config.");
509 
510  return std::make_unique<SecurityClient<ReqT, RespT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
511 }
512 
513 template <typename ReqT, typename RespT>
514 template <typename SecurityConfigT>
516  const std::string& url_str, SecurityConfigT&& sec_cfg, InitType type) {
517  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
518  "SecurityConfigT must be Security::Config.");
519 
520  return std::make_shared<SecurityClient<ReqT, RespT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
521 }
522 
523 template <typename ReqT, typename RespT>
524 template <typename ConfT, typename SecurityConfigT, typename>
525 inline SecurityClient<ReqT, RespT>::SecurityClient(const ConfT& conf, SecurityConfigT&& sec_cfg, InitType type)
526  : Client<ReqT, RespT, SecurityType::kWithSecurity>(conf, InitType::kWithoutInit) {
527  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
528  "SecurityConfigT must be Security::Config.");
529 
530  this->enable_security(std::forward<SecurityConfigT>(sec_cfg));
531 
532  if VLIKELY (type == InitType::kWithInit) {
533  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
534  }
535 }
536 
537 template <typename ReqT, typename RespT>
538 template <typename SecurityConfigT>
539 inline SecurityClient<ReqT, RespT>::SecurityClient(const std::string& url_str, SecurityConfigT&& sec_cfg, InitType type)
540  : Client<ReqT, RespT, SecurityType::kWithSecurity>(url_str, InitType::kWithoutInit) {
541  static_assert(std::is_same_v<std::decay_t<SecurityConfigT>, Security::Config>,
542  "SecurityConfigT must be Security::Config.");
543 
544  this->enable_security(std::forward<SecurityConfigT>(sec_cfg));
545 
546  if VLIKELY (type == InitType::kWithInit) {
547  this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
548  }
549 }
550 
551 } // namespace vlink
#define VLOG_F(...)
Definition: logger.h:906
#define VLOG_W(...)
Definition: logger.h:902
#define VLOG_T(...)
Definition: logger.h:896
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284