1use broadcast_common::modular::Modulus;
22
23const SEQ_SPACE_BITS: u32 = 31;
27const SEQ_SPACE: Modulus = Modulus::new::<SEQ_SPACE_BITS>();
29
30pub const MESSAGE_NUMBER_BITS: u32 = 26;
33pub const MESSAGE_NUMBER_MASK: u32 = (1 << MESSAGE_NUMBER_BITS) - 1;
35
36#[derive(Clone, Copy, PartialEq, Eq, Hash)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize))]
47#[cfg_attr(feature = "serde", serde(transparent))]
48pub struct SeqNo(u32);
49
50impl SeqNo {
51 pub const MAX: SeqNo = SeqNo(SEQ_SPACE.mask() as u32);
53
54 pub const fn try_new(value: u32) -> Result<Self, SeqOutOfRange> {
60 if value as u64 > SEQ_SPACE.mask() {
61 Err(SeqOutOfRange { value })
62 } else {
63 Ok(Self(value))
64 }
65 }
66
67 #[must_use]
73 pub const fn from_wire_masked(value: u32) -> Self {
74 Self(value & SEQ_SPACE.mask() as u32)
75 }
76
77 #[must_use]
79 pub const fn value(self) -> u32 {
80 self.0
81 }
82
83 #[must_use]
85 pub const fn add(self, n: u32) -> Self {
86 Self(SEQ_SPACE.add(self.0 as u64, n as u64) as u32)
87 }
88
89 #[must_use]
91 pub const fn next(self) -> Self {
92 self.add(1)
93 }
94
95 #[must_use]
98 pub const fn diff(self, other: Self) -> i32 {
99 SEQ_SPACE.signed(other.0 as u64, self.0 as u64) as i32
100 }
101
102 #[must_use]
104 pub const fn lt(self, other: Self) -> bool {
105 SEQ_SPACE.lt(self.0 as u64, other.0 as u64)
106 }
107
108 #[must_use]
110 pub const fn leq(self, other: Self) -> bool {
111 SEQ_SPACE.leq(self.0 as u64, other.0 as u64)
112 }
113
114 #[must_use]
116 pub const fn gt(self, other: Self) -> bool {
117 SEQ_SPACE.gt(self.0 as u64, other.0 as u64)
118 }
119
120 #[must_use]
122 pub const fn geq(self, other: Self) -> bool {
123 SEQ_SPACE.geq(self.0 as u64, other.0 as u64)
124 }
125
126 #[must_use]
130 pub const fn in_closed_range(self, first: Self, last: Self) -> bool {
131 SEQ_SPACE.in_closed_range(self.0 as u64, first.0 as u64, last.0 as u64)
132 }
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
138#[error("sequence number {value:#010X} exceeds the 31-bit range")]
139#[non_exhaustive]
140pub struct SeqOutOfRange {
141 pub value: u32,
143}
144
145impl TryFrom<u32> for SeqNo {
146 type Error = SeqOutOfRange;
147 fn try_from(value: u32) -> Result<Self, Self::Error> {
148 Self::try_new(value)
149 }
150}
151
152impl From<SeqNo> for u32 {
153 fn from(seq: SeqNo) -> Self {
154 seq.0
155 }
156}
157
158impl core::fmt::Debug for SeqNo {
159 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
160 write!(f, "SeqNo({:#010X})", self.0)
161 }
162}
163
164impl core::fmt::Display for SeqNo {
165 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
166 write!(f, "{}", self.0)
167 }
168}
169
170#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
178#[cfg_attr(feature = "serde", derive(serde::Serialize))]
179#[cfg_attr(feature = "serde", serde(transparent))]
180pub struct SocketId(u32);
181
182impl SocketId {
183 pub const CONNECTION_REQUEST: SocketId = SocketId(0);
186
187 #[must_use]
189 pub const fn from_wire(value: u32) -> Self {
190 Self(value)
191 }
192
193 #[must_use]
195 pub const fn value(self) -> u32 {
196 self.0
197 }
198
199 #[must_use]
201 pub const fn is_connection_request(self) -> bool {
202 self.0 == 0
203 }
204}
205
206impl From<u32> for SocketId {
207 fn from(value: u32) -> Self {
208 SocketId(value)
209 }
210}
211
212impl From<SocketId> for u32 {
213 fn from(id: SocketId) -> Self {
214 id.0
215 }
216}
217
218impl core::fmt::Debug for SocketId {
219 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
220 write!(f, "SocketId({})", self.0)
221 }
222}
223
224impl core::fmt::Display for SocketId {
225 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
226 write!(f, "{}", self.0)
227 }
228}
229
230#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
242#[cfg_attr(feature = "serde", derive(serde::Serialize))]
243#[cfg_attr(feature = "serde", serde(transparent))]
244pub struct AckNumber(u32);
245
246impl AckNumber {
247 pub const ZERO: AckNumber = AckNumber(0);
250
251 #[must_use]
253 pub const fn from_wire(value: u32) -> Self {
254 Self(value)
255 }
256
257 #[must_use]
259 pub const fn value(self) -> u32 {
260 self.0
261 }
262}
263
264impl From<u32> for AckNumber {
265 fn from(value: u32) -> Self {
266 AckNumber(value)
267 }
268}
269
270impl From<AckNumber> for u32 {
271 fn from(n: AckNumber) -> Self {
272 n.0
273 }
274}
275
276impl core::fmt::Debug for AckNumber {
277 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
278 write!(f, "AckNumber({})", self.0)
279 }
280}
281
282impl core::fmt::Display for AckNumber {
283 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
284 write!(f, "{}", self.0)
285 }
286}
287
288#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
296#[cfg_attr(feature = "serde", derive(serde::Serialize))]
297#[cfg_attr(feature = "serde", serde(transparent))]
298pub struct MessageNumber(u32);
299
300impl MessageNumber {
301 pub const fn try_new(value: u32) -> Result<Self, MessageNumberOutOfRange> {
307 if value > MESSAGE_NUMBER_MASK {
308 Err(MessageNumberOutOfRange { value })
309 } else {
310 Ok(Self(value))
311 }
312 }
313
314 #[must_use]
318 pub const fn from_wire_masked(value: u32) -> Self {
319 Self(value & MESSAGE_NUMBER_MASK)
320 }
321
322 #[must_use]
324 pub const fn value(self) -> u32 {
325 self.0
326 }
327
328 #[must_use]
331 pub const fn next(self) -> Self {
332 match ((self.0 & MESSAGE_NUMBER_MASK) + 1) & MESSAGE_NUMBER_MASK {
333 0 => Self(1),
334 next => Self(next),
335 }
336 }
337}
338
339#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
342#[error("message number {value:#010X} exceeds the 26-bit range")]
343#[non_exhaustive]
344pub struct MessageNumberOutOfRange {
345 pub value: u32,
347}
348
349impl TryFrom<u32> for MessageNumber {
350 type Error = MessageNumberOutOfRange;
351 fn try_from(value: u32) -> Result<Self, Self::Error> {
352 Self::try_new(value)
353 }
354}
355
356impl From<MessageNumber> for u32 {
357 fn from(n: MessageNumber) -> Self {
358 n.0
359 }
360}
361
362impl core::fmt::Debug for MessageNumber {
363 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
364 write!(f, "MessageNumber({})", self.0)
365 }
366}
367
368impl core::fmt::Display for MessageNumber {
369 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
370 write!(f, "{}", self.0)
371 }
372}
373
374#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
385#[cfg_attr(feature = "serde", derive(serde::Serialize))]
386#[cfg_attr(feature = "serde", serde(transparent))]
387pub struct SrtTimestamp(u32);
388
389impl SrtTimestamp {
390 pub const ZERO: SrtTimestamp = SrtTimestamp(0);
392
393 #[must_use]
395 pub const fn from_wire(value: u32) -> Self {
396 Self(value)
397 }
398
399 #[must_use]
401 pub const fn value(self) -> u32 {
402 self.0
403 }
404}
405
406impl From<u32> for SrtTimestamp {
407 fn from(value: u32) -> Self {
408 SrtTimestamp(value)
409 }
410}
411
412impl From<SrtTimestamp> for u32 {
413 fn from(ts: SrtTimestamp) -> Self {
414 ts.0
415 }
416}
417
418impl core::fmt::Debug for SrtTimestamp {
419 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
420 write!(f, "SrtTimestamp({})", self.0)
421 }
422}
423
424impl core::fmt::Display for SrtTimestamp {
425 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
426 write!(f, "{}", self.0)
427 }
428}
429
430#[cfg(test)]
431mod tests {
432 use super::*;
433
434 #[test]
438 fn ack_number_keeps_every_32_bit_value() {
439 for v in [0, 1, 0x7FFF_FFFF, 0x8000_0000, 0xFFFF_FFFF] {
440 let n = AckNumber::from_wire(v);
441 assert_eq!(n.value(), v);
442 assert_eq!(u32::from(AckNumber::from(v)), v);
443 assert_eq!(alloc::format!("{n:?}"), alloc::format!("AckNumber({v})"));
444 assert_eq!(alloc::format!("{n}"), alloc::format!("{v}"));
445 }
446 assert_eq!(AckNumber::ZERO.value(), 0);
447 }
448
449 #[test]
450 fn seq_space_matches_the_wire_mask() {
451 assert_eq!(SEQ_SPACE.mask().count_ones(), SEQ_SPACE_BITS);
452 assert_eq!(SeqNo::MAX.value().count_ones(), 31);
453 assert_eq!(SeqNo::from_wire_masked(0x8000_0005).value(), 5);
454 }
455
456 #[test]
457 fn seq_try_new_rejects_an_out_of_range_value() {
458 assert_eq!(
459 SeqNo::try_new(0x8000_0000),
460 Err(SeqOutOfRange { value: 0x8000_0000 })
461 );
462 assert_eq!(
463 SeqNo::try_new(SeqNo::MAX.value()).map(SeqNo::value),
464 Ok(0x7FFF_FFFF)
465 );
466 }
467
468 #[test]
469 fn seq_wraps_and_orders_circularly() {
470 assert_eq!(SeqNo::MAX.next(), SeqNo::from_wire_masked(0));
471 assert!(SeqNo::MAX.lt(SeqNo::from_wire_masked(0)));
472 assert_eq!(SeqNo::from_wire_masked(0).diff(SeqNo::MAX), 1);
473 assert!(SeqNo::from_wire_masked(10).lt(SeqNo::from_wire_masked(20)));
474 assert!(SeqNo::from_wire_masked(20).gt(SeqNo::from_wire_masked(10)));
475 }
476
477 #[test]
478 fn seq_closed_range_walks_across_the_wrap() {
479 let lo = SeqNo::MAX.add(SEQ_SPACE.mask() as u32); assert_eq!(lo.value(), constant_max_minus_one());
481 assert!(SeqNo::from_wire_masked(0).in_closed_range(lo, SeqNo::from_wire_masked(1)));
482 assert!(
483 SeqNo::from_wire_masked(SeqNo::MAX.value())
484 .in_closed_range(lo, SeqNo::from_wire_masked(1))
485 );
486 assert!(!SeqNo::from_wire_masked(2).in_closed_range(lo, SeqNo::from_wire_masked(1)));
487 }
488
489 fn constant_max_minus_one() -> u32 {
490 SeqNo::MAX.value() - 1
491 }
492
493 #[test]
494 fn socket_id_round_trips_and_names_the_connection_request() {
495 assert!(SocketId::CONNECTION_REQUEST.is_connection_request());
496 assert!(!SocketId::from_wire(7).is_connection_request());
497 assert_eq!(u32::from(SocketId::from_wire(42)), 42);
498 }
499
500 #[test]
501 fn message_number_wraps_to_one_and_rejects_overwide() {
502 assert_eq!(MessageNumber::from_wire_masked(0).next().value(), 1);
503 assert_eq!(
504 MessageNumber::from_wire_masked(MESSAGE_NUMBER_MASK)
505 .next()
506 .value(),
507 1
508 );
509 assert_eq!(
510 MessageNumber::try_new(1 << MESSAGE_NUMBER_BITS),
511 Err(MessageNumberOutOfRange {
512 value: 1 << MESSAGE_NUMBER_BITS
513 })
514 );
515 }
516
517 #[test]
518 fn message_number_masks_the_wire_word() {
519 assert_eq!(MessageNumber::from_wire_masked(0xFC00_0005).value(), 5);
520 }
521}