VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
setter-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
30#include "../base/logger.h"
31#include "../impl/url.h"
32#include "../serializer.h"
33#include "../setter.h"
34
35namespace vlink {
36
37template <typename ValueT, SecurityType SecT>
39 InitType type) {
40 return std::make_unique<Setter<ValueT, SecT>>(url_str, type);
41}
42
43template <typename ValueT, SecurityType SecT>
45 InitType type) {
46 return std::make_shared<Setter<ValueT, SecT>>(url_str, type);
47}
48
49template <typename ValueT, SecurityType SecT>
50template <typename ConfT, typename>
51inline Setter<ValueT, SecT>::Setter(const ConfT& conf, InitType type) {
52 static_assert(ConfT::get_allow_impl_type() & kImplType, "Conf does not support setter mode.");
53
54 if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
55 VLOG_F(conf, " setter configuration is invalid or could not be parsed.");
56 return;
57 }
58
59 this->impl_ = conf.create_setter();
60
61 if VUNLIKELY (!this->impl_) {
62 VLOG_F(conf, " setter implementation not available for this transport.");
63 return;
64 }
65
66 this->impl_->transport_type = conf.get_transport_type();
68 this->impl_->schema_type = Serializer::get_schema_type<kValueType, ValueT>();
69 this->impl_->is_cdr_type = Serializer::is_cdr_type<ValueT>();
70
71 if constexpr (std::is_same_v<ConfT, Url>) {
72 this->impl_->url = conf.get_str();
73 }
74
75 if constexpr (SecT == SecurityType::kWithSecurity) {
76 this->impl_->is_security_type = true;
77 this->enable_security();
78 }
79
80 if (type == InitType::kWithInit) {
81 this->init(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
82 }
83}
84
85template <typename ValueT, SecurityType SecT>
86inline Setter<ValueT, SecT>::Setter(const std::string& url_str, InitType type)
87 : Setter<ValueT, SecT>(Url(url_str), type) {}
88
89template <typename ValueT, SecurityType SecT>
92 return false;
93 }
94
95 this->impl_->sync([this]() {
96 if VLIKELY (value_.has_value()) {
97 write(value_.value());
98 }
99 });
100
101 return true;
102}
103
104template <typename ValueT, SecurityType SecT>
108
109template <typename ValueT, SecurityType SecT>
110inline void Setter<ValueT, SecT>::set(const ValueT& value) {
111#ifndef VLINK_DISABLE_PROFILER
112 CpuProfilerGuard profiler_guard(this->impl_->profiler.get());
113#endif
115 {
116 std::lock_guard lock(mtx_);
117 value_.emplace(value);
118 }
119
120 write(value);
121}
122
123template <typename ValueT, SecurityType SecT>
125 if VUNLIKELY (this->has_inited_) {
126 this->impl_->deinit_ext();
127 this->impl_->impl_type = kPublisher;
128 this->impl_->init_ext();
129 } else {
130 this->impl_->impl_type = kPublisher;
131 }
132}
133
134template <typename ValueT, SecurityType SecT>
135inline void Setter<ValueT, SecT>::write(const ValueT& value) {
136 if constexpr (std::is_same_v<ValueT, Bytes>) {
137 write_bytes(value);
138 } else {
139 Bytes msg_data;
140
141 if constexpr (SecT != SecurityType::kWithSecurity) {
142 if (this->is_support_loan_) {
143 size_t ser_size = Serializer::get_serialized_size<kValueType>(value);
144
145 msg_data = this->impl_->loan(ser_size);
146
147 if VUNLIKELY (ser_size != 0 && msg_data.empty()) {
148 return;
149 }
150 }
151 }
152
153 if VUNLIKELY (!Serializer::serialize<kValueType>(value, msg_data, this->impl_->transport_type)) {
154 VLOG_T("Setter serialize failed, url: ", this->impl_->url, ".");
155
156 if constexpr (SecT != SecurityType::kWithSecurity) {
157 if (this->is_support_loan_) {
158 this->impl_->return_loan(msg_data);
160 }
161
162 return;
163 }
164
165 write_bytes(msg_data);
166 }
167}
168
169template <typename ValueT, SecurityType SecT>
170inline void Setter<ValueT, SecT>::write_bytes(const Bytes& data) {
171 if constexpr (SecT == SecurityType::kWithSecurity) {
172 Bytes sec_data;
173
174 if VUNLIKELY (!this->security_->encrypt(data, sec_data)) {
175 VLOG_T("Setter encrypt failed, url: ", this->impl_->url, ".");
176 return;
177 }
178
179 this->impl_->write(sec_data);
180 } else {
181 this->impl_->try_record(ActionType::kSet, data);
182
183 this->impl_->write(data);
185}
186
187} // namespace vlink
RAII guard that automatically calls CpuProfiler::begin() and CpuProfiler::end().
Global singleton logger with three output styles and pluggable backends.
#define VLOG_F(...)
定义 logger.h:856
#define VLOG_T(...)
定义 logger.h:846
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
定义 macros.h:302
#define VLIKELY(...)
Shorthand alias for VLINK_LIKELY. Hints that the expression is likely true.
定义 macros.h:297
Compile-time type-detection and serialisation utilities for VLink messages.
Type-safe field-model writer for VLink topics.
URL-based transport configuration dispatcher for VLink nodes.