VLink  2.0.0
A high-performance communication middleware
server_impl.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 /**
25  * @file server_impl.h
26  * @brief Transport-neutral base class for every method-model server implementation.
27  *
28  * @details
29  * This is an internal implementation header used by the public @c Server
30  * template; applications should depend on @c server.h. @c ServerImpl extends
31  * @c NodeImpl with the responder-side bookkeeping: it tracks whether a
32  * @c listen() callback has been installed, whether a response is expected and
33  * whether the reply is delivered synchronously inside the request callback or
34  * asynchronously via @c reply().
35  *
36  * @par ImplType
37  * The constructor stamps @c impl_type with @c kServer, advertising the role to
38  * discovery and recording layers.
39  *
40  * @par Lifecycle
41  * - Construction stamps the impl type.
42  * - The public @c Server template calls @c listen() once the user installs a
43  * request handler; @c is_listened becomes @c true on success.
44  * - @c init() / @c deinit() inherited from @c NodeImpl bring the transport up.
45  * - Synchronous mode writes the response into the buffer supplied to the
46  * handler; asynchronous mode requires a later @c reply() call.
47  *
48  * @par Role table
49  * | Capability | Provider |
50  * | -------------------------------- | ------------------------------------------------------- |
51  * | Request callback registration | Subclass override of @c listen() |
52  * | Client presence query | Subclass override of @c has_clients() (optional) |
53  * | Deferred response delivery | Subclass override of @c reply() for async transports |
54  * | Sync / async behaviour flags | @c is_resp_type / @c is_sync_type (set by public layer) |
55  *
56  * @par Internal API contract
57  * | Method | Default | Subclass duty |
58  * | -------------------------------------- | ----------------------------- | ------------------------- |
59  * | @c listen(ReqRespCallback&&) | Pure virtual | Bind transport receiver |
60  * | @c has_clients() const | Returns @c false | Report client presence |
61  * | @c reply(req_id, bytes, is_sync) | Warns on async path | Implement deferred reply |
62  */
63 
64 #pragma once
65 
66 #include "./node_impl.h"
67 
68 namespace vlink {
69 
70 /**
71  * @class ServerImpl
72  * @brief Method-model server base shared by every transport implementation.
73  *
74  * @details
75  * Backends override @c listen() to bind the supplied callback to the transport
76  * receive path and, where supported, override @c has_clients() and @c reply()
77  * to expose client discovery and asynchronous responses respectively.
78  */
80  public:
81  /**
82  * @brief Releases backend resources.
83  */
84  ~ServerImpl() override;
85 
86  /**
87  * @brief Registers the request / response handler.
88  *
89  * @details
90  * Pure virtual. The callback is invoked for every incoming RPC with the
91  * request bytes and a unique @c req_id; the handler writes the response into
92  * @c *resp_data, or receives @c nullptr in fire-and-forget mode. The owning
93  * @c Server marks @c is_listened to @c true once this method succeeds.
94  *
95  * @param callback Callable @c void(uint64_t req_id, const Bytes& req_data, Bytes* resp_data).
96  * @return @c true on success; @c false on registration error.
97  */
98  virtual bool listen(ReqRespCallback&& callback) = 0;
99 
100  /**
101  * @brief Reports whether at least one client is currently connected.
102  *
103  * @details
104  * Default returns @c false. Backends that expose matched-publication
105  * discovery override the method.
106  *
107  * @return @c true when one or more clients are reachable.
108  */
109  [[nodiscard]] virtual bool has_clients() const;
110 
111  /**
112  * @brief Sends a response for a previously received request.
113  *
114  * @details
115  * Used in asynchronous server mode (@c is_sync_type == @c false) when the
116  * response is produced after the request callback returns. The base
117  * implementation warns when @c is_sync is @c false and always returns
118  * @c false; backends that support deferred replies override the method.
119  *
120  * @param req_id Identifier supplied to the request callback.
121  * @param resp_data Serialised response payload.
122  * @param is_sync @c true when called synchronously inside the request callback.
123  * @return @c true on success; @c false otherwise.
124  */
125  virtual bool reply(uint64_t req_id, const Bytes& resp_data, bool is_sync);
126 
127  bool is_listened{false}; ///< @c true once @c listen() has been registered successfully.
128  bool is_resp_type{false}; ///< @c true when the server is expected to produce a response.
129  bool is_sync_type{false}; ///< @c true when the reply is delivered inside the request callback.
130 
131  protected:
132  /**
133  * @brief Stamps the node as @c kServer.
134  */
136 
137  private:
139 };
140 
141 } // namespace vlink
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Foundational base classes shared by every transport-backed VLink node.