VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
calculate_sample.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 calculate_sample.h
26 * @brief Per-GUID cumulative sample loss tracker for VLink subscribers.
27 *
28 * @details
29 * @c CalculateSample maintains per-sender (GUID) sequence-number state to
30 * detect gaps in the message stream. It is used by DDS and other transports
31 * that carry monotonically-increasing sequence numbers with their messages.
32 *
33 * @par Algorithm
34 * For each GUID, the tracker records:
35 * - @c first -- the first sequence number seen from this sender.
36 * - @c expected -- the next sequence number expected.
37 * - @c lost -- cumulative count of skipped sequence numbers.
38 *
39 * On each @c update(seq, guid) call:
40 * - If @c expected == 0 or the gap looks like a reset, the state is
41 * re-initialised (no loss recorded for the first message).
42 * - Otherwise, @c lost is incremented by the difference
43 * @c (seq - expected), and @c expected is set to @c seq + 1.
44 *
45 * @par Thread Safety
46 * All public methods are thread-safe; @c update() uses an exclusive lock and
47 * @c get_total() / @c get_lost() use a shared lock.
48 *
49 * @note
50 * - A gap larger than @c UINT32_MAX between consecutive sequence numbers is
51 * treated as a counter reset rather than a loss event.
52 * - @c get_total() returns the sum over all GUIDs of
53 * @c (expected - first), i.e. the number of expected messages.
54 */
55
56#pragma once
57
58#include <cstdint>
59#include <shared_mutex>
60#include <unordered_map>
61
62#include "../base/macros.h"
63
64namespace vlink {
65
66/**
67 * @class CalculateSample
68 * @brief Thread-safe, per-GUID cumulative sample loss counter.
69 *
70 * @details
71 * Instantiated once per @c SubscriberImpl or @c GetterImpl that has
72 * latency/loss tracking enabled. The @c guid parameter allows a single
73 * subscriber to track messages from multiple publishers independently.
74 */
76 public:
77 /**
78 * @brief Default constructor.
79 */
80 CalculateSample() noexcept;
81
82 /**
83 * @brief Destructor.
84 */
85 ~CalculateSample() noexcept;
86
87 /**
88 * @brief Processes an incoming sequence number for the given sender.
89 *
90 * @details
91 * Detects gaps in the sequence and accumulates the loss count.
92 * Out-of-order or wrap-around sequences larger than @c UINT32_MAX are
93 * treated as resets and do not increment the loss counter.
94 *
95 * @param seq Sequence number of the received message.
96 * @param guid Sender identifier (GUID). Use @c 0 for single-sender streams.
97 */
98 void update(uint64_t seq, uint64_t guid = 0) noexcept;
99
100 /**
101 * @brief Returns the total number of expected samples across all senders.
102 *
103 * @details
104 * Computed as the sum of @c (expected - first) for every tracked GUID.
105 * This includes both successfully delivered and lost samples.
106 *
107 * @return Total expected sample count, or 0 if no data has been received.
108 */
109 [[nodiscard]] uint64_t get_total() const noexcept;
110
111 /**
112 * @brief Returns the cumulative number of lost samples across all senders.
113 *
114 * @details
115 * Accumulated from every gap detected since the last reset.
116 *
117 * @return Total lost sample count.
118 */
119 [[nodiscard]] uint64_t get_lost() const noexcept;
120
121 private:
122 struct Number final {
123 uint64_t first{0};
124 uint64_t expected{0};
125 uint64_t lost{0};
126 };
127
128 std::unordered_map<uint64_t, Number> number_map_;
129 mutable std::shared_mutex mtx_;
130
132};
133
134} // namespace vlink
Platform-independent macro definitions for the VLink library.
#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