30 #include "../base/cpu_profiler_guard.h"
31 #include "../base/logger.h"
32 #include "../impl/url.h"
33 #include "../serializer.h"
34 #include "../server.h"
38 template <
typename ReqT,
typename RespT, SecurityType SecT>
40 const std::string& url_str,
InitType type) {
41 return std::make_unique<Server<ReqT, RespT, SecT>>(url_str, type);
44 template <
typename ReqT,
typename RespT, SecurityType SecT>
46 const std::string& url_str,
InitType type) {
47 return std::make_shared<Server<ReqT, RespT, SecT>>(url_str, type);
50 template <
typename ReqT,
typename RespT, SecurityType SecT>
51 template <
typename ConfT,
typename>
53 static_assert(ConfT::get_allow_impl_type() & kImplType,
"Conf does not support server mode.");
55 if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
56 VLOG_F(conf,
" server configuration is invalid or could not be parsed.");
60 this->impl_ = conf.create_server();
63 VLOG_F(conf,
" server implementation not available for this transport.");
67 this->impl_->transport_type = conf.get_transport_type();
68 this->impl_->ser_type = Serializer::get_serialized_type<kReqType, ReqT>();
70 if constexpr (kHasResp) {
71 const auto resp_ser_type = Serializer::get_serialized_type<kRespType, RespT>();
73 if (!this->impl_->ser_type.empty() || !resp_ser_type.empty()) {
74 this->impl_->ser_type +=
"|" + resp_ser_type;
79 constexpr
auto kReqSchemaType = Serializer::get_schema_type<kReqType, ReqT>();
80 constexpr
auto kRespSchemaType = Serializer::get_schema_type<kRespType, RespT>();
82 if constexpr (kHasResp && kReqSchemaType != kRespSchemaType) {
85 this->impl_->schema_type = kReqSchemaType;
89 this->impl_->is_cdr_type = Serializer::is_cdr_type<ReqT>();
90 this->impl_->is_resp_type = kHasResp;
91 if constexpr (kHasResp) {
92 this->impl_->is_resp_cdr_type = Serializer::is_cdr_type<RespT>();
95 if constexpr (std::is_same_v<ConfT, Url>) {
96 this->impl_->url = conf.get_str();
100 this->impl_->is_security_type =
true;
108 template <
typename ReqT,
typename RespT, SecurityType SecT>
110 :
Server<ReqT, RespT, SecT>(
Url(url_str), type) {}
112 template <
typename ReqT,
typename RespT, SecurityType SecT>
114 static_assert(!kHasResp,
"Reply not supported; use listen(ReqRespCallback&&) instead.");
116 this->impl_->is_sync_type =
true;
118 return listen_bytes([
this, callback = std::move(callback)](uint64_t,
const Bytes& req_data,
Bytes*) {
119 #ifndef VLINK_DISABLE_PROFILER
123 if constexpr (std::is_same_v<ReqT, Bytes>) {
128 auto req = this->
template get_default_value<ReqT>();
130 if VUNLIKELY (!Serializer::deserialize<kReqType>(req_data, req, this->impl_->transport_type)) {
131 VLOG_T(
"Server deserialize failed, url: ", this->impl_->url,
".");
140 template <
typename ReqT,
typename RespT, SecurityType SecT>
142 static_assert(kHasResp,
"Must have reply.");
144 this->impl_->is_sync_type =
true;
146 return listen_bytes([
this, callback = std::move(callback)](uint64_t req_id,
const Bytes& req_data,
Bytes* resp_data) {
147 #ifndef VLINK_DISABLE_PROFILER
152 VLOG_E(
"Server resp_data pointer is null.");
156 if constexpr (std::is_same_v<ReqT, Bytes> && std::is_same_v<RespT, Bytes>) {
159 callback(req_data, *resp_data);
161 reply_bytes<true>(req_id, *resp_data,
true, resp_data);
163 auto req = this->
template get_default_value<ReqT>();
164 auto resp = this->
template get_default_value<RespT>();
166 if constexpr (std::is_same_v<ReqT, Bytes>) {
167 req.shallow_copy(req_data);
168 }
else if VUNLIKELY (!Serializer::deserialize<kReqType>(req_data, req, this->impl_->transport_type)) {
169 VLOG_T(
"Server deserialize failed, url: ", this->impl_->url,
".");
176 if constexpr (std::is_same_v<RespT, Bytes>) {
177 if (resp.is_owner() || resp.is_loaned()) {
178 *resp_data = std::move(resp);
182 }
else if VUNLIKELY (!Serializer::serialize_to_transport<kRespType>(
183 resp, *resp_data, this->impl_->transport_type, use_loan,
184 [
this](
size_t size) { return this->impl_->loan(size); })) {
185 VLOG_T(
"Server serialize failed, url: ", this->impl_->url,
".");
188 if (this->is_support_loan_) {
189 this->impl_->return_loan(*resp_data);
196 reply_bytes<true>(req_id, *resp_data,
true, resp_data);
201 template <
typename ReqT,
typename RespT, SecurityType SecT>
203 static_assert(kHasResp,
"Must have reply.");
205 this->impl_->is_sync_type =
false;
207 return listen_bytes([
this, callback = std::move(callback)](uint64_t req_id,
const Bytes& req_data,
Bytes*) {
208 #ifndef VLINK_DISABLE_PROFILER
212 if constexpr (std::is_same_v<ReqT, Bytes>) {
215 callback(req_id, req_data);
217 auto req = this->
template get_default_value<ReqT>();
219 if VUNLIKELY (!Serializer::deserialize<kReqType>(req_data, req, this->impl_->transport_type)) {
220 VLOG_T(
"Server deserialize failed, url: ", this->impl_->url,
".");
224 callback(req_id, req);
229 template <
typename ReqT,
typename RespT, SecurityType SecT>
231 static_assert(kHasResp,
"Reply requires a response type.");
233 if VUNLIKELY (!this->impl_->is_listened) {
234 VLOG_F(
"Server::reply() requires listen() to be called first.");
238 if VUNLIKELY (this->impl_->is_sync_type) {
239 VLOG_F(
"Server::reply() is not available in synchronous listen mode.");
243 if constexpr (std::is_same_v<RespT, Bytes>) {
244 return reply_bytes<false>(req_id, resp,
false);
249 if VUNLIKELY (!Serializer::serialize_to_transport<kRespType>(
250 resp, resp_data, this->impl_->transport_type, use_loan,
251 [
this](
size_t size) { return this->impl_->loan(size); })) {
252 VLOG_T(
"Server serialize failed, url: ", this->impl_->url,
".");
255 if (this->is_support_loan_) {
256 this->impl_->return_loan(resp_data);
263 bool ret = reply_bytes<false>(req_id, resp_data,
false);
269 template <
typename ReqT,
typename RespT, SecurityType SecT>
270 inline bool Server<ReqT, RespT, SecT>::has_clients()
const {
271 return this->impl_->has_clients();
274 template <
typename ReqT,
typename RespT, SecurityType SecT>
276 if VUNLIKELY (!this->has_inited_.load(std::memory_order_acquire)) {
277 VLOG_F(
"Server::listen_bytes() called before init().");
281 if VUNLIKELY (this->impl_->is_listened) {
282 VLOG_F(
"Server has already been listened, url: ", this->impl_->url,
".");
286 bool ret = this->impl_->listen(
287 [
this, callback = std::move(callback)](uint64_t req_id,
const Bytes& req_data, Bytes* resp_data) {
291 if VUNLIKELY (!this->impl_->security || !this->impl_->security->decrypt(req_data, sec_req_data)) {
292 VLOG_T(
"Server decrypt failed, url: ", this->impl_->url,
".");
296 this->invoke_callback(callback, req_id, sec_req_data, resp_data);
300 this->invoke_callback(callback, req_id, req_data, resp_data);
304 this->impl_->is_listened = ret;
309 template <
typename ReqT,
typename RespT, SecurityType SecT>
310 template <
bool HasPtrT>
312 [[maybe_unused]]
Bytes* resp_data_ptr) {
313 if VUNLIKELY (!this->has_inited_.load(std::memory_order_acquire)) {
314 VLOG_F(
"Server::reply_bytes() called before init().");
320 if VUNLIKELY (!this->impl_->security || !this->impl_->security->encrypt(resp_data, sec_resp_data)) {
321 if constexpr (HasPtrT) {
322 resp_data_ptr->clear();
325 VLOG_T(
"Server encrypt failed, url: ", this->impl_->url,
".");
329 if constexpr (HasPtrT) {
330 *resp_data_ptr = std::move(sec_resp_data);
331 return this->impl_->reply(req_id, *resp_data_ptr, is_sync);
334 return this->impl_->reply(req_id, sec_resp_data, is_sync);
336 if constexpr (HasPtrT) {
337 *resp_data_ptr = resp_data;
342 return this->impl_->reply(req_id, resp_data, is_sync);
346 template <
typename ReqT,
typename RespT>
347 template <
typename SecurityConfigT>
349 const std::string& url_str, SecurityConfigT&& sec_cfg,
InitType type) {
350 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
351 "SecurityConfigT must be Security::Config.");
353 return std::make_unique<SecurityServer<ReqT, RespT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
356 template <
typename ReqT,
typename RespT>
357 template <
typename SecurityConfigT>
359 const std::string& url_str, SecurityConfigT&& sec_cfg,
InitType type) {
360 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
361 "SecurityConfigT must be Security::Config.");
363 return std::make_shared<SecurityServer<ReqT, RespT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
366 template <
typename ReqT,
typename RespT>
367 template <
typename ConfT,
typename SecurityConfigT,
typename>
370 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
371 "SecurityConfigT must be Security::Config.");
380 template <
typename ReqT,
typename RespT>
381 template <
typename SecurityConfigT>
384 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
385 "SecurityConfigT must be Security::Config.");
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
Scope guard that opens and closes a CpuProfiler active interval.
Definition: cpu_profiler_guard.h:80
Copyable type-erased callable analogue of std::function with a tunable SBO and pool spill.
Definition: functional.h:132
Function< void(uint64_t, const Bytes &, Bytes *)> ReqRespCallback
Request / response handler installed by ServerImpl::listen().
Definition: node_impl.h:193
bool enable_security(const Security::Config &cfg)
Installs a Security configuration before transport initialisation.
Definition: node-inl.h:331
virtual bool init()
Initialises the node and its transport back-end.
Definition: node-inl.h:40
std::shared_ptr< SecurityServer< ReqT, RespT > > SharedPtr
Owning shared-pointer alias.
Definition: server.h:272
std::unique_ptr< SecurityServer< ReqT, RespT > > UniquePtr
Owning unique-pointer alias.
Definition: server.h:271
static UniquePtr create_unique(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityServer and wraps it in a std::unique_ptr.
Definition: server-inl.h:348
static SharedPtr create_shared(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityServer and wraps it in a std::shared_ptr.
Definition: server-inl.h:358
SecurityServer(const ConfT &conf, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Constructs a SecurityServer from a typed configuration object.
Definition: server-inl.h:368
Type-safe RPC handler for the VLink method communication model.
Definition: server.h:126
bool reply(uint64_t req_id, const RespT &resp)
Emits the asynchronous response for a previously received request.
Definition: server-inl.h:230
static SharedPtr create_shared(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Server and wraps it in a std::shared_ptr.
Definition: server-inl.h:45
static UniquePtr create_unique(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Server and wraps it in a std::unique_ptr.
Definition: server-inl.h:39
Server(const ConfT &conf, InitType type=InitType::kWithInit)
Constructs a server from a typed transport configuration object.
Definition: server-inl.h:52
std::shared_ptr< Server< ReqT, RespT, SecT > > SharedPtr
Owning shared-pointer alias.
Definition: server.h:129
bool listen(ReqCallback &&callback)
Installs a fire-and-forget request handler.
Definition: server-inl.h:113
bool listen_for_reply(ReqAsyncRespCallback &&callback)
Installs a handler that defers the reply via reply().
Definition: server-inl.h:202
std::unique_ptr< Server< ReqT, RespT, SecT > > UniquePtr
Owning unique-pointer alias.
Definition: server.h:128
#define VLOG_E(...)
Definition: logger.h:904
#define VLOG_F(...)
Definition: logger.h:906
#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
InitType
Selects between immediate and deferred node initialisation.
Definition: types.h:150
@ kWithoutInit
Defer initialisation; call init() manually.
@ kWithInit
Initialise immediately in the constructor.
@ kUnknown
Decoding family unknown.
@ kServerRequest
RPC request observed by a Server node.
@ kServerResponse
RPC response emitted by a Server node.
SecurityType
Compile-time selector for the per-node message security variant.
Definition: types.h:168
@ kWithSecurity
Encrypted and authenticated transport.
Aggregate of every parameter accepted by the Security constructor.
Definition: security.h:164
Conf subclass that routes virtual calls to the transport selected by a URL string.
Definition: url.h:216