srt-runtime 0.13.1

SRT (Secure Reliable Transport, draft-sharabayko-srt-01) packet codecs, sans-IO HSv5 handshakes, a one-connection link engine, ARQ, TSBPD, LiveCC/FileCC congestion control, optional payload crypto and an optional tokio UDP 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! The `broadcast_core::Profile` presets of this crate's bounds. Every number is
//! a documented constant used only by a `for_profile` body; the caller replaces
//! any field through its typed `with_*` setter.

use core::time::Duration;

use crate::arq::{ArqBounds, ReceiverBounds, SenderBounds};
use crate::handshake_sm::HandshakeConfig;
#[cfg(feature = "tokio")]
use crate::io::{ChannelCapacities, CloseTimings, IoConfig, ListenerLimits};
#[cfg(feature = "link")]
use crate::link::LinkCadence;
use broadcast_core::Profile;
#[cfg(feature = "tokio")]
use core::num::NonZeroUsize;

/// Embedded: 8192 packets, over 5 s of a 15 Mbit/s stream in the loss window. Measured (#4834): exercised on a real fixture by the Embedded harness.
const RECEIVER_MAX_TRACKED_WINDOW_EMBEDDED: u32 = 1 << 13;
/// Device: 32 768 packets, over 5 s of a 60 Mbit/s 4K stream.
const RECEIVER_MAX_TRACKED_WINDOW_DEVICE: u32 = 1 << 15;
/// Server: 262 144 packets, the window the origin always tracked.
const RECEIVER_MAX_TRACKED_WINDOW_SERVER: u32 = 1 << 18;
/// Headend: 262 144 packets, a contribution link's high-latency window.
const RECEIVER_MAX_TRACKED_WINDOW_HEADEND: u32 = 1 << 18;

/// Embedded: 64 queued too-late skips between drains. Measured (#4834): exercised on a real fixture by the Embedded harness.
const RECEIVER_MAX_PENDING_SKIPS_EMBEDDED: usize = 64;
/// Device: 256 queued too-late skips between drains.
const RECEIVER_MAX_PENDING_SKIPS_DEVICE: usize = 256;
/// Server: 1024 queued too-late skips, the value the origin always used.
const RECEIVER_MAX_PENDING_SKIPS_SERVER: usize = 1024;
/// Headend: 4096 queued too-late skips on a lossy contribution link.
const RECEIVER_MAX_PENDING_SKIPS_HEADEND: usize = 4096;

/// Embedded: 64 unanswered full ACKs, many RTTs of 10 ms ACKs. No run-time cost: a timing or retry count.
const RECEIVER_MAX_OUTSTANDING_ACKS_EMBEDDED: usize = 64;
/// Device: 256 unanswered full ACKs.
const RECEIVER_MAX_OUTSTANDING_ACKS_DEVICE: usize = 256;
/// Server: 1024 unanswered full ACKs, the value the origin always used.
const RECEIVER_MAX_OUTSTANDING_ACKS_SERVER: usize = 1024;
/// Headend: 4096 unanswered full ACKs on a long-haul link.
const RECEIVER_MAX_OUTSTANDING_ACKS_HEADEND: usize = 4096;

/// Embedded: 4096 sequence numbers expanded from one NAK. Measured (#4834): exercised on a real fixture by the Embedded harness.
const SENDER_MAX_NAK_SEQS_PER_PACKET_EMBEDDED: usize = 1 << 12;
/// Device: 16 384 sequence numbers expanded from one NAK.
const SENDER_MAX_NAK_SEQS_PER_PACKET_DEVICE: usize = 1 << 14;
/// Server: 65 536 sequence numbers expanded from one NAK, the value the origin always used.
const SENDER_MAX_NAK_SEQS_PER_PACKET_SERVER: usize = 1 << 16;
/// Headend: 65 536 sequence numbers expanded from one NAK.
const SENDER_MAX_NAK_SEQS_PER_PACKET_HEADEND: usize = 1 << 16;

/// Embedded: 40 ms between resends of one packet spares a slow uplink. No run-time cost: a timing or retry count.
const SENDER_MIN_RETRANSMIT_INTERVAL_EMBEDDED: Duration = Duration::from_millis(40);
/// Device: 30 ms between resends of one packet.
const SENDER_MIN_RETRANSMIT_INTERVAL_DEVICE: Duration = Duration::from_millis(30);
/// Server: 20 ms between resends of one packet, the value the origin always used.
const SENDER_MIN_RETRANSMIT_INTERVAL_SERVER: Duration = Duration::from_millis(20);
/// Headend: 20 ms between resends of one packet.
const SENDER_MIN_RETRANSMIT_INTERVAL_HEADEND: Duration = Duration::from_millis(20);

