srt-runtime 0.6.0

SRT (Secure Reliable Transport, draft-sharabayko-srt-01) packet codecs + sans-IO HSv5 Caller-Listener and Rendezvous handshake state machines, an ARQ reliability engine, a TSBPD delivery scheduler, LiveCC packet pacing, FileCC window-based congestion control, optional (crypto feature) payload encryption, and an optional (tokio feature) async UDP socket adapter; no_std core.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! ARQ sender-side reliability state — `draft-sharabayko-srt-01` §4.8
//! (Acknowledgement and Lost Packet Handling), §4.8.1 (ACKs/ACKACKs), §4.8.2
//! (NAKs), §4.10 (RTT). Curated rules: `specs/rules/srt-arq.md`.
//!
//! Sans-IO: [`Sender`] never reads a wall clock. [`Sender::on_data`] buffers
//! a freshly-submitted data packet (rule 1) and returns the wire bytes to
//! send; [`Sender::on_nak`] records the reported loss list for prioritized
//! retransmission (rules 5, 15, 16, 18); [`Sender::tick`] drains the pending
//! retransmit queue; [`Sender::on_ack`] frees acknowledged packets (rules 7,
//! 8, 16, 17) and, for a Full ACK, updates RTT/RTTVar (rule 33) and returns
//! the ACKACK reply (rules 3, 9).
//!
//! # Priority note (rules 5, 15, 16)
//! This sans-IO engine has no internal scheduler: [`Sender::on_data`] sends
//! its packet immediately rather than queuing it behind pending
//! retransmissions. A caller reproduces the spec's "loss list before first
//! transmission" priority by calling [`Sender::tick`] (which drains only the
//! retransmit queue) before submitting new application data each round.
//!
//! # Non-goals
//! Send-queue overflow / unsent-packet drop (rules 19-20) and RTO-based
//! periodic retransmission without a NAK (§5, FileCC) are out of scope — see
//! the `arq` module doc.

use alloc::collections::{BTreeSet, VecDeque};
use alloc::vec::Vec;
use core::time::Duration;

use crate::error::{Error, Result};
use crate::packet::data::MESSAGE_NUMBER_MASK;
use crate::packet::{
    AckAckPacket, AckCif, AckPacket, ControlPacket, DataPacket, EncryptionKeyField, LossListEntry,
    NakPacket, PacketPosition, SEQ_NUMBER_MASK,
};

use super::rtt::RttEstimator;
use super::{duration_to_wire_us, seq};

/// A NAK loss-list range is a compact wire encoding (Appendix A), but
/// nothing stops a malformed/adversarial range from declaring billions of
/// entries. [`Sender::on_nak`] never expands a range: it intersects it with
/// the (small) send buffer, so the work per range is bounded by what is
/// actually buffered. This is not a `specs/rules/srt-arq.md` rule — it is a
/// further safety cap on how many buffered packets one NAK datagram may mark
/// for retransmission, bounding the work a single (possibly spoofed) NAK can
/// cause.
const MAX_NAK_SEQS_PER_PACKET: usize = 1 << 16;

/// One buffered, sent-but-not-yet-acknowledged data packet (rules 1, 16,
/// 18).
#[derive(Debug, Clone)]
struct SentPacket {
    seq: u32,
    message_number: u32,
    payload: Vec<u8>,
    /// Resend counter (rule 18): incremented on every retransmission.
    resend_count: u32,
}

/// ARQ sender-side state (`draft-sharabayko-srt-01` §4.8). See the module
/// doc for the sans-IO contract.
#[derive(Debug)]
pub struct Sender {
    dest_socket_id: u32,
    /// Send buffer of unacknowledged packets, oldest first (rule 1).
    buffer: VecDeque<SentPacket>,
    /// Sequence numbers the receiver has reported lost (via NAK), pending
    /// retransmission (rules 16, 18).
    pending_retransmit: BTreeSet<u32>,
    rtt: RttEstimator,
    /// Work counter for the buffer lookups (one increment per buffer entry
    /// probed or visited while resolving a NAK range or a retransmit) — the
    /// deterministic evidence that a NAK costs `O(log buffer)` per entry plus
    /// the buffered packets it really names, never `O(buffer × range)`.
    lookup_steps: u64,
}

