1use broadcast_common::seq::{MAX_SEQ_BITS, SeqSpace};
22
23const SEQ_SPACE_BITS: u32 = 31;
27const SEQ_SPACE: SeqSpace = broadcast_common::seq_space!(SEQ_SPACE_BITS);
29
30const _: () = assert!(SEQ_SPACE_BITS == MAX_SEQ_BITS);
31
32pub const MESSAGE_NUMBER_BITS: u32 = 26;
35pub const MESSAGE_NUMBER_MASK: u32 = (1 << MESSAGE_NUMBER_BITS) - 1;
37
38#[derive(Clone, Copy, PartialEq, Eq, Hash)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize))]
49#[cfg_attr(feature = "serde", serde(transparent))]
50pub struct SeqNo(u32);
51
52impl SeqNo {
53 pub const MAX: SeqNo = SeqNo(SEQ_SPACE.mask());
55
56 pub const fn try_new(value: u32) -> Result<Self, SeqOutOfRange> {
62 if value > SEQ_SPACE.mask() {
63 Err(SeqOutOfRange { value })
64 } else {
65 Ok(Self(value))
66 }
67 }
68
69 #[must_use]
75 pub const fn from_wire_masked(value: u32) -> Self {
76 Self(value & SEQ_SPACE.mask())
77 }
78
79 #[must_use]
81 pub const fn value(self) -> u32 {
82 self.0
83 }
84
85 #[must_use]
87 pub const fn add(self, n: u32) -> Self {
88 Self(SEQ_SPACE.add(self.0, n))
89 }
90
91 #[must_use]
93 pub const fn next(self) -> Self {
94 self.add(1)
95 }
96
97 #[must_use]
100 pub const fn diff(self, other: Self) -> i32 {
101 SEQ_SPACE.diff(self.0, other.0)
102 }
103
104 #[must_use]
106 pub const fn lt(self, other: Self) -> bool {
107 SEQ_SPACE.lt(self.0, other.0)
108 }
109
110 #[must_use]
112 pub const fn leq(self, other: Self) -> bool {
113 SEQ_SPACE.leq(self.0, other.0)
114 }
115
116 #[must_use]
118 pub const fn gt(self, other: Self) -> bool {
119 SEQ_SPACE.gt(self.0, other.0)
120 }
121
122 #[must_use]
124 pub const fn geq(self, other: Self) -> bool {
125 SEQ_SPACE.geq(self.0, other.0)
126 }
127
128 #[must_use]
132 pub const fn in_closed_range(self, first: Self, last: Self) -> bool {
133 SEQ_SPACE.in_closed_range(self.0, first.0, last.0)
134 }
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140#[non_exhaustive]
141pub struct SeqOutOfRange {
142 pub value: u32,
144}
145
146impl core::fmt::Display for SeqOutOfRange {
147 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
148 write!(
149 f,
150 "sequence number {:#010X} exceeds the 31-bit range",
151 self.value
152 )
153 }
154}
155
156impl core::error::Error for SeqOutOfRange {}
157
158impl TryFrom<u32> for SeqNo {
159 type Error = SeqOutOfRange;
160 fn try_from(value: u32) -> Result<Self, Self::Error> {
161 Self::try_new(value)
162 }
163}
164
165impl From<SeqNo> for u32 {
166 fn from(seq: SeqNo) -> Self {
167 seq.0
168 }
169}
170
171impl core::fmt::Debug for SeqNo {
172 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
173 write!(f, "SeqNo({:#010X})", self.0)
174 }
175}
176
177impl core::fmt::Display for SeqNo {
178 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
179 write!(f, "{}", self.0)
180 }
181}
182
183#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
191#[cfg_attr(feature = "serde", derive(serde::Serialize))]
192#[cfg_attr(feature = "serde", serde(transparent))]
193pub struct SocketId(u32);
194
195impl SocketId {
196 pub const CONNECTION_REQUEST: SocketId = SocketId(0);
199
200 #[must_use]
202 pub const fn from_wire(value: u32) -> Self {
203 Self(value)
204 }
205
206 #[must_use]
208 pub const fn value(self) -> u32 {
209 self.0
210 }
211
212 #[must_use]
214 pub const fn is_connection_request(self) -> bool {
215 self.0 == 0
216 }
217}
218
219impl From<u32> for SocketId {
220 fn from(value: u32) -> Self {
221 SocketId(value)
222 }
223}
224
225impl From<SocketId> for u32 {
226 fn from(id: SocketId) -> Self {
227 id.0
228 }
229}
230
231impl core::fmt::Debug for SocketId {
232 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
233 write!(f, "SocketId({})", self.0)
234 }
235}
236
237impl core::fmt::Display for SocketId {
238 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
239 write!(f, "{}", self.0)
240 }
241}
242
243#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
255#[cfg_attr(feature = "serde", derive(serde::Serialize))]
256#[cfg_attr(feature = "serde", serde(transparent))]
257pub struct AckNumber(u32);
258
259impl AckNumber {
260 pub const ZERO: AckNumber = AckNumber(0);
263
264 #[must_use]
266 pub const fn from_wire(value: u32) -> Self {
267 Self(value)
268 }
269
270 #[must_use]
272 pub const fn value(self) -> u32 {
273 self.0
274 }
275}
276
277impl From<u32> for AckNumber {
278 fn from(value: u32) -> Self {
279 AckNumber(value)
280 }
281}
282
283impl From<AckNumber> for u32 {
284 fn from(n: AckNumber) -> Self {
285 n.0
286 }
287}
288
289impl core::fmt::Debug for AckNumber {
290 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
291 write!(f, "AckNumber({})", self.0)
292 }
293}
294
295impl core::fmt::Display for AckNumber {
296 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
297 write!(f, "{}", self.0)
298 }
299}
300
301#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
309#[cfg_attr(feature = "serde", derive(serde::Serialize))]
310#[cfg_attr(feature = "serde", serde(transparent))]
311pub struct MessageNumber(u32);
312
313impl MessageNumber {
314 pub const fn try_new(value: u32) -> Result<Self, MessageNumberOutOfRange> {
320 if value > MESSAGE_NUMBER_MASK {
321 Err(MessageNumberOutOfRange { value })
322 } else {
323 Ok(Self(value))
324 }
325 }
326
327 #[must_use]
331 pub const fn from_wire_masked(value: u32) -> Self {
332 Self(value & MESSAGE_NUMBER_MASK)
333 }
334
335 #[must_use]
337 pub const fn value(self) -> u32 {
338 self.0
339 }
340
341 #[must_use]
344 pub const fn next(self) -> Self {
345 match ((self.0 & MESSAGE_NUMBER_MASK) + 1) & MESSAGE_NUMBER_MASK {
346 0 => Self(1),
347 next => Self(next),
348 }
349 }
350}
351
352#[derive(Debug, Clone, Copy, PartialEq, Eq)]
355#[non_exhaustive]
356pub struct MessageNumberOutOfRange {
357 pub value: u32,
359}
360
361impl core::fmt::Display for MessageNumberOutOfRange {
362 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
363 write!(
364 f,
365 "message number {:#010X} exceeds the 26-bit range",
366 self.value
367 )
368 }
369}
370
371impl core::error::Error for MessageNumberOutOfRange {}
372
373impl TryFrom<u32> for MessageNumber {
374 type Error = MessageNumberOutOfRange;
375 fn try_from(value: u32) -> Result<Self, Self::Error> {
376 Self::try_new(value)
377 }
378}
379
380impl From<MessageNumber> for u32 {
381 fn from(n: MessageNumber) -> Self {
382 n.0
383 }
384}
385
386impl core::fmt::Debug for MessageNumber {
387 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
388 write!(f, "MessageNumber({})", self.0)
389 }
390}
391
392impl core::fmt::Display for MessageNumber {
393 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
394 write!(f, "{}", self.0)
395 }
396}
397
398#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
409#[cfg_attr(feature = "serde", derive(serde::Serialize))]
410#[cfg_attr(feature = "serde", serde(transparent))]
411pub struct SrtTimestamp(u32);
412
413impl SrtTimestamp {
414 pub const ZERO: SrtTimestamp = SrtTimestamp(0);
416
417 #[must_use]
419 pub const fn from_wire(value: u32) -> Self {
420 Self(value)
421 }
422
423 #[must_use]
425 pub const fn value(self) -> u32 {
426 self.0
427 }
428}
429
430impl From<u32> for SrtTimestamp {
431 fn from(value: u32) -> Self {
432 SrtTimestamp(value)
433 }
434}
435
436impl From<SrtTimestamp> for u32 {
437 fn from(ts: SrtTimestamp) -> Self {
438 ts.0
439 }
440}
441
442impl core::fmt::Debug for SrtTimestamp {
443 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
444 write!(f, "SrtTimestamp({})", self.0)
445 }
446}
447
448impl core::fmt::Display for SrtTimestamp {
449 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
450 write!(f, "{}", self.0)
451 }
452}
453
454#[cfg(test)]
455mod tests {
456 use super::*;
457
458 #[test]
462 fn ack_number_keeps_every_32_bit_value() {
463 for v in [0, 1, 0x7FFF_FFFF, 0x8000_0000, 0xFFFF_FFFF] {
464 let n = AckNumber::from_wire(v);
465 assert_eq!(n.value(), v);
466 assert_eq!(u32::from(AckNumber::from(v)), v);
467 assert_eq!(alloc::format!("{n:?}"), alloc::format!("AckNumber({v})"));
468 assert_eq!(alloc::format!("{n}"), alloc::format!("{v}"));
469 }
470 assert_eq!(AckNumber::ZERO.value(), 0);
471 }
472
473 #[test]
474 fn seq_space_matches_the_wire_mask() {
475 assert_eq!(SEQ_SPACE.mask().count_ones(), SEQ_SPACE_BITS);
476 assert_eq!(SeqNo::MAX.value().count_ones(), 31);
477 assert_eq!(SeqNo::from_wire_masked(0x8000_0005).value(), 5);
478 }
479
480 #[test]
481 fn seq_try_new_rejects_an_out_of_range_value() {
482 assert_eq!(
483 SeqNo::try_new(0x8000_0000),
484 Err(SeqOutOfRange { value: 0x8000_0000 })
485 );
486 assert_eq!(
487 SeqNo::try_new(SeqNo::MAX.value()).map(SeqNo::value),
488 Ok(0x7FFF_FFFF)
489 );
490 }
491
492 #[test]
493 fn seq_wraps_and_orders_circularly() {
494 assert_eq!(SeqNo::MAX.next(), SeqNo::from_wire_masked(0));
495 assert!(SeqNo::MAX.lt(SeqNo::from_wire_masked(0)));
496 assert_eq!(SeqNo::from_wire_masked(0).diff(SeqNo::MAX), 1);
497 assert!(SeqNo::from_wire_masked(10).lt(SeqNo::from_wire_masked(20)));
498 assert!(SeqNo::from_wire_masked(20).gt(SeqNo::from_wire_masked(10)));
499 }
500
501 #[test]
502 fn seq_closed_range_walks_across_the_wrap() {
503 let lo = SeqNo::MAX.add(SEQ_SPACE.mask()); assert_eq!(lo.value(), constant_max_minus_one());
505 assert!(SeqNo::from_wire_masked(0).in_closed_range(lo, SeqNo::from_wire_masked(1)));
506 assert!(
507 SeqNo::from_wire_masked(SeqNo::MAX.value())
508 .in_closed_range(lo, SeqNo::from_wire_masked(1))
509 );
510 assert!(!SeqNo::from_wire_masked(2).in_closed_range(lo, SeqNo::from_wire_masked(1)));
511 }
512
513 fn constant_max_minus_one() -> u32 {
514 SeqNo::MAX.value() - 1
515 }
516
517 #[test]
518 fn socket_id_round_trips_and_names_the_connection_request() {
519 assert!(SocketId::CONNECTION_REQUEST.is_connection_request());
520 assert!(!SocketId::from_wire(7).is_connection_request());
521 assert_eq!(u32::from(SocketId::from_wire(42)), 42);
522 }
523
524 #[test]
525 fn message_number_wraps_to_one_and_rejects_overwide() {
526 assert_eq!(MessageNumber::from_wire_masked(0).next().value(), 1);
527 assert_eq!(
528 MessageNumber::from_wire_masked(MESSAGE_NUMBER_MASK)
529 .next()
530 .value(),
531 1
532 );
533 assert_eq!(
534 MessageNumber::try_new(1 << MESSAGE_NUMBER_BITS),
535 Err(MessageNumberOutOfRange {
536 value: 1 << MESSAGE_NUMBER_BITS
537 })
538 );
539 }
540
541 #[test]
542 fn message_number_masks_the_wire_word() {
543 assert_eq!(MessageNumber::from_wire_masked(0xFC00_0005).value(), 5);
544 }
545}