/// Embedded: 4 packets per pacing burst keep a small device's socket buffer short. Measured (#4834): exercised on a real fixture by the Embedded harness.
const SENDER_PACING_BURST_PACKETS_EMBEDDED: usize = 4;
/// Device: 8 packets per pacing burst.
const SENDER_PACING_BURST_PACKETS_DEVICE: usize = 8;
/// Server: 16 packets per pacing burst, the value the origin always used.
const SENDER_PACING_BURST_PACKETS_SERVER: usize = 16;
/// Headend: 32 packets per pacing burst for multi-hundred-megabit feeds.
const SENDER_PACING_BURST_PACKETS_HEADEND: usize = 32;

#[cfg(feature = "tokio")]
mod listener_values {
    use super::*;
    /// Embedded: 16 handshakes in progress, a few callers at once. No run-time cost: a timing or retry count.
    pub(super) const LISTENER_MAX_PENDING_EMBEDDED: usize = 16;
    /// Device: 64 handshakes in progress.
    pub(super) const LISTENER_MAX_PENDING_DEVICE: usize = 64;
    /// Server: 1024 handshakes in progress, bounding a spoofed INDUCTION flood.
    pub(super) const LISTENER_MAX_PENDING_SERVER: usize = 1024;
    /// Headend: 4096 handshakes in progress across many contribution feeds.
    pub(super) const LISTENER_MAX_PENDING_HEADEND: usize = 4096;

    /// Embedded: expire and resend pending handshakes every 250 ms. No run-time cost: a timing or retry count.
    pub(super) const LISTENER_PENDING_TICK_EMBEDDED: Duration = Duration::from_millis(250);
    /// Device: expire and resend pending handshakes every 200 ms.
    pub(super) const LISTENER_PENDING_TICK_DEVICE: Duration = Duration::from_millis(200);
    /// Server: expire and resend pending handshakes every 100 ms.
    pub(super) const LISTENER_PENDING_TICK_SERVER: Duration = Duration::from_millis(100);
    /// Headend: expire and resend pending handshakes every 100 ms.
    pub(super) const LISTENER_PENDING_TICK_HEADEND: Duration = Duration::from_millis(100);

    /// Embedded: replace the SYN-cookie key every 120 s. No run-time cost: a timing or retry count.
    pub(super) const LISTENER_COOKIE_KEY_ROTATION_EMBEDDED: Duration = Duration::from_secs(120);
    /// Device: replace the SYN-cookie key every 90 s.
    pub(super) const LISTENER_COOKIE_KEY_ROTATION_DEVICE: Duration = Duration::from_secs(90);
    /// Server: replace the SYN-cookie key every 60 s, the cookie's one-minute time bucket.
    pub(super) const LISTENER_COOKIE_KEY_ROTATION_SERVER: Duration = Duration::from_secs(60);
    /// Headend: replace the SYN-cookie key every 60 s, the cookie's one-minute time bucket.
    pub(super) const LISTENER_COOKIE_KEY_ROTATION_HEADEND: Duration = Duration::from_secs(60);

    /// Embedded: answer a repeated CONCLUSION for 20 s. No run-time cost: a timing or retry count.
    pub(super) const LISTENER_RECENT_ACCEPT_TTL_EMBEDDED: Duration = Duration::from_secs(20);
    /// Device: answer a repeated CONCLUSION for 15 s.
    pub(super) const LISTENER_RECENT_ACCEPT_TTL_DEVICE: Duration = Duration::from_secs(15);
    /// Server: answer a repeated CONCLUSION for 10 s, longer than any caller's retry series.
    pub(super) const LISTENER_RECENT_ACCEPT_TTL_SERVER: Duration = Duration::from_secs(10);
    /// Headend: answer a repeated CONCLUSION for 10 s, longer than any caller's retry series.
    pub(super) const LISTENER_RECENT_ACCEPT_TTL_HEADEND: Duration = Duration::from_secs(10);
}
#[cfg(feature = "tokio")]
use listener_values::*;

/// Embedded: 80 ms latency on a nearby LAN sender; the peer's larger value wins. No run-time cost: a timing or retry count.
const HANDSHAKE_LATENCY_EMBEDDED: Duration = Duration::from_millis(80);
/// Device: 120 ms latency, libsrt's default; the peer's larger value wins.
const HANDSHAKE_LATENCY_DEVICE: Duration = Duration::from_millis(120);
/// Server: 120 ms latency, libsrt's default; the peer's larger value wins.
const HANDSHAKE_LATENCY_SERVER: Duration = Duration::from_millis(120);
/// Headend: 120 ms latency, libsrt's default; the peer's larger value wins.
const HANDSHAKE_LATENCY_HEADEND: Duration = Duration::from_millis(120);