impl Sender {
    /// A fresh sender addressing `dest_socket_id` (the peer's SRT Socket ID,
    /// carried in every packet header, §3).
    pub fn new(dest_socket_id: u32) -> Self {
        Sender {
            dest_socket_id,
            buffer: VecDeque::new(),
            pending_retransmit: BTreeSet::new(),
            rtt: RttEstimator::new(),
            lookup_steps: 0,
        }
    }

    /// Index of the buffered packet with sequence number `seq`, by binary
    /// search over the send buffer (which is held in strictly increasing
    /// circular sequence order — [`Self::on_data`]'s contract).
    fn index_of(&mut self, seq: u32) -> Option<usize> {
        let (mut lo, mut hi) = (0usize, self.buffer.len());
        while lo < hi {
            self.lookup_steps = self.lookup_steps.saturating_add(1);
            let mid = lo + (hi - lo) / 2;
            let d = seq::seq_diff(self.buffer[mid].seq, seq);
            match d.cmp(&0) {
                core::cmp::Ordering::Equal => return Some(mid),
                core::cmp::Ordering::Less => lo = mid + 1,
                core::cmp::Ordering::Greater => hi = mid,
            }
        }
        None
    }

    /// Index of the first buffered packet at or after `seq` (circular
    /// order), `buffer.len()` if there is none.
    fn first_at_or_after(&mut self, seq: u32) -> usize {
        let (mut lo, mut hi) = (0usize, self.buffer.len());
        while lo < hi {
            self.lookup_steps = self.lookup_steps.saturating_add(1);
            let mid = lo + (hi - lo) / 2;
            if seq::seq_diff(self.buffer[mid].seq, seq) < 0 {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        lo
    }

    /// The current RTT estimate (rule 33 — updated from each Full ACK's
    /// carried value).
    pub fn rtt(&self) -> Duration {
        self.rtt.rtt()
    }

    /// The current RTTVar estimate.
    pub fn rtt_var(&self) -> Duration {
        self.rtt.rtt_var()
    }

    /// Number of packets still buffered, unacknowledged.
    pub fn buffered_count(&self) -> usize {
        self.buffer.len()
    }

    /// Number of sequence numbers currently pending retransmission.
    pub fn pending_retransmit_count(&self) -> usize {
        self.pending_retransmit.len()
    }

    /// Submit a new data packet for first transmission (rule 1: the sender
    /// buffers every sent packet to enable retransmission). Returns the
    /// wire bytes to send now — see the module doc's priority note.
    ///
    /// `seq` must increase (circularly) from one call to the next — the send
    /// buffer is searched by binary search, and a debug build asserts it.
    ///
    /// # Errors
    /// [`Error::FieldTooWide`] if `seq` does not fit its 31-bit field or
    /// `message_number` its 26-bit one; nothing is buffered then.
    pub fn on_data(
        &mut self,
        seq: u32,
        message_number: u32,
        payload: &[u8],
        now: Duration,
    ) -> Result<Vec<u8>> {
        // The wire fields are 31 and 26 bits wide (§3.1): a wider value is a
        // caller bug, reported rather than silently wrapped into another
        // packet's number.
        if seq > SEQ_NUMBER_MASK {
            return Err(Error::FieldTooWide {
                what: "Packet Sequence Number",
                value: u64::from(seq),
                bits: 31,
            });
        }
        if message_number > MESSAGE_NUMBER_MASK {
            return Err(Error::FieldTooWide {
                what: "Message Number",
                value: u64::from(message_number),
                bits: 26,
            });
        }
        debug_assert!(
            self.buffer
                .back()
                .is_none_or(|last| seq::seq_lt(last.seq, seq)),
            "Sender::on_data requires circularly increasing sequence numbers"
        );
        let pkt = DataPacket {
            seq_number: seq,
            position: PacketPosition::Solo,
            in_order: true,
            key_flag: EncryptionKeyField::NotEncrypted,
            retransmitted: false,
            message_number,
            timestamp: duration_to_wire_us(now),
            dest_socket_id: self.dest_socket_id,
            data: payload,
        };
        let mut buf = alloc::vec![0u8; pkt.serialized_len()];
        pkt.serialize_into(&mut buf)?;
        self.buffer.push_back(SentPacket {
            seq,
            message_number,
            payload: payload.to_vec(),
            resend_count: 0,
        });
        Ok(buf)
    }

    /// Record a NAK's loss-list entries for prioritized retransmission
    /// (`specs/rules/srt-arq.md` rules 5, 15, 16, 18). Entries no longer in
    /// the send buffer (already freed by a since-received ACK) are silently
    /// ignored (rule 17).
    ///
    /// Each entry is resolved against the send buffer by binary search, and a
    /// range is intersected with the buffered window rather than expanded, so
    /// the work is bounded by the buffer and by `MAX_NAK_SEQS_PER_PACKET`
    /// no matter what range a (possibly spoofed) NAK declares. A range whose
    /// end precedes its start is malformed and ignored.
    pub fn on_nak(&mut self, nak: &NakPacket<'_>) {
        let mut budget = MAX_NAK_SEQS_PER_PACKET;
        for entry in nak.entries() {
            if budget == 0 {
                break;
            }
            let Ok(entry) = entry else { continue };
            match entry {
                LossListEntry::Single(s) => {
                    if self.index_of(s).is_some() {
                        self.pending_retransmit.insert(s);
                    }
                    budget -= 1;
                }
                LossListEntry::Range(first, last) => {
                    // A range whose `last` precedes `first` (malformed) yields
                    // nothing: its first candidate is already past `last`.
                    let mut i = self.first_at_or_after(first);
                    while budget > 0 && i < self.buffer.len() {
                        self.lookup_steps = self.lookup_steps.saturating_add(1);
                        let s = self.buffer[i].seq;
                        if seq::seq_diff(s, last) > 0 {
                            break;
                        }
                        self.pending_retransmit.insert(s);
                        budget -= 1;
                        i += 1;
                    }
                }
            }
        }
    }

    /// Drain the pending retransmit queue, returning the wire bytes of each
    /// retransmission (rules 16, 18 — the `R` flag is set, the resend
    /// counter incremented). A queued sequence number no longer in the
    /// buffer (freed by a since-received ACK) is dropped without emitting
    /// anything (rule 17).
    pub fn tick(&mut self, now: Duration) -> Vec<Vec<u8>> {
        let mut seqs: Vec<u32> = core::mem::take(&mut self.pending_retransmit)
            .into_iter()
            .collect();
        // Oldest first in *circular* order: the set is ordered by raw value,
        // which interleaves the two sides of a sequence-number wrap.
        if let Some(front) = self.buffer.front() {
            let base = front.seq;
            seqs.sort_unstable_by_key(|&s| seq::seq_diff(s, base));
        }
        let mut out = Vec::with_capacity(seqs.len());
        for seq in seqs {
            let Some(idx) = self.index_of(seq) else {
                continue; // rule 17: already dropped from the buffer.
            };
            let sent = &mut self.buffer[idx];
            sent.resend_count = sent.resend_count.saturating_add(1);
            let pkt = DataPacket {
                seq_number: sent.seq,
                position: PacketPosition::Solo,
                in_order: true,
                key_flag: EncryptionKeyField::NotEncrypted,
                retransmitted: true,
                message_number: sent.message_number,
                timestamp: duration_to_wire_us(now),
                dest_socket_id: self.dest_socket_id,
                data: &sent.payload,
            };
            let mut buf = alloc::vec![0u8; pkt.serialized_len()];
            // Built from an already-buffered (so in-range) packet: cannot fail,
            // but a failure drops this retransmit rather than panicking.
            if pkt.serialize_into(&mut buf).is_ok() {
                out.push(buf);
            }
        }
        out
    }

    /// Process an incoming ACK: free every acknowledged packet (rules 7, 8,
    /// 16, 17), and — for a Full ACK only — update RTT/RTTVar (rule 33) and
    /// return the ACKACK reply (rules 3, 9).
    pub fn on_ack(&mut self, ack: &AckPacket, now: Duration) -> Option<Vec<u8>> {
        let last_ack_seq = match ack.cif {
            AckCif::Full { last_ack_seq, .. }
            | AckCif::Small { last_ack_seq, .. }
            | AckCif::Light { last_ack_seq } => last_ack_seq,
        };
        // rule 8: every seq strictly before `last_ack_seq` is acknowledged.
        while self
            .buffer
            .front()
            .is_some_and(|front| seq::seq_lt(front.seq, last_ack_seq))
        {
            if let Some(freed) = self.buffer.pop_front() {
                self.pending_retransmit.remove(&freed.seq); // rule 17
            }
        }
        // rule 17: nothing more to purge — `on_nak` only ever queues a seq
        // that is in the buffer, and the loop above removed each freed seq
        // from the queue, so the queue is still a subset of the buffer
        // (no per-ACK rebuild of the whole buffer's seq set).

        if let AckCif::Full { rtt_us, .. } = ack.cif {
            // rule 33: same EWMA as rules 29-30, `rtt` = the ACK's carried
            // value.
            self.rtt.update(Duration::from_micros(u64::from(rtt_us)));

            let pkt = ControlPacket::AckAck(AckAckPacket {
                ack_number: ack.ack_number,
                timestamp: duration_to_wire_us(now),
                dest_socket_id: self.dest_socket_id,
                libsrt_pad: true,
            });
            let mut buf = alloc::vec![0u8; pkt.serialized_len()];
            pkt.serialize_into(&mut buf).ok()?;
            Some(buf)
        } else {
            // rule 12: a Light ACK does not trigger an ACKACK. A Small
            // ACK's ack_number is likewise "should be set to 0" (§3.2.4)
            // and is not part of the numbered ACK/ACKACK exchange (rule
            // 24) — srt-arq.md does not state this explicitly for Small
            // ACK, resolved the same way as Light for consistency with
            // that wire convention.
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::packet::nak::build_loss_list;

    const PEER: u32 = 0xAAAA;

    fn nak_bytes(entries: &[LossListEntry]) -> Vec<u8> {
        let raw = build_loss_list(entries).unwrap();
        let pkt = ControlPacket::Nak(NakPacket {
            timestamp: 0,
            dest_socket_id: PEER,
            raw_loss_list: &raw,
        });
        let mut buf = alloc::vec![0u8; pkt.serialized_len()];
        pkt.serialize_into(&mut buf).unwrap();
        buf
    }

    #[test]
    fn on_data_buffers_and_returns_wire_bytes() {
        let mut s = Sender::new(PEER);
        let bytes = s.on_data(5, 5, b"hello", Duration::from_millis(1)).unwrap();
        assert_eq!(s.buffered_count(), 1);
        let dp = DataPacket::parse(&bytes).unwrap();
        assert_eq!(dp.seq_number, 5);
        assert!(!dp.retransmitted);
        assert_eq!(dp.data, b"hello");
    }

    #[test]
    fn nak_then_tick_retransmits_with_r_flag_set() {
        let mut s = Sender::new(PEER);
        s.on_data(0, 0, b"a", Duration::ZERO).unwrap();
        s.on_data(1, 1, b"b", Duration::ZERO).unwrap();

        let raw = nak_bytes(&[LossListEntry::Single(1)]);
        let ControlPacket::Nak(nak) = ControlPacket::parse(&raw).unwrap() else {
            panic!("expected NAK");
        };
        s.on_nak(&nak);
        assert_eq!(s.pending_retransmit_count(), 1);

        let out = s.tick(Duration::from_millis(5));
        assert_eq!(out.len(), 1);
        let dp = DataPacket::parse(&out[0]).unwrap();
        assert_eq!(dp.seq_number, 1);
        assert!(dp.retransmitted);
        assert_eq!(s.pending_retransmit_count(), 0);
    }

    #[test]
    fn nak_for_unbuffered_seq_is_ignored() {
        let mut s = Sender::new(PEER);
        s.on_data(0, 0, b"a", Duration::ZERO).unwrap();
        let raw = nak_bytes(&[LossListEntry::Single(99)]);
        let ControlPacket::Nak(nak) = ControlPacket::parse(&raw).unwrap() else {
            panic!("expected NAK");
        };
        s.on_nak(&nak);
        assert_eq!(s.pending_retransmit_count(), 0);
        assert!(s.tick(Duration::ZERO).is_empty());
    }

    #[test]
    fn full_ack_frees_buffer_and_updates_rtt_and_replies_ackack() {
        let mut s = Sender::new(PEER);
        s.on_data(0, 0, b"a", Duration::ZERO).unwrap();
        s.on_data(1, 1, b"b", Duration::ZERO).unwrap();
        s.on_data(2, 2, b"c", Duration::ZERO).unwrap();

        let ack = AckPacket {
            ack_number: 1,
            timestamp: 0,
            dest_socket_id: PEER,
            cif: AckCif::Full {
                last_ack_seq: 2,
                rtt_us: 20_000,
                rtt_var_us: 5_000,
                avail_buf_size: 0,
                pkt_recv_rate: 0,
                est_link_capacity: 0,
                recv_rate_bps: 0,
            },
        };
        let reply = s.on_ack(&ack, Duration::from_millis(1)).unwrap();
        assert_eq!(s.buffered_count(), 1); // seq 2 remains (not < last_ack_seq)
        assert!(s.rtt() < Duration::from_millis(100)); // moved from the 100ms init toward 20ms

        let ControlPacket::AckAck(ackack) = ControlPacket::parse(&reply).unwrap() else {
            panic!("expected ACKACK");
        };
        assert_eq!(ackack.ack_number, 1);
    }

    #[test]
    fn light_ack_does_not_trigger_ackack() {
        let mut s = Sender::new(PEER);
        s.on_data(0, 0, b"a", Duration::ZERO).unwrap();
        let ack = AckPacket {
            ack_number: 0,
            timestamp: 0,
            dest_socket_id: PEER,
            cif: AckCif::Light { last_ack_seq: 1 },
        };
        assert!(s.on_ack(&ack, Duration::ZERO).is_none());
        assert_eq!(s.buffered_count(), 0);
    }

    fn nak_for(entries: &[LossListEntry]) -> Vec<u8> {
        nak_bytes(entries)
    }

    fn feed_nak(s: &mut Sender, entries: &[LossListEntry]) {
        let raw = nak_for(entries);
        let ControlPacket::Nak(nak) = ControlPacket::parse(&raw).unwrap() else {
            panic!("expected NAK");
        };
        s.on_nak(&nak);
    }

    /// r08-SRT-W1: a NAK of ~180 maximal range entries against a 10 000-packet
    /// buffer used to cost `180 × 65 537 × buffer` comparisons. The lookup
    /// work counter (incremented on every buffer entry probed or visited)
    /// must stay within the per-NAK cap plus a logarithmic probe per entry.
    #[test]
    fn nak_with_huge_ranges_does_bounded_buffer_work() {
        const BUFFERED: u32 = 10_000;
        const RANGES: usize = 180;
        let mut s = Sender::new(PEER);
        for q in 0..BUFFERED {
            s.on_data(q, q, b"x", Duration::ZERO).unwrap();
        }
        // 180 entries each claiming [5 000, 0x3FFF_FFFF]; 180 x 8 B = 1 440 B
        // fits one datagram.
        let entries = alloc::vec![LossListEntry::Range(5_000, 0x3FFF_FFFF); RANGES];
        feed_nak(&mut s, &entries);
        assert_eq!(s.pending_retransmit_count(), (BUFFERED - 5_000) as usize);
        // log2(10 000) < 14 probes to find each range start.
        let bound = (MAX_NAK_SEQS_PER_PACKET + RANGES * 14) as u64;
        assert!(
            s.lookup_steps <= bound,
            "lookup work {} exceeds the bound {bound}",
            s.lookup_steps
        );
    }

    /// r08-SRT-W1: a single NAK entry costs a logarithmic lookup, not a scan.
    #[test]
    fn single_nak_entry_lookup_is_logarithmic() {
        let mut s = Sender::new(PEER);
        for q in 0..8192u32 {
            s.on_data(q, q, b"x", Duration::ZERO).unwrap();
        }
        feed_nak(&mut s, &[LossListEntry::Single(8000)]);
        assert_eq!(s.pending_retransmit_count(), 1);
        assert!(
            s.lookup_steps <= 14,
            "log2(8192) = 13 probes, got {}",
            s.lookup_steps
        );
        // And the retransmit lookup in `tick` is logarithmic too.
        let before = s.lookup_steps;
        let out = s.tick(Duration::ZERO);
        assert_eq!(out.len(), 1);
        assert!(s.lookup_steps - before <= 14);
        assert_eq!(DataPacket::parse(&out[0]).unwrap().seq_number, 8000);
    }

    /// A NAK range that straddles the 31-bit sequence wrap selects exactly
    /// the buffered packets inside it, and they are retransmitted oldest
    /// first in circular order (the pending set is ordered by raw value,
    /// which would send `0, 1` before `MAX - 1, MAX`).
    #[test]
    fn nak_range_across_the_sequence_wrap_retransmits_in_circular_order() {
        const MAX: u32 = crate::packet::SEQ_NUMBER_MASK;
        let mut s = Sender::new(PEER);
        for q in [MAX - 2, MAX - 1, MAX, 0, 1, 2] {
            s.on_data(q, 1, b"x", Duration::ZERO).unwrap();
        }
        feed_nak(&mut s, &[LossListEntry::Range(MAX - 1, 1)]);
        assert_eq!(s.pending_retransmit_count(), 4);
        let seqs: Vec<u32> = s
            .tick(Duration::ZERO)
            .iter()
            .map(|b| DataPacket::parse(b).unwrap().seq_number)
            .collect();
        assert_eq!(seqs, alloc::vec![MAX - 1, MAX, 0, 1]);
    }

    /// A range whose end precedes its start is malformed and names nothing.
    #[test]
    fn malformed_nak_range_names_nothing() {
        let mut s = Sender::new(PEER);
        for q in 0..20u32 {
            s.on_data(q, q, b"x", Duration::ZERO).unwrap();
        }
        feed_nak(&mut s, &[LossListEntry::Range(10, 5)]);
        assert_eq!(s.pending_retransmit_count(), 0);
        // Beyond half the sequence space is also "before" in circular order.
        feed_nak(&mut s, &[LossListEntry::Range(0, 0x5000_0000)]);
        assert_eq!(s.pending_retransmit_count(), 0);
    }

    /// r08-SRT-O3: an ACK drops exactly the freed sequence numbers from the
    /// retransmit queue and leaves the rest (it no longer rebuilds a set of
    /// the whole buffer to do it).
    #[test]
    fn ack_purges_only_freed_seqs_from_the_retransmit_queue() {
        let mut s = Sender::new(PEER);
        for q in 0..6u32 {
            s.on_data(q, q, b"x", Duration::ZERO).unwrap();
        }
        feed_nak(&mut s, &[LossListEntry::Range(1, 4)]);
        assert_eq!(s.pending_retransmit_count(), 4);
        let ack = AckPacket {
            ack_number: 0,
            timestamp: 0,
            dest_socket_id: PEER,
            cif: AckCif::Light { last_ack_seq: 3 },
        };
        assert!(s.on_ack(&ack, Duration::ZERO).is_none());
        // seqs 1, 2 freed; 3, 4 still pending and still buffered.
        assert_eq!(s.pending_retransmit_count(), 2);
        let seqs: Vec<u32> = s
            .tick(Duration::ZERO)
            .iter()
            .map(|b| DataPacket::parse(b).unwrap().seq_number)
            .collect();
        assert_eq!(seqs, alloc::vec![3, 4]);
    }

    /// Wire fields are 31 / 26 bits: wider inputs are an error (nothing is
    /// buffered), the widest legal values are fine.
    #[test]
    fn on_data_rejects_values_wider_than_their_wire_fields() {
        let mut s = Sender::new(PEER);
        assert!(matches!(
            s.on_data(SEQ_NUMBER_MASK + 1, 0, b"x", Duration::ZERO),
            Err(Error::FieldTooWide { bits: 31, .. })
        ));
        assert!(matches!(
            s.on_data(0, MESSAGE_NUMBER_MASK + 1, b"x", Duration::ZERO),
            Err(Error::FieldTooWide { bits: 26, .. })
        ));
        assert_eq!(s.buffered_count(), 0, "a rejected packet is not buffered");
        let bytes = s
            .on_data(SEQ_NUMBER_MASK, MESSAGE_NUMBER_MASK, b"x", Duration::ZERO)
            .unwrap();
        let dp = DataPacket::parse(&bytes).unwrap();
        assert_eq!(dp.seq_number, SEQ_NUMBER_MASK);
        assert_eq!(dp.message_number, MESSAGE_NUMBER_MASK);
        feed_nak(&mut s, &[LossListEntry::Single(SEQ_NUMBER_MASK)]);
        assert_eq!(s.pending_retransmit_count(), 1);
    }
}