VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
client_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 client_impl.h
26 * @brief Abstract base class for all transport-specific client (RPC caller) implementations.
27 *
28 * @details
29 * @c ClientImpl is the intermediate layer between the generic @c Client<Req,Resp>
30 * template and a concrete transport backend (e.g. @c DdsClientImpl,
31 * @c ShmClientImpl). It inherits from @c NodeImpl and adds method-client semantics:
32 *
33 * - Server-connection detection and notification via @c detect_connected() /
34 * @c update_connected().
35 * - Blocking wait for server availability with @c wait_for_connected().
36 * - Synchronous (blocking) RPC call via @c call().
37 *
38 * @par Connection Detection Flow
39 * @code
40 * // Transport detects server appearance or disappearance:
41 * client_impl->update_connected();
42 * // -> compares is_connected() against cached state
43 * // -> notifies the condition variable
44 * // -> fires the ConnectCallback registered via detect_connected()
45 * @endcode
46 *
47 * @note Concrete subclasses must implement @c is_connected() and @c call().
48 */
49
50#pragma once
51
52#include <chrono>
53#include <memory>
54
55#include "./node_impl.h"
56
57namespace vlink {
58
59/**
60 * @class ClientImpl
61 * @brief Transport-agnostic base for client (RPC caller) node implementations.
62 *
63 * @details
64 * Provides the server-connection-detection infrastructure (condition variable +
65 * callback) used by @c Client<Req,Resp>::wait_for_connected() and
66 * @c Client<Req,Resp>::detect_connected(). Concrete backends override
67 * @c is_connected() to query the transport layer and call @c update_connected()
68 * whenever the server connection state changes.
69 */
71 public:
72 /**
73 * @brief Destructor.
74 */
75 ~ClientImpl() override;
76
77 /**
78 * @brief Interrupts the client, waking any blocked @c wait_for_connected() or
79 * @c call() operation.
80 *
81 * @details
82 * Calls @c NodeImpl::interrupt() to set the interrupted flag, then
83 * @c notify_all() on the internal condition variable so that any thread blocked
84 * in @c wait_for_connected() returns immediately. Concrete backends typically
85 * also forward the interrupt to any pending @c AckManager requests.
86 */
87 void interrupt() override;
88
89 /**
90 * @brief Registers a callback to be fired when the server connection state changes.
91 *
92 * @details
93 * The @p callback is stored and invoked with @c true when the server becomes
94 * reachable and @c false when it disconnects. If the server is already connected
95 * at registration time the callback is fired immediately with @c true before this
96 * function returns.
97 *
98 * @param callback Callable @c void(bool) to invoke on connection state change.
99 */
100 virtual void detect_connected(ConnectCallback&& callback);
101
102 /**
103 * @brief Blocks until the server is reachable or the timeout elapses.
104 *
105 * @details
106 * Returns immediately if @c is_connected() is already @c true. Otherwise waits
107 * on an internal condition variable that is notified by @c update_connected() and
108 * @c interrupt().
109 *
110 * - @p timeout < 0 (e.g. @c Timeout::kInfinite): waits indefinitely.
111 * - @p timeout >= 0: returns @c false if no server appears within the period.
112 *
113 * @param timeout Maximum time to wait; negative value means wait forever.
114 * @return @c true if the server was detected; @c false on timeout or
115 * interruption.
116 */
117 virtual bool wait_for_connected(std::chrono::milliseconds timeout);
118
119 /**
120 * @brief Returns @c true when the transport is connected to a server.
121 *
122 * @details
123 * Must be implemented by each concrete transport backend. Called by
124 * @c wait_for_connected() and @c update_connected() to determine whether the
125 * connection state has changed.
126 *
127 * @return @c true if a server is reachable; @c false otherwise.
128 */
129 [[nodiscard]] virtual bool is_connected() const = 0;
130
131 /**
132 * @brief Sends a request and blocks until the response arrives or the timeout elapses.
133 *
134 * @details
135 * Must be implemented by each concrete transport backend. @p req_data contains
136 * the serialised request payload. The @p callback is invoked with the response
137 * bytes once the round-trip completes.
138 *
139 * @param req_data Serialised request payload.
140 * @param callback Callable @c void(const Bytes&) invoked with the response bytes.
141 * @param timeout Maximum time to wait for the response; negative = infinite.
142 * @return @c true if a response was received within the timeout;
143 * @c false on timeout, interruption, or transport error.
144 */
145 virtual bool call(const Bytes& req_data, MsgCallback&& callback, std::chrono::milliseconds timeout) = 0;
146
147 /**
148 * @brief Notifies the connection-detection subsystem that connectivity may have changed.
149 *
150 * @details
151 * Called by the concrete transport backend whenever a server connects or disconnects.
152 * Compares the current @c is_connected() result against the cached state; if it
153 * differs, the condition variable is notified and the registered
154 * @c ConnectCallback is fired.
155 *
156 * @note This method is intended to be called from the transport's internal
157 * discovery thread, not from user code.
158 */
160
161 bool is_resp_type{false}; ///< @c true when the call expects a response (vs fire-and-forget).
162
163 protected:
164 /**
165 * @brief Protected constructor; initialises the client with @c kClient role.
166 */
168
169 private:
170 std::unique_ptr<struct ClientImplHelper> helper_;
171
173};
174
175} // namespace vlink
#define VLINK_EXPORT
定义 macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184
Abstract transport node base classes used by all VLink node implementations.