/// Embedded: a 1500-byte MTU carries the usual 1316-byte live payload. No run-time cost: a timing or retry count.
const HANDSHAKE_MTU_EMBEDDED: u32 = 1500;
/// Device: a 1500-byte Ethernet MTU, libsrt's default.
const HANDSHAKE_MTU_DEVICE: u32 = 1500;
/// Server: a 1500-byte Ethernet MTU, libsrt's default.
const HANDSHAKE_MTU_SERVER: u32 = 1500;
/// Headend: a 1500-byte Ethernet MTU, libsrt's default.
const HANDSHAKE_MTU_HEADEND: u32 = 1500;

/// Embedded: 1024 packets in flight, a LAN round trip at tens of megabits. Measured (#4834): exercised on a real fixture by the Embedded harness.
const HANDSHAKE_FLOW_WINDOW_EMBEDDED: u32 = 1024;
/// Device: 4096 packets in flight.
const HANDSHAKE_FLOW_WINDOW_DEVICE: u32 = 4096;
/// Server: 8192 packets in flight, libsrt's default flow window.
const HANDSHAKE_FLOW_WINDOW_SERVER: u32 = 8192;
/// Headend: 8192 packets in flight, libsrt's default flow window.
const HANDSHAKE_FLOW_WINDOW_HEADEND: u32 = 8192;

/// Embedded: resend an unanswered handshake packet every 250 ms. No run-time cost: a timing or retry count.
const HANDSHAKE_RETRANSMIT_AFTER_EMBEDDED: Duration = Duration::from_micros(250_000);
/// Device: resend an unanswered handshake packet every 125 ms.
const HANDSHAKE_RETRANSMIT_AFTER_DEVICE: Duration = Duration::from_micros(125_000);
/// Server: resend an unanswered handshake packet every 83.333 ms, 12 in about 1 s.
const HANDSHAKE_RETRANSMIT_AFTER_SERVER: Duration = Duration::from_micros(83_333);
/// Headend: resend an unanswered handshake packet every 83.333 ms, 12 in about 1 s.
const HANDSHAKE_RETRANSMIT_AFTER_HEADEND: Duration = Duration::from_micros(83_333);

/// Embedded: 6 retransmissions, about a 1.5 s connect budget. No run-time cost: a timing or retry count.
const HANDSHAKE_MAX_RETRIES_EMBEDDED: u32 = 6;
/// Device: 8 retransmissions, about a 1 s connect budget.
const HANDSHAKE_MAX_RETRIES_DEVICE: u32 = 8;
/// Server: 12 retransmissions, about a 1 s connect budget.
const HANDSHAKE_MAX_RETRIES_SERVER: u32 = 12;
/// Headend: 12 retransmissions, about a 1 s connect budget.
const HANDSHAKE_MAX_RETRIES_HEADEND: u32 = 12;

#[cfg(feature = "link")]
/// Every profile: resend an unanswered handshake request every 250 ms, libsrt's period.
const CADENCE_HANDSHAKE_RETRANSMIT: Duration = Duration::from_millis(250);
#[cfg(feature = "link")]
/// Every profile: a Keep-Alive after 1 s without a packet, libsrt's period.
const CADENCE_KEEPALIVE: Duration = Duration::from_secs(1);
#[cfg(feature = "link")]
/// Every profile: resend an unconfirmed refresh KMREQ every 500 ms.
const CADENCE_KM_RESEND: Duration = Duration::from_millis(500);
#[cfg(feature = "link")]
/// Every profile: process a peer's Key Material at most every 100 ms, bounding PBKDF2 work.
const CADENCE_KMREQ_MIN_INTERVAL: Duration = Duration::from_millis(100);

