VLink  2.0.0
A high-performance communication middleware
subscriber_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 subscriber_impl.h
26  * @brief Transport-neutral base class for every event-model subscriber implementation.
27  *
28  * @details
29  * This is an internal implementation header used by the public @c Subscriber
30  * template; applications should depend on @c subscriber.h. @c SubscriberImpl
31  * extends @c NodeImpl with the receive-side semantics: two listen overloads
32  * cover the serialised wire path (the default) and the zero-copy in-process
33  * path used only by the intra backend, plus optional latency / loss tracking
34  * that mirrors @c GetterImpl.
35  *
36  * @par ImplType
37  * The constructor stamps @c impl_type with @c kSubscriber so subscriptions are
38  * tagged correctly in discovery, recording and proxy paths.
39  *
40  * @par Lifecycle
41  * - Constructed by the matching @c Conf::create_subscriber().
42  * - The public @c Subscriber template calls @c listen() once the user installs
43  * a callback; @c is_listened becomes @c true on success.
44  * - @c set_latency_and_lost_enabled() may be toggled before or after listen.
45  * - @c init() / @c deinit() inherited from @c NodeImpl drive the transport.
46  *
47  * @par Role table
48  * | Capability | Provider |
49  * | ------------------------------- | ---------------------------------------------------------------- |
50  * | Wire receive callback | Subclass override of @c listen(MsgCallback&&) |
51  * | Zero-copy in-process callback | @c IntraSubscriberImpl override of @c listen(IntraMsgCallback&&) |
52  * | Latency / loss reporting | Subclass overrides of the optional getters |
53  * | Listen state flag | @c is_listened (set by @c Subscriber) |
54  *
55  * @par Internal API contract
56  * | Method | Default | Subclass duty |
57  * | ------------------------------------- | ----------------------------- | ----------------------- |
58  * | @c listen(MsgCallback&&) | Pure virtual | Wire transport receiver |
59  * | @c listen(IntraMsgCallback&&) | Warns, returns @c false | Only intra:// overrides |
60  * | @c set_latency_and_lost_enabled(bool) | No-op | Toggle instrumentation |
61  * | @c is_latency_and_lost_enabled() const| Returns @c false | Report tracking state |
62  * | @c get_latency() const | Returns @c 0 | Latest measured latency |
63  * | @c get_lost() const | Returns zero @c SampleLostInfo| Cumulative loss stats |
64  */
65 
66 #pragma once
67 
68 #include "./node_impl.h"
69 
70 namespace vlink {
71 
72 /**
73  * @class SubscriberImpl
74  * @brief Event-model receive base shared by every transport-specific subscriber.
75  *
76  * @details
77  * Provides safe defaults for the optional latency / loss instrumentation so
78  * backends without those signals can be plugged in without empty overrides.
79  * Concrete backends override @c listen() to bind the supplied callback to the
80  * transport receive path.
81  */
83  public:
84  /**
85  * @brief Releases backend resources.
86  */
87  ~SubscriberImpl() override;
88 
89  /**
90  * @brief Installs the serialised-message receive callback.
91  *
92  * @details
93  * Pure virtual. Backends invoke @p callback with the raw payload bytes for
94  * each frame received. The public @c Subscriber sets @c is_listened to
95  * @c true on success.
96  *
97  * @param callback Callable @c void(const Bytes&) invoked on every received frame.
98  * @return @c true when registration succeeded; @c false on error.
99  */
100  virtual bool listen(MsgCallback&& callback) = 0;
101 
102  /**
103  * @brief Installs the zero-copy in-process receive callback.
104  *
105  * @details
106  * Only the @c intra:// backend overrides this overload; the default logs a
107  * warning and returns @c false.
108  *
109  * @param callback Callable @c void(const IntraData&) invoked for each in-process delivery.
110  * @return @c true when registration succeeded; @c false when the transport does not support
111  * @c IntraData.
112  */
113  virtual bool listen(IntraMsgCallback&& callback);
114 
115  /**
116  * @brief Enables or disables per-message latency / loss tracking.
117  *
118  * @details
119  * Default no-op. Backends that maintain timestamps and sequence numbers
120  * override the method.
121  *
122  * @param enable @c true to enable; @c false to disable.
123  */
124  virtual void set_latency_and_lost_enabled(bool enable);
125 
126  /**
127  * @brief Reports whether tracking is currently enabled.
128  *
129  * @return @c true when the backend is collecting latency / loss data.
130  */
131  [[nodiscard]] virtual bool is_latency_and_lost_enabled() const;
132 
133  /**
134  * @brief Returns the most recently measured end-to-end latency in nanoseconds.
135  *
136  * @details
137  * Only meaningful when @c is_latency_and_lost_enabled() returns @c true.
138  *
139  * @return Latency in nanoseconds; @c 0 when tracking is off or unsupported.
140  */
141  [[nodiscard]] virtual int64_t get_latency() const;
142 
143  /**
144  * @brief Returns cumulative delivered / lost counters.
145  *
146  * @details
147  * Only meaningful when tracking is enabled. Default returns a zero-initialised
148  * @c SampleLostInfo.
149  *
150  * @return @c SampleLostInfo with @c total and @c lost counts.
151  */
152  [[nodiscard]] virtual SampleLostInfo get_lost() const;
153 
154  bool is_listened{false}; ///< @c true once @c listen() has been registered successfully.
155 
156  protected:
157  /**
158  * @brief Stamps the node as @c kSubscriber.
159  */
161 
162  private:
164 };
165 
166 } // 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.