roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
seq_num.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include "roo_logging.h"
6
7namespace roo_transport {
8namespace internal {
9
10// This class implements a sequence number that wraps around at 2^16.
11// It is used to track the sequence numbers of packets in a reliable
12// streaming protocol. The sequence number is represented as a 16-bit
13// unsigned integer, and the class provides various comparison and
14// arithmetic operations to facilitate the handling of sequence numbers
15// in a circular buffer context. The class also provides methods to
16// increment the sequence number and to convert it to its raw
17// representation.
18//
19// The usage relies on the fact that in the context of a reliable
20// streaming protocol, at any given time, the sequence numbers are expected to
21// be within a certain reception window, up to 2^10 in size (typically below
22// 2^8). Wrap-around is handled on this basis.
23//
24// In practice, the streaming protocol uses 12-bit sequence IDs, so we should in
25// fact wrap around at 2^12, rather than 2^16. However, we keep the 16-bit
26// representation for simplicity. (The ring buffer class has a helper method to
27// convert 12-bit sequence IDs to 16-bit, again on the basis of the fact that
28// the values are expected to be contained in a small range).
29class SeqNum {
30 public:
31 SeqNum(uint16_t seq) : seq_(seq) {}
32
33 bool operator==(SeqNum other) const { return seq_ == other.seq_; }
34
35 bool operator!=(SeqNum other) const { return seq_ != other.seq_; }
36
37 bool operator<(SeqNum other) const {
38 return (int16_t)(seq_ - other.seq_) < 0;
39 }
40
41 bool operator<=(SeqNum other) const {
42 return (int16_t)(seq_ - other.seq_) <= 0;
43 }
44
45 bool operator>(SeqNum other) const {
46 return (int16_t)(seq_ - other.seq_) > 0;
47 }
48
49 bool operator>=(SeqNum other) const {
50 return (int16_t)(seq_ - other.seq_) >= 0;
51 }
52
54 ++seq_;
55 return *this;
56 }
57
58 SeqNum operator++(int) { return SeqNum(seq_++); }
59
60 SeqNum& operator+=(int increment) {
61 seq_ += increment;
62 return *this;
63 }
64
65 int operator-(SeqNum other) const { return (int16_t)(seq_ - other.seq_); }
66
67 SeqNum operator+(int other) const { return SeqNum(seq_ + other); }
68 SeqNum operator-(int other) const { return SeqNum(seq_ - other); }
69
70 uint16_t raw() const { return seq_; }
71
72 private:
73 uint16_t seq_;
74};
75
76} // namespace internal
77} // namespace roo_transport
78
79namespace roo_logging {
80
81inline roo_logging::Stream& operator<<(roo_logging::Stream& os,
83 os << seq.raw();
84 return os;
85}
86
87}
bool operator==(SeqNum other) const
Definition seq_num.h:33
bool operator<(SeqNum other) const
Definition seq_num.h:37
int operator-(SeqNum other) const
Definition seq_num.h:65
SeqNum & operator+=(int increment)
Definition seq_num.h:60
bool operator!=(SeqNum other) const
Definition seq_num.h:35
bool operator>(SeqNum other) const
Definition seq_num.h:45
SeqNum operator+(int other) const
Definition seq_num.h:67
SeqNum operator-(int other) const
Definition seq_num.h:68
bool operator<=(SeqNum other) const
Definition seq_num.h:41
bool operator>=(SeqNum other) const
Definition seq_num.h:49