#[cfg(feature = "tokio")]
mod io_values {
    use super::*;
    /// Every profile: a 1500-byte datagram, the largest a 1500-byte MTU link carries.
    pub(super) const IO_MAX_DATAGRAM: usize = 1500;
    /// Embedded: 5 s to resolve and bind. No run-time cost: a timing or retry count.
    pub(super) const IO_CONNECT_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
    /// Device: 8 s to resolve and bind on a mobile network.
    pub(super) const IO_CONNECT_TIMEOUT_DEVICE: Duration = Duration::from_secs(8);
    /// Server: 10 s to resolve and bind, the value the origin always used.
    pub(super) const IO_CONNECT_TIMEOUT_SERVER: Duration = Duration::from_secs(10);
    /// Headend: 10 s to resolve and bind.
    pub(super) const IO_CONNECT_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
    /// Embedded: 3 s for the caller handshake, libsrt's connect timeout. No run-time cost: a timing or retry count.
    pub(super) const IO_HANDSHAKE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(3);
    /// Device: 3 s for the caller handshake, libsrt's connect timeout.
    pub(super) const IO_HANDSHAKE_TIMEOUT_DEVICE: Duration = Duration::from_secs(3);
    /// Server: 5 s for the caller handshake, the value the origin always used.
    pub(super) const IO_HANDSHAKE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
    /// Headend: 5 s for a long-haul handshake.
    pub(super) const IO_HANDSHAKE_TIMEOUT_HEADEND: Duration = Duration::from_secs(5);
    /// Embedded: 5 s of peer silence, libsrt's peer idle timeout. No run-time cost: a timing or retry count.
    pub(super) const IO_READ_IDLE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
    /// Device: 5 s of peer silence, libsrt's peer idle timeout.
    pub(super) const IO_READ_IDLE_TIMEOUT_DEVICE: Duration = Duration::from_secs(5);
    /// Server: 5 s of peer silence, the value the origin always used.
    pub(super) const IO_READ_IDLE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
    /// Headend: 10 s of peer silence on a sparse contribution link.
    pub(super) const IO_READ_IDLE_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
    /// Embedded: 5 s for one send. No run-time cost: a timing or retry count.
    pub(super) const IO_WRITE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
    /// Device: 5 s for one send.
    pub(super) const IO_WRITE_TIMEOUT_DEVICE: Duration = Duration::from_secs(5);
    /// Server: 5 s for one send, the value the origin always used.
    pub(super) const IO_WRITE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
    /// Headend: 10 s for one send on a congested egress.
    pub(super) const IO_WRITE_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
    /// Embedded: 256 packets per drift-sampling window. Measured (#4834): exercised on a real fixture by the Embedded harness.
    pub(super) const IO_DRIFT_SAMPLE_COUNT_EMBEDDED: u32 = 256;
    /// Device: 512 packets per drift-sampling window.
    pub(super) const IO_DRIFT_SAMPLE_COUNT_DEVICE: u32 = 512;
    /// Server: 1000 packets per drift-sampling window, the value the origin always used.
    pub(super) const IO_DRIFT_SAMPLE_COUNT_SERVER: u32 = 1000;
    /// Headend: 2000 packets per drift-sampling window.
    pub(super) const IO_DRIFT_SAMPLE_COUNT_HEADEND: u32 = 2000;
    /// Every profile: pace DATA to 1 Gbit/s, libsrt's default `MAXBW`.
    pub(super) const IO_MAX_BW: crate::livecc::MaxBwConfig =
        crate::livecc::MaxBwConfig::Set(125_000_000);
    /// Embedded: 512 inbound datagrams queued per connection. Measured (#4834): exercised on a real fixture by the Embedded harness.
    pub(super) const CHANNEL_RX_EMBEDDED: NonZeroUsize = non_zero(512);
    /// Device: 1024 inbound datagrams queued per connection.
    pub(super) const CHANNEL_RX_DEVICE: NonZeroUsize = non_zero(1024);
    /// Server: 4096 inbound datagrams, the value the origin always used.
    pub(super) const CHANNEL_RX_SERVER: NonZeroUsize = non_zero(4096);
    /// Headend: 16 384 inbound datagrams queued per connection.
    pub(super) const CHANNEL_RX_HEADEND: NonZeroUsize = non_zero(16_384);
    /// Embedded: 128 unrouted datagrams queued; provisional until measured (#4834): only `SrtListener::bind` reads it; the Embedded harness drives a rendezvous, not a listener.
    pub(super) const CHANNEL_UNROUTED_EMBEDDED: NonZeroUsize = non_zero(128);
    /// Device: 256 unrouted datagrams queued.
    pub(super) const CHANNEL_UNROUTED_DEVICE: NonZeroUsize = non_zero(256);
    /// Server: 1024 unrouted datagrams, the value the origin always used.
    pub(super) const CHANNEL_UNROUTED_SERVER: NonZeroUsize = non_zero(1024);
    /// Headend: 4096 unrouted datagrams queued.
    pub(super) const CHANNEL_UNROUTED_HEADEND: NonZeroUsize = non_zero(4096);
    /// Embedded: 128 payloads queued to send. Measured (#4834): exercised on a real fixture by the Embedded harness.
    pub(super) const CHANNEL_SEND_EMBEDDED: NonZeroUsize = non_zero(128);
    /// Device: 256 payloads queued to send.
    pub(super) const CHANNEL_SEND_DEVICE: NonZeroUsize = non_zero(256);
    /// Server: 1024 payloads queued to send, the value the origin always used.
    pub(super) const CHANNEL_SEND_SERVER: NonZeroUsize = non_zero(1024);
    /// Headend: 4096 payloads queued to send.
    pub(super) const CHANNEL_SEND_HEADEND: NonZeroUsize = non_zero(4096);
    /// Embedded: 512 delivered items awaiting the application. Measured (#4834): exercised on a real fixture by the Embedded harness.
    pub(super) const CHANNEL_DELIVER_EMBEDDED: NonZeroUsize = non_zero(512);
    /// Device: 1024 delivered items awaiting the application.
    pub(super) const CHANNEL_DELIVER_DEVICE: NonZeroUsize = non_zero(1024);
    /// Server: 4096 delivered items, the value the origin always used.
    pub(super) const CHANNEL_DELIVER_SERVER: NonZeroUsize = non_zero(4096);
    /// Headend: 16 384 delivered items awaiting the application.
    pub(super) const CHANNEL_DELIVER_HEADEND: NonZeroUsize = non_zero(16_384);
    /// Embedded: 8 finished handshakes awaiting `accept`; provisional until measured (#4834): only `SrtListener::bind` reads it; the Embedded harness drives a rendezvous, not a listener.
    pub(super) const CHANNEL_ACCEPT_EMBEDDED: NonZeroUsize = non_zero(8);
    /// Device: 16 finished handshakes awaiting `accept`.
    pub(super) const CHANNEL_ACCEPT_DEVICE: NonZeroUsize = non_zero(16);
    /// Server: 64 finished handshakes, the value the origin always used.
    pub(super) const CHANNEL_ACCEPT_SERVER: NonZeroUsize = non_zero(64);
    /// Headend: 256 finished handshakes awaiting `accept`.
    pub(super) const CHANNEL_ACCEPT_HEADEND: NonZeroUsize = non_zero(256);

