VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
server_impl.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/**
25 * @file server_impl.h
26 * @brief Abstract base class for all transport-specific server (RPC responder) implementations.
27 *
28 * @details
29 * @c ServerImpl is the intermediate layer between the generic @c Server<Req,Resp>
30 * template and a concrete transport backend (e.g. @c DdsServerImpl,
31 * @c ShmServerImpl). It inherits from @c NodeImpl and adds method-server semantics:
32 *
33 * - Request listening via @c listen(ReqRespCallback&&) -- the transport invokes the
34 * callback for each incoming RPC request, passing the request bytes and a unique
35 * @c req_id.
36 * - Asynchronous response dispatch via @c reply() -- the application calls this
37 * after processing the request.
38 * - Optional client-presence query via @c has_clients().
39 *
40 * @par Synchronous vs Asynchronous Responses
41 * The @c is_sync_type flag controls whether the server uses a synchronous reply
42 * path (reply is sent inside the callback, @c is_sync_type = true) or an asynchronous
43 * path where the server stores the request and calls @c reply() later. The base
44 * @c reply() implementation only warns when @c is_sync is @c false and always
45 * returns @c false; transports that support asynchronous replies override it.
46 */
47
48#pragma once
49
50#include "./node_impl.h"
51
52namespace vlink {
53
54/**
55 * @class ServerImpl
56 * @brief Transport-agnostic base for server (RPC responder) node implementations.
57 *
58 * @details
59 * Concrete backends override @c listen(ReqRespCallback&&) to register the request
60 * handler, and optionally override @c has_clients() and @c reply() for client
61 * discovery and asynchronous response support respectively.
62 */
64 public:
65 /**
66 * @brief Destructor.
67 */
68 ~ServerImpl() override;
69
70 /**
71 * @brief Registers the request handler callback.
72 *
73 * @details
74 * Must be implemented by each concrete transport backend. The callback is
75 * invoked for every incoming RPC request with the request bytes and a unique
76 * identifier. After registration @c is_listened is set to @c true by the
77 * @c Server<Req,Resp> layer.
78 *
79 * @param callback Callable @c void(uint64_t req_id, const Bytes& req_data, Bytes* resp_data)
80 * invoked for each incoming request. The handler writes
81 * its response into @c *resp_data; @c nullptr in fire-and-forget mode.
82 * @return @c true if registration succeeded; @c false on error.
83 */
84 virtual bool listen(ReqRespCallback&& callback) = 0;
85
86 /**
87 * @brief Returns @c true when at least one client is currently connected.
88 *
89 * @details
90 * The default implementation returns @c false. Transports that support client
91 * discovery (e.g. DDS matched publications) override this method.
92 *
93 * @return @c true if one or more clients are connected; @c false otherwise.
94 */
95 [[nodiscard]] virtual bool has_clients() const;
96
97 /**
98 * @brief Sends a response for a previously received request.
99 *
100 * @details
101 * Used in asynchronous server mode (@c is_sync_type = @c false) to deliver a
102 * response after the request callback has returned. @p req_id must match the
103 * identifier provided to the request callback; @p resp_data contains the
104 * serialised response payload.
105 *
106 * The default implementation logs a warning when @c is_sync is @c false and
107 * always returns @c false. Transports that support asynchronous replies
108 * override this method.
109 *
110 * @param req_id Unique identifier of the request to respond to.
111 * @param resp_data Serialised response payload.
112 * @param is_sync @c true if called synchronously inside the request callback;
113 * @c false for deferred (post-callback) replies.
114 * @return @c true if the response was queued or sent; @c false on error.
115 */
116 virtual bool reply(uint64_t req_id, const Bytes& resp_data, bool is_sync);
117
118 bool is_listened{false}; ///< @c true after @c listen() has been successfully called.
119 bool is_resp_type{false}; ///< @c true when the server sends a response (vs fire-and-forget).
120 bool is_sync_type{false}; ///< @c true when the reply is sent synchronously inside the callback.
121
122 protected:
123 /**
124 * @brief Protected constructor; initialises the server with @c kServer role.
125 */
127
128 private:
130};
131
132} // namespace vlink
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184
Abstract transport node base classes used by all VLink node implementations.