VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
subscriber_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 subscriber_impl.h
26 * @brief Abstract base class for all transport-specific subscriber implementations.
27 *
28 * @details
29 * @c SubscriberImpl is the intermediate layer between the generic @c Subscriber<T>
30 * template and a concrete transport backend (e.g. @c DdsSubscriberImpl,
31 * @c ShmSubscriberImpl). It inherits from @c NodeImpl and adds receive-side
32 * semantics:
33 *
34 * - Two listen overloads: a serialised @c MsgCallback for network transports and
35 * a zero-copy @c IntraMsgCallback for the @c intra:// backend.
36 * - Optional latency and sample-loss tracking via @c CalculateSample.
37 * - A public @c is_listened flag indicating whether a listener has been installed.
38 *
39 * @note Concrete subclasses must implement @c listen(MsgCallback&&).
40 * @c listen(IntraMsgCallback&&) is only overridden by @c IntraSubscriberImpl;
41 * all other transports inherit the base no-op that logs a warning and returns
42 * @c false.
43 */
44
45#pragma once
46
47#include "./node_impl.h"
48
49namespace vlink {
50
51/**
52 * @class SubscriberImpl
53 * @brief Transport-agnostic base for subscriber node implementations.
54 *
55 * @details
56 * Provides default (no-op) implementations for the optional latency/loss tracking
57 * interface. Concrete transport backends override @c listen(MsgCallback&&) to
58 * register their receive callback and, optionally, override the latency/loss
59 * methods if the transport provides that information.
60 */
62 public:
63 /**
64 * @brief Destructor.
65 */
66 ~SubscriberImpl() override;
67
68 /**
69 * @brief Registers the serialised-message receive callback.
70 *
71 * @details
72 * Must be implemented by each concrete transport backend. The callback is
73 * invoked with a @c Bytes buffer containing the raw serialised message each
74 * time a new message arrives. After registration @c is_listened is set to
75 * @c true by the @c Subscriber<T> layer.
76 *
77 * @param callback Callable @c void(const Bytes&) invoked on every received message.
78 * @return @c true if registration succeeded; @c false on error.
79 */
80 virtual bool listen(MsgCallback&& callback) = 0;
81
82 /**
83 * @brief Registers the zero-copy in-process receive callback.
84 *
85 * @details
86 * Used exclusively on the @c intra:// transport to receive @c IntraData directly
87 * from a co-located publisher without serialisation. The default implementation
88 * logs a warning and returns @c false; only @c IntraSubscriberImpl overrides this.
89 *
90 * @param callback Callable @c void(const IntraData&) invoked on every received
91 * in-process message.
92 * @return @c true if registration succeeded; @c false if this transport
93 * does not support @c IntraData.
94 */
95 virtual bool listen(IntraMsgCallback&& callback);
96
97 /**
98 * @brief Enables or disables per-message latency and sample-loss tracking.
99 *
100 * @details
101 * When enabled, the transport backend tracks message timestamps (for latency)
102 * and sequence numbers (for loss detection). The default implementation is a
103 * no-op; transports that support tracking override this method.
104 *
105 * @param enable @c true to enable tracking; @c false to disable.
106 */
107 virtual void set_latency_and_lost_enabled(bool enable);
108
109 /**
110 * @brief Returns whether latency and sample-loss tracking is currently enabled.
111 *
112 * @details
113 * The default implementation always returns @c false. Transports that support
114 * tracking override this to reflect the current enabled state.
115 *
116 * @return @c true if tracking is active; @c false otherwise.
117 */
118 [[nodiscard]] virtual bool is_latency_and_lost_enabled() const;
119
120 /**
121 * @brief Returns the most recently measured end-to-end message latency in microseconds.
122 *
123 * @details
124 * Only meaningful when @c is_latency_and_lost_enabled() returns @c true. The
125 * default implementation returns @c 0. Transports that support latency measurement
126 * override this to return the measured value.
127 *
128 * @return Latency in microseconds, or @c 0 if tracking is disabled or unsupported.
129 */
130 [[nodiscard]] virtual int64_t get_latency() const;
131
132 /**
133 * @brief Returns cumulative sample delivery statistics.
134 *
135 * @details
136 * Returns the total number of expected samples and the number that were lost due
137 * to queue overflow or network gaps. Only meaningful when
138 * @c is_latency_and_lost_enabled() returns @c true. The default implementation
139 * returns a zero-initialised @c SampleLostInfo.
140 *
141 * @return @c SampleLostInfo containing @c total and @c lost counts.
142 */
143 [[nodiscard]] virtual SampleLostInfo get_lost() const;
144
145 bool is_listened{false}; ///< @c true after @c listen() has been successfully called.
146
147 protected:
148 /**
149 * @brief Protected constructor; initialises the subscriber with @c kSubscriber role.
150 */
152
153 private:
155};
156
157} // 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.