    /// `n` as a [`NonZeroUsize`], or the smallest one for `0`, so a preset
    /// constant is written as a plain count.
    const fn non_zero(n: usize) -> NonZeroUsize {
        match NonZeroUsize::new(n) {
            Some(n) => n,
            None => NonZeroUsize::MIN,
        }
    }
}
#[cfg(feature = "tokio")]
use io_values::*;

impl ReceiverBounds {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            max_tracked_window: profile.select(
                RECEIVER_MAX_TRACKED_WINDOW_EMBEDDED,
                RECEIVER_MAX_TRACKED_WINDOW_DEVICE,
                RECEIVER_MAX_TRACKED_WINDOW_SERVER,
                RECEIVER_MAX_TRACKED_WINDOW_HEADEND,
            ),
            max_pending_skips: profile.select(
                RECEIVER_MAX_PENDING_SKIPS_EMBEDDED,
                RECEIVER_MAX_PENDING_SKIPS_DEVICE,
                RECEIVER_MAX_PENDING_SKIPS_SERVER,
                RECEIVER_MAX_PENDING_SKIPS_HEADEND,
            ),
            max_outstanding_acks: profile.select(
                RECEIVER_MAX_OUTSTANDING_ACKS_EMBEDDED,
                RECEIVER_MAX_OUTSTANDING_ACKS_DEVICE,
                RECEIVER_MAX_OUTSTANDING_ACKS_SERVER,
                RECEIVER_MAX_OUTSTANDING_ACKS_HEADEND,
            ),
        }
    }

    /// Replace [`Self::max_tracked_window`].
    #[must_use]
    pub fn with_max_tracked_window(mut self, value: u32) -> Self {
        self.max_tracked_window = value;
        self
    }

    /// Replace [`Self::max_pending_skips`].
    #[must_use]
    pub fn with_max_pending_skips(mut self, value: usize) -> Self {
        self.max_pending_skips = value;
        self
    }

    /// Replace [`Self::max_outstanding_acks`].
    #[must_use]
    pub fn with_max_outstanding_acks(mut self, value: usize) -> Self {
        self.max_outstanding_acks = value;
        self
    }
}

impl SenderBounds {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            max_nak_seqs_per_packet: profile.select(
                SENDER_MAX_NAK_SEQS_PER_PACKET_EMBEDDED,
                SENDER_MAX_NAK_SEQS_PER_PACKET_DEVICE,
                SENDER_MAX_NAK_SEQS_PER_PACKET_SERVER,
                SENDER_MAX_NAK_SEQS_PER_PACKET_HEADEND,
            ),
            min_retransmit_interval: profile.select(
                SENDER_MIN_RETRANSMIT_INTERVAL_EMBEDDED,
                SENDER_MIN_RETRANSMIT_INTERVAL_DEVICE,
                SENDER_MIN_RETRANSMIT_INTERVAL_SERVER,
                SENDER_MIN_RETRANSMIT_INTERVAL_HEADEND,
            ),
            pacing_burst_packets: profile.select(
                SENDER_PACING_BURST_PACKETS_EMBEDDED,
                SENDER_PACING_BURST_PACKETS_DEVICE,
                SENDER_PACING_BURST_PACKETS_SERVER,
                SENDER_PACING_BURST_PACKETS_HEADEND,
            ),
        }
    }

    /// Replace [`Self::max_nak_seqs_per_packet`].
    #[must_use]
    pub fn with_max_nak_seqs_per_packet(mut self, value: usize) -> Self {
        self.max_nak_seqs_per_packet = value;
        self
    }

    /// Replace [`Self::min_retransmit_interval`].
    #[must_use]
    pub fn with_min_retransmit_interval(mut self, value: Duration) -> Self {
        self.min_retransmit_interval = value;
        self
    }

    /// Replace [`Self::pacing_burst_packets`].
    #[must_use]
    pub fn with_pacing_burst_packets(mut self, value: usize) -> Self {
        self.pacing_burst_packets = value;
        self
    }
}

impl ArqBounds {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            receiver: ReceiverBounds::for_profile(profile),
            sender: SenderBounds::for_profile(profile),
        }
    }

    /// Replace [`Self::receiver`].
    #[must_use]
    pub fn with_receiver(mut self, value: crate::arq::ReceiverBounds) -> Self {
        self.receiver = value;
        self
    }

    /// Replace [`Self::sender`].
    #[must_use]
    pub fn with_sender(mut self, value: crate::arq::SenderBounds) -> Self {
        self.sender = value;
        self
    }
}

#[cfg(feature = "tokio")]
impl ListenerLimits {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            max_pending: profile.select(
                LISTENER_MAX_PENDING_EMBEDDED,
                LISTENER_MAX_PENDING_DEVICE,
                LISTENER_MAX_PENDING_SERVER,
                LISTENER_MAX_PENDING_HEADEND,
            ),
            pending_tick: profile.select(
                LISTENER_PENDING_TICK_EMBEDDED,
                LISTENER_PENDING_TICK_DEVICE,
                LISTENER_PENDING_TICK_SERVER,
                LISTENER_PENDING_TICK_HEADEND,
            ),
            cookie_key_rotation: profile.select(
                LISTENER_COOKIE_KEY_ROTATION_EMBEDDED,
                LISTENER_COOKIE_KEY_ROTATION_DEVICE,
                LISTENER_COOKIE_KEY_ROTATION_SERVER,
                LISTENER_COOKIE_KEY_ROTATION_HEADEND,
            ),
            recent_accept_ttl: profile.select(
                LISTENER_RECENT_ACCEPT_TTL_EMBEDDED,
                LISTENER_RECENT_ACCEPT_TTL_DEVICE,
                LISTENER_RECENT_ACCEPT_TTL_SERVER,
                LISTENER_RECENT_ACCEPT_TTL_HEADEND,
            ),
        }
    }

    /// Replace [`Self::max_pending`].
    #[must_use]
    pub fn with_max_pending(mut self, value: usize) -> Self {
        self.max_pending = value;
        self
    }

    /// Replace [`Self::pending_tick`].
    #[must_use]
    pub fn with_pending_tick(mut self, value: Duration) -> Self {
        self.pending_tick = value;
        self
    }

    /// Replace [`Self::cookie_key_rotation`].
    #[must_use]
    pub fn with_cookie_key_rotation(mut self, value: Duration) -> Self {
        self.cookie_key_rotation = value;
        self
    }

    /// Replace [`Self::recent_accept_ttl`].
    #[must_use]
    pub fn with_recent_accept_ttl(mut self, value: Duration) -> Self {
        self.recent_accept_ttl = value;
        self
    }
}

impl HandshakeConfig {
    /// The [`Profile`] preset, built from this crate's feature set with no
    /// stream id, group or encryption, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self::new(
            profile.select(
                HANDSHAKE_LATENCY_EMBEDDED,
                HANDSHAKE_LATENCY_DEVICE,
                HANDSHAKE_LATENCY_SERVER,
                HANDSHAKE_LATENCY_HEADEND,
            ),
            profile.select(
                HANDSHAKE_MTU_EMBEDDED,
                HANDSHAKE_MTU_DEVICE,
                HANDSHAKE_MTU_SERVER,
                HANDSHAKE_MTU_HEADEND,
            ),
            profile.select(
                HANDSHAKE_FLOW_WINDOW_EMBEDDED,
                HANDSHAKE_FLOW_WINDOW_DEVICE,
                HANDSHAKE_FLOW_WINDOW_SERVER,
                HANDSHAKE_FLOW_WINDOW_HEADEND,
            ),
            profile.select(
                HANDSHAKE_RETRANSMIT_AFTER_EMBEDDED,
                HANDSHAKE_RETRANSMIT_AFTER_DEVICE,
                HANDSHAKE_RETRANSMIT_AFTER_SERVER,
                HANDSHAKE_RETRANSMIT_AFTER_HEADEND,
            ),
            profile.select(
                HANDSHAKE_MAX_RETRIES_EMBEDDED,
                HANDSHAKE_MAX_RETRIES_DEVICE,
                HANDSHAKE_MAX_RETRIES_SERVER,
                HANDSHAKE_MAX_RETRIES_HEADEND,
            ),
        )
    }
}

#[cfg(feature = "link")]
impl LinkCadence {
    /// The [`Profile`] preset, replaceable through its `with_*` setters. The
    /// cadence is a protocol rhythm, the same on every profile.
    #[must_use]
    pub fn for_profile(_profile: Profile) -> Self {
        Self {
            handshake_retransmit: CADENCE_HANDSHAKE_RETRANSMIT,
            keepalive: CADENCE_KEEPALIVE,
            km_resend: CADENCE_KM_RESEND,
            kmreq_min_interval: CADENCE_KMREQ_MIN_INTERVAL,
        }
    }

    /// Replace [`Self::handshake_retransmit`].
    #[must_use]
    pub fn with_handshake_retransmit(mut self, handshake_retransmit: Duration) -> Self {
        self.handshake_retransmit = handshake_retransmit;
        self
    }

    /// Replace [`Self::keepalive`].
    #[must_use]
    pub fn with_keepalive(mut self, keepalive: Duration) -> Self {
        self.keepalive = keepalive;
        self
    }

    /// Replace [`Self::km_resend`].
    #[must_use]
    pub fn with_km_resend(mut self, km_resend: Duration) -> Self {
        self.km_resend = km_resend;
        self
    }

    /// Replace [`Self::kmreq_min_interval`].
    #[must_use]
    pub fn with_kmreq_min_interval(mut self, kmreq_min_interval: Duration) -> Self {
        self.kmreq_min_interval = kmreq_min_interval;
        self
    }
}

#[cfg(feature = "tokio")]
impl ChannelCapacities {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            rx: profile.select(
                CHANNEL_RX_EMBEDDED,
                CHANNEL_RX_DEVICE,
                CHANNEL_RX_SERVER,
                CHANNEL_RX_HEADEND,
            ),
            unrouted: profile.select(
                CHANNEL_UNROUTED_EMBEDDED,
                CHANNEL_UNROUTED_DEVICE,
                CHANNEL_UNROUTED_SERVER,
                CHANNEL_UNROUTED_HEADEND,
            ),
            send: profile.select(
                CHANNEL_SEND_EMBEDDED,
                CHANNEL_SEND_DEVICE,
                CHANNEL_SEND_SERVER,
                CHANNEL_SEND_HEADEND,
            ),
            deliver: profile.select(
                CHANNEL_DELIVER_EMBEDDED,
                CHANNEL_DELIVER_DEVICE,
                CHANNEL_DELIVER_SERVER,
                CHANNEL_DELIVER_HEADEND,
            ),
            accept: profile.select(
                CHANNEL_ACCEPT_EMBEDDED,
                CHANNEL_ACCEPT_DEVICE,
                CHANNEL_ACCEPT_SERVER,
                CHANNEL_ACCEPT_HEADEND,
            ),
        }
    }

    /// Replace [`Self::rx`].
    #[must_use]
    pub fn with_rx(mut self, rx: NonZeroUsize) -> Self {
        self.rx = rx;
        self
    }

    /// Replace [`Self::unrouted`].
    #[must_use]
    pub fn with_unrouted(mut self, unrouted: NonZeroUsize) -> Self {
        self.unrouted = unrouted;
        self
    }

    /// Replace [`Self::send`].
    #[must_use]
    pub fn with_send(mut self, send: NonZeroUsize) -> Self {
        self.send = send;
        self
    }

    /// Replace [`Self::deliver`].
    #[must_use]
    pub fn with_deliver(mut self, deliver: NonZeroUsize) -> Self {
        self.deliver = deliver;
        self
    }

    /// Replace [`Self::accept`].
    #[must_use]
    pub fn with_accept(mut self, accept: NonZeroUsize) -> Self {
        self.accept = accept;
        self
    }
}

#[cfg(feature = "tokio")]
impl IoConfig {
    /// The [`Profile`] preset: every field stated, the listener, ARQ, cadence
    /// and channel bounds from their own presets. Every preset passes
    /// [`Self::validate`] (pinned by a test); the existing `with_*` setters and
    /// the public fields replace any part.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            max_datagram: IO_MAX_DATAGRAM,
            timeouts: broadcast_net::timeouts::Timeouts::new(
                profile.select(
                    IO_CONNECT_TIMEOUT_EMBEDDED,
                    IO_CONNECT_TIMEOUT_DEVICE,
                    IO_CONNECT_TIMEOUT_SERVER,
                    IO_CONNECT_TIMEOUT_HEADEND,
                ),
                profile.select(
                    IO_HANDSHAKE_TIMEOUT_EMBEDDED,
                    IO_HANDSHAKE_TIMEOUT_DEVICE,
                    IO_HANDSHAKE_TIMEOUT_SERVER,
                    IO_HANDSHAKE_TIMEOUT_HEADEND,
                ),
                profile.select(
                    IO_READ_IDLE_TIMEOUT_EMBEDDED,
                    IO_READ_IDLE_TIMEOUT_DEVICE,
                    IO_READ_IDLE_TIMEOUT_SERVER,
                    IO_READ_IDLE_TIMEOUT_HEADEND,
                ),
                profile.select(
                    IO_WRITE_TIMEOUT_EMBEDDED,
                    IO_WRITE_TIMEOUT_DEVICE,
                    IO_WRITE_TIMEOUT_SERVER,
                    IO_WRITE_TIMEOUT_HEADEND,
                ),
            ),
            km_refresh_thresholds: crate::km_refresh::KmRefreshThresholds::RECOMMENDED,
            cadence: LinkCadence::for_profile(profile),
            listener: ListenerLimits::for_profile(profile),
            arq: ArqBounds::for_profile(profile),
            drift_sample_count: profile.select(
                IO_DRIFT_SAMPLE_COUNT_EMBEDDED,
                IO_DRIFT_SAMPLE_COUNT_DEVICE,
                IO_DRIFT_SAMPLE_COUNT_SERVER,
                IO_DRIFT_SAMPLE_COUNT_HEADEND,
            ),
            channels: ChannelCapacities::for_profile(profile),
            max_bw: IO_MAX_BW,
            close: CloseTimings::for_profile(profile),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn server_and_headend_literals_are_pinned() {
        let arq = crate::arq::ArqBounds::for_profile(Profile::Server);
        assert_eq!(arq.receiver.max_tracked_window, 1 << 18);
        assert_eq!(arq.receiver.max_pending_skips, 1024);
        assert_eq!(arq.receiver.max_outstanding_acks, 1024);
        assert_eq!(arq.sender.max_nak_seqs_per_packet, 1 << 16);
        assert_eq!(
            arq.sender.min_retransmit_interval,
            Duration::from_millis(20)
        );
        assert_eq!(arq.sender.pacing_burst_packets, 16);
        let headend = crate::arq::ArqBounds::for_profile(Profile::Headend);
        assert_eq!(headend.receiver.max_pending_skips, 4096);
        assert_eq!(headend.sender.pacing_burst_packets, 32);
        #[cfg(feature = "tokio")]
        let listener = crate::io::ListenerLimits::for_profile(Profile::Server);
        #[cfg(feature = "tokio")]
        {
            assert_eq!(listener.max_pending, 1024);
            assert_eq!(listener.pending_tick, Duration::from_millis(100));
            assert_eq!(listener.cookie_key_rotation, Duration::from_secs(60));
            assert_eq!(listener.recent_accept_ttl, Duration::from_secs(10));
            assert_eq!(
                crate::io::ListenerLimits::for_profile(Profile::Headend).max_pending,
                4096
            );
        }
        let handshake = crate::handshake_sm::HandshakeConfig::for_profile(Profile::Server);
        assert_eq!(handshake.latency, Duration::from_millis(120));
        assert_eq!(handshake.mtu, 1500);
        assert_eq!(handshake.max_flow_window_size, 8192);
        assert_eq!(handshake.retransmit_after, Duration::from_micros(83_333));
        assert_eq!(handshake.max_retries, 12);
        assert_eq!(
            crate::handshake_sm::HandshakeConfig::for_profile(Profile::Headend).mtu,
            1500
        );
    }

    #[cfg(feature = "tokio")]
    #[test]
    fn every_io_preset_validates_and_server_is_the_origin_value() {
        for profile in Profile::ALL {
            IoConfig::for_profile(profile)
                .validate()
                .unwrap_or_else(|e| panic!("{profile:?}: {e}"));
        }
        let server = IoConfig::for_profile(Profile::Server);
        assert_eq!(server.max_datagram, 1500);
        assert_eq!(server.drift_sample_count, 1000);
        assert_eq!(server.timeouts.connect, Duration::from_secs(10));
        assert_eq!(server.timeouts.write, Duration::from_secs(5));
        assert_eq!(server.channels.rx.get(), 4096);
        assert_eq!(server.channels.accept.get(), 64);
        assert_eq!(server.max_bw, crate::livecc::MaxBwConfig::Set(125_000_000));
        assert_eq!(server.cadence.keepalive, Duration::from_secs(1));
        let rx = Profile::ALL.map(|p| ChannelCapacities::for_profile(p).rx);
        assert!(rx.windows(2).all(|w| w[0] <= w[1]));
    }

    #[test]
    fn capacities_grow_with_the_profile() {
        let windows = Profile::ALL.map(|p| ReceiverBounds::for_profile(p).max_tracked_window);
        assert!(windows.windows(2).all(|w| w[0] <= w[1]));
        let skips = Profile::ALL.map(|p| ReceiverBounds::for_profile(p).max_pending_skips);
        assert!(skips.windows(2).all(|w| w[0] <= w[1]));
        #[cfg(feature = "tokio")]
        {
            let pending =
                Profile::ALL.map(|p| crate::io::ListenerLimits::for_profile(p).max_pending);
            assert!(pending.windows(2).all(|w| w[0] <= w[1]));
        }
    }
}