asupersync 0.4.11

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
//! Authenticated single-connection native QUIC over a real UDP endpoint.
//!
//! This module owns the production composition seam between the wire-driven
//! rustls handshake, handshake-derived 1-RTT packet protection, the real UDP
//! endpoint, and the application-facing [`QuicConnection`]. It deliberately
//! remains caller-driven: no executor, background task, or ambient listener is
//! created.
//!
//! # Caller-driven waits (GH#67)
//!
//! [`NativeQuicUdpConnection::drive_io_once`] bounds its receive wait with
//! [`timeout`] over [`QuicUdpEndpoint::receive_batch`]. Under the documented
//! composition — a plain executor such as `futures_lite::block_on` plus an
//! explicit [`Cx`] that carries no runtime drivers — both halves of that wait
//! park the calling thread: the timeout's `Sleep` registers with the
//! process-global fallback timer, and the socket's readiness interest
//! registers with the process-global fallback I/O driver owned by `net::udp`,
//! whose pump thread wakes the waiter when a datagram arrives. A quiet
//! connection therefore costs ~0 CPU between wakeups instead of re-polling in
//! a hot loop until the timeout fires. Under a runtime whose `Cx` carries I/O
//! and timer drivers, the same wait uses those drivers instead.

use std::fmt;
use std::net::SocketAddr;
use std::time::{Duration, Instant};

use crate::bytes::BytesMut;
use crate::cx::Cx;
use crate::net::atp::quic::{AtpPacketProtection, AtpPacketProtectionConfig};
use crate::net::quic_core::{ConnectionId, ProtectedHeaderPrefix, TransportParameters};
use crate::time::timeout;

use super::connection::{NativeQuicConnectionConfig, NativeQuicConnectionError};
use super::connection_manager::{
    ConnectionRouterError, PROTECTED_1RTT_MAX_PACKET_BYTES, assemble_protected_1rtt_packet,
    generate_congestion_admitted_1rtt_frames, is_ack_eliciting, protected_1rtt_packet_len,
    unprotect_1rtt_packet,
};
use super::endpoint::{
    OutgoingPacket, QuicUdpEndpoint, QuicUdpEndpointConfig, QuicUdpEndpointError, ReceivedPacket,
};
use super::endpoint_api::QuicConnection;
use super::handshake_driver::{
    QuicHandshakeDriver, client_handshake_over_udp, server_handshake_over_udp_with_early_data,
};
use super::managed_endpoint::{ManagedEndpointConfig, ManagedEndpointError, ManagedQuicEndpoint};
use super::streams::{StreamRole, StreamWindows};
use super::transport::PacketNumberSpace;

const RECEIVE_BATCH_SIZE: usize = 32;
const MAX_PACKETS_PER_FLUSH: usize = 64;
pub(crate) const FINAL_HANDSHAKE_FLIGHT_RESEND_INTERVAL: Duration = Duration::from_millis(750);

/// Progress made by one bounded live-UDP drive operation.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NativeQuicUdpIoProgress {
    /// Early 1-RTT packets retained during server handshake completion and
    /// replayed into the authenticated application-data path.
    pub early_packets_replayed: usize,
    /// Authenticated 1-RTT packets delivered to the connection state machine.
    pub packets_received: usize,
    /// Protected 1-RTT packets sent after receive processing.
    pub packets_sent: usize,
    /// Packets ignored before application delivery.
    pub packets_dropped: usize,
    /// Retained final handshake flights retransmitted after stale long-header traffic.
    pub handshake_flights_retransmitted: usize,
    /// Whether the bounded receive window elapsed without a UDP batch.
    pub receive_timed_out: bool,
}

/// Errors from the authenticated single-connection UDP owner.
#[derive(Debug)]
pub enum NativeQuicUdpConnectionError {
    /// The explicit capability context was cancelled.
    Cancelled,
    /// The real TLS/QUIC handshake failed.
    Handshake(super::tls::QuicTlsError),
    /// The native connection state machine rejected an operation.
    Transport(NativeQuicConnectionError),
    /// The real UDP endpoint failed.
    Endpoint(QuicUdpEndpointError),
    /// The completed handshake did not install all required state.
    HandshakeIncomplete(&'static str),
    /// TLS selected a protocol other than the required application protocol.
    AlpnMismatch {
        /// Required ALPN.
        expected: Vec<u8>,
        /// Negotiated ALPN, or `None` when the peer selected none.
        negotiated: Option<Vec<u8>>,
    },
    /// TLS-authenticated QUIC transport parameters were missing or invalid.
    TransportParameters(String),
    /// The packet-protection or packet-assembly boundary failed.
    Packet(String),
    /// A UDP batch reported partial failure.
    BatchSend(String),
}

impl fmt::Display for NativeQuicUdpConnectionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cancelled => write!(f, "native QUIC UDP operation cancelled"),
            Self::Handshake(error) => write!(f, "native QUIC handshake failed: {error}"),
            Self::Transport(error) => write!(f, "native QUIC transport failed: {error}"),
            Self::Endpoint(error) => write!(f, "native QUIC UDP endpoint failed: {error}"),
            Self::HandshakeIncomplete(reason) => {
                write!(f, "native QUIC handshake handoff incomplete: {reason}")
            }
            Self::AlpnMismatch {
                expected,
                negotiated,
            } => write!(
                f,
                "native QUIC ALPN mismatch: expected {:?}, negotiated {:?}",
                String::from_utf8_lossy(expected),
                negotiated.as_deref().map(String::from_utf8_lossy)
            ),
            Self::TransportParameters(reason) => {
                write!(f, "native QUIC transport parameters invalid: {reason}")
            }
            Self::Packet(reason) => write!(f, "native QUIC packet failed: {reason}"),
            Self::BatchSend(reason) => write!(f, "native QUIC UDP batch failed: {reason}"),
        }
    }
}

impl std::error::Error for NativeQuicUdpConnectionError {}

impl From<NativeQuicConnectionError> for NativeQuicUdpConnectionError {
    fn from(value: NativeQuicConnectionError) -> Self {
        match value {
            NativeQuicConnectionError::Cancelled => Self::Cancelled,
            other => Self::Transport(other),
        }
    }
}

impl From<QuicUdpEndpointError> for NativeQuicUdpConnectionError {
    fn from(value: QuicUdpEndpointError) -> Self {
        match value {
            QuicUdpEndpointError::Cancelled => Self::Cancelled,
            other => Self::Endpoint(other),
        }
    }
}

impl From<ConnectionRouterError> for NativeQuicUdpConnectionError {
    fn from(value: ConnectionRouterError) -> Self {
        match value {
            ConnectionRouterError::Cancelled => Self::Cancelled,
            other => Self::Packet(other.to_string()),
        }
    }
}

/// One authenticated native QUIC connection bound to its real UDP socket.
///
/// The handle is intentionally not split: application stream state, packet
/// protection, connection IDs, timers, and UDP ownership cannot outlive or
/// silently detach from one another. Callers queue/read streams through
/// [`Self::connection_mut`], then call [`Self::flush`] and
/// [`Self::drive_io_once`] from their structured-concurrency scope.
/// [`Self::into_managed`] transfers this complete owner into a managed endpoint
/// without repeating the handshake or detaching its packet protection.
pub struct NativeQuicUdpConnection {
    connection: QuicConnection,
    endpoint: QuicUdpEndpoint,
    protection: AtpPacketProtection,
    local_cid: ConnectionId,
    peer_cid: ConnectionId,
    peer_addr: SocketAddr,
    negotiated_alpn: Vec<u8>,
    final_handshake_flight: Vec<OutgoingPacket>,
    early_one_rtt_packets: Vec<ReceivedPacket>,
    last_final_flight_retransmit: Option<Instant>,
    clock_origin: Instant,
}

/// Crate-private ownership transfer after the managed endpoint's preflight.
///
/// Keep the complete application handle and its original recovery clock. These
/// parts are moved, never reconstructed from handshake flags or cloned keys.
/// The retained packet vectors keep their existing order and timestamps.
pub(crate) struct NativeQuicUdpHandoffParts {
    pub(crate) connection: QuicConnection,
    pub(crate) endpoint: QuicUdpEndpoint,
    pub(crate) protection: AtpPacketProtection,
    pub(crate) local_cid: ConnectionId,
    pub(crate) peer_cid: ConnectionId,
    pub(crate) peer_addr: SocketAddr,
    pub(crate) negotiated_alpn: Vec<u8>,
    pub(crate) final_handshake_flight: Vec<OutgoingPacket>,
    pub(crate) early_one_rtt_packets: Vec<ReceivedPacket>,
    pub(crate) last_final_flight_retransmit: Option<Instant>,
    pub(crate) clock_origin: Instant,
}

/// TLS-derived application ownership independent of the socket that drove it.
/// Only the completed-driver validator below creates these parts; managed
/// admission keeps its original UDP endpoint throughout the handshake.
pub(crate) struct AuthenticatedQuicParts {
    pub(crate) connection: QuicConnection,
    pub(crate) protection: AtpPacketProtection,
    pub(crate) peer_cid: ConnectionId,
    pub(crate) negotiated_alpn: Vec<u8>,
    pub(crate) final_handshake_flight: Vec<OutgoingPacket>,
}

/// A refused managed handoff, retaining the original authenticated UDP owner.
///
/// No connection, socket, queued stream data, packet-protection state, or
/// retained handshake/early packets are discarded by a preflight refusal.
/// Recover the owner with [`Self::into_connection`] to continue driving it or
/// retry the handoff with a suitable context and configuration.
#[derive(Debug)]
pub struct ManagedQuicHandoffError {
    error: ManagedEndpointError,
    connection: Box<NativeQuicUdpConnection>,
}

impl ManagedQuicHandoffError {
    pub(crate) fn new(error: ManagedEndpointError, connection: NativeQuicUdpConnection) -> Self {
        Self {
            error,
            connection: Box::new(connection),
        }
    }

    /// The typed reason the managed endpoint refused the handoff.
    #[must_use]
    pub fn error(&self) -> &ManagedEndpointError {
        &self.error
    }

    /// Inspect the original owner without consuming the refusal.
    #[must_use]
    pub fn connection(&self) -> &NativeQuicUdpConnection {
        &self.connection
    }

    /// Recover the original authenticated UDP owner.
    #[must_use]
    pub fn into_connection(self) -> NativeQuicUdpConnection {
        *self.connection
    }

    /// Recover both the refusal reason and the original owner.
    #[must_use]
    pub fn into_parts(self) -> (ManagedEndpointError, NativeQuicUdpConnection) {
        (self.error, *self.connection)
    }
}

impl fmt::Display for ManagedQuicHandoffError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "native QUIC managed handoff refused: {}", self.error)
    }
}

impl std::error::Error for ManagedQuicHandoffError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.error)
    }
}

impl fmt::Debug for NativeQuicUdpConnection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NativeQuicUdpConnection")
            .field("role", &self.connection.role())
            .field("local_addr", &self.endpoint.local_addr())
            .field("peer_addr", &self.peer_addr)
            .field("local_cid", &self.local_cid)
            .field("peer_cid", &self.peer_cid)
            .field(
                "negotiated_alpn",
                &String::from_utf8_lossy(&self.negotiated_alpn),
            )
            .finish_non_exhaustive()
    }
}

impl NativeQuicUdpConnection {
    /// Transfer this authenticated connection and its socket to a managed driver.
    ///
    /// The handoff preserves the whole application handle, verified TLS state,
    /// packet-protection provider, negotiated transport parameters and ALPN,
    /// distinct local/peer connection IDs, original recovery clock, retained
    /// final handshake flight, and early application packets in their order.
    /// It performs no network I/O and starts no background task. Drive the
    /// returned endpoint from the caller's structured-concurrency scope.
    ///
    /// The supplied configuration controls managed scheduling and lifecycle.
    /// Its UDP and connection templates do not reconfigure the already bound
    /// socket or replace the authenticated connection's negotiated state.
    ///
    /// # Errors
    ///
    /// Returns the original owner with a typed reason when managed preflight
    /// refuses the context, configuration, role, clock, or connection state.
    pub fn into_managed(
        self,
        cx: &Cx,
        config: ManagedEndpointConfig,
    ) -> Result<ManagedQuicEndpoint, ManagedQuicHandoffError> {
        ManagedQuicEndpoint::from_authenticated_connection(cx, self, config)
    }

    /// The exact configuration retained by the already bound UDP endpoint.
    pub(crate) fn udp_config(&self) -> &QuicUdpEndpointConfig {
        self.endpoint.config()
    }

    pub(crate) fn into_managed_parts(self) -> NativeQuicUdpHandoffParts {
        let Self {
            connection,
            endpoint,
            protection,
            local_cid,
            peer_cid,
            peer_addr,
            negotiated_alpn,
            final_handshake_flight,
            early_one_rtt_packets,
            last_final_flight_retransmit,
            clock_origin,
        } = self;
        NativeQuicUdpHandoffParts {
            connection,
            endpoint,
            protection,
            local_cid,
            peer_cid,
            peer_addr,
            negotiated_alpn,
            final_handshake_flight,
            early_one_rtt_packets,
            last_final_flight_retransmit,
            clock_origin,
        }
    }

    pub(crate) fn from_managed_parts(parts: NativeQuicUdpHandoffParts) -> Self {
        let NativeQuicUdpHandoffParts {
            connection,
            endpoint,
            protection,
            local_cid,
            peer_cid,
            peer_addr,
            negotiated_alpn,
            final_handshake_flight,
            early_one_rtt_packets,
            last_final_flight_retransmit,
            clock_origin,
        } = parts;
        Self {
            connection,
            endpoint,
            protection,
            local_cid,
            peer_cid,
            peer_addr,
            negotiated_alpn,
            final_handshake_flight,
            early_one_rtt_packets,
            last_final_flight_retransmit,
            clock_origin,
        }
    }

    /// Complete a client handshake over `endpoint` and bind its authenticated
    /// state directly to a live application-data connection.
    pub async fn connect(
        cx: &Cx,
        endpoint: QuicUdpEndpoint,
        peer_addr: SocketAddr,
        mut driver: QuicHandshakeDriver,
        initial_dcid: ConnectionId,
        local_cid: ConnectionId,
        connection_config: NativeQuicConnectionConfig,
        required_alpn: &[u8],
    ) -> Result<Self, NativeQuicUdpConnectionError> {
        if cx.checkpoint().is_err() {
            return Err(NativeQuicUdpConnectionError::Cancelled);
        }
        let mut endpoint = endpoint;
        if let Err(error) = client_handshake_over_udp(
            cx,
            &mut endpoint,
            peer_addr,
            &mut driver,
            initial_dcid,
            local_cid,
        )
        .await
        {
            return if cx.checkpoint().is_err() {
                Err(NativeQuicUdpConnectionError::Cancelled)
            } else {
                Err(NativeQuicUdpConnectionError::Handshake(error))
            };
        }
        Self::from_completed_handshake(
            cx,
            endpoint,
            peer_addr,
            driver,
            local_cid,
            connection_config,
            required_alpn,
            StreamRole::Client,
            Vec::new(),
        )
    }

    /// Complete a server handshake over `endpoint` and bind its authenticated
    /// state directly to a live application-data connection.
    ///
    /// `initial_dcid` is the destination CID from the client's Initial packet;
    /// a multi-connection listener is responsible for inspecting and routing
    /// that first datagram before calling this single-connection API.
    pub async fn accept(
        cx: &Cx,
        endpoint: QuicUdpEndpoint,
        mut driver: QuicHandshakeDriver,
        initial_dcid: ConnectionId,
        local_cid: ConnectionId,
        connection_config: NativeQuicConnectionConfig,
        required_alpn: &[u8],
    ) -> Result<Self, NativeQuicUdpConnectionError> {
        if cx.checkpoint().is_err() {
            return Err(NativeQuicUdpConnectionError::Cancelled);
        }
        let mut endpoint = endpoint;
        let (peer_addr, early_one_rtt_packets) = match server_handshake_over_udp_with_early_data(
            cx,
            &mut endpoint,
            &mut driver,
            initial_dcid,
            local_cid,
        )
        .await
        {
            Ok(peer_addr) => peer_addr,
            Err(error) => {
                return if cx.checkpoint().is_err() {
                    Err(NativeQuicUdpConnectionError::Cancelled)
                } else {
                    Err(NativeQuicUdpConnectionError::Handshake(error))
                };
            }
        };
        Self::from_completed_handshake(
            cx,
            endpoint,
            peer_addr,
            driver,
            local_cid,
            connection_config,
            required_alpn,
            StreamRole::Server,
            early_one_rtt_packets,
        )
    }

    fn from_completed_handshake(
        cx: &Cx,
        endpoint: QuicUdpEndpoint,
        peer_addr: SocketAddr,
        driver: QuicHandshakeDriver,
        local_cid: ConnectionId,
        connection_config: NativeQuicConnectionConfig,
        required_alpn: &[u8],
        role: StreamRole,
        early_one_rtt_packets: Vec<ReceivedPacket>,
    ) -> Result<Self, NativeQuicUdpConnectionError> {
        let parts = Self::finish_authenticated_handshake(
            cx,
            driver,
            connection_config,
            required_alpn,
            role,
        )?;
        Ok(Self {
            connection: parts.connection,
            endpoint,
            protection: parts.protection,
            local_cid,
            peer_cid: parts.peer_cid,
            peer_addr,
            negotiated_alpn: parts.negotiated_alpn,
            final_handshake_flight: parts.final_handshake_flight,
            early_one_rtt_packets,
            last_final_flight_retransmit: None,
            clock_origin: Instant::now(),
        })
    }

    pub(crate) fn finish_authenticated_handshake(
        cx: &Cx,
        mut driver: QuicHandshakeDriver,
        connection_config: NativeQuicConnectionConfig,
        required_alpn: &[u8],
        role: StreamRole,
    ) -> Result<AuthenticatedQuicParts, NativeQuicUdpConnectionError> {
        if !driver.is_complete() || !driver.one_rtt_keys_installed() {
            return Err(NativeQuicUdpConnectionError::HandshakeIncomplete(
                "TLS did not complete with installed 1-RTT keys",
            ));
        }
        let negotiated_alpn = match driver.negotiated_alpn().map(<[u8]>::to_vec) {
            Some(negotiated) if negotiated == required_alpn => negotiated,
            negotiated => {
                return Err(NativeQuicUdpConnectionError::AlpnMismatch {
                    expected: required_alpn.to_vec(),
                    negotiated,
                });
            }
        };
        let peer_cid = driver.peer_connection_id().ok_or(
            NativeQuicUdpConnectionError::HandshakeIncomplete(
                "peer connection ID was not authenticated",
            ),
        )?;

        let local_parameters = TransportParameters::decode(driver.local_transport_parameters())
            .map_err(|error| {
                NativeQuicUdpConnectionError::TransportParameters(format!(
                    "local decode failed: {error}"
                ))
            })?;
        let peer_parameter_bytes = driver.peer_transport_parameters().ok_or(
            NativeQuicUdpConnectionError::HandshakeIncomplete(
                "peer transport parameters were not authenticated",
            ),
        )?;
        let peer_parameters =
            TransportParameters::decode(peer_parameter_bytes).map_err(|error| {
                NativeQuicUdpConnectionError::TransportParameters(format!(
                    "peer decode failed: {error}"
                ))
            })?;
        let bound =
            bind_transport_parameters(connection_config, &local_parameters, &peer_parameters);

        let mut connection = match role {
            StreamRole::Client => QuicConnection::client(bound.config),
            StreamRole::Server => QuicConnection::server(bound.config),
        };
        connection.inner_mut().set_remote_stream_limits(
            local_parameters.initial_max_streams_bidi.unwrap_or(0),
            local_parameters.initial_max_streams_uni.unwrap_or(0),
        );
        connection
            .inner_mut()
            .set_initial_stream_windows(bound.send_windows, bound.recv_windows);
        connection.begin_handshake(cx)?;
        connection.mark_handshake_keys_available(cx)?;
        connection.mark_app_keys_available(cx)?;
        if role == StreamRole::Client {
            // Reaching here means rustls/WebPKI completed the client handshake
            // for its configured ServerName and roots.
            connection.record_verified_server_identity();
        }
        connection.confirm_handshake(cx)?;

        // DATAGRAM admission must know this connection's exact packet budget
        // before the first flush (GH#66).
        connection.inner_mut().set_one_rtt_frame_budget(
            PROTECTED_1RTT_MAX_PACKET_BYTES.saturating_sub(protected_1rtt_packet_len(peer_cid, 0)),
        );
        let final_handshake_flight = driver.take_final_flight();
        let protection = AtpPacketProtection::from_provider(
            Box::new(driver.into_provider()),
            AtpPacketProtectionConfig::default(),
        );
        Ok(AuthenticatedQuicParts {
            connection,
            protection,
            peer_cid,
            negotiated_alpn,
            final_handshake_flight,
        })
    }

    /// Application-facing QUIC stream/datagram handle.
    #[must_use]
    pub fn connection(&self) -> &QuicConnection {
        &self.connection
    }

    /// Application-facing mutable QUIC stream/datagram handle.
    pub fn connection_mut(&mut self) -> &mut QuicConnection {
        &mut self.connection
    }

    /// UDP socket address owned by this connection.
    #[must_use]
    pub fn local_addr(&self) -> SocketAddr {
        self.endpoint.local_addr()
    }

    /// Authenticated peer UDP address.
    #[must_use]
    pub fn peer_addr(&self) -> SocketAddr {
        self.peer_addr
    }

    /// Connection ID the peer uses as its short-header destination.
    #[must_use]
    pub fn local_connection_id(&self) -> ConnectionId {
        self.local_cid
    }

    /// Authenticated peer source CID retained from the handshake.
    #[must_use]
    pub fn peer_connection_id(&self) -> ConnectionId {
        self.peer_cid
    }

    /// Exact ALPN admitted before application state was exposed.
    #[must_use]
    pub fn negotiated_alpn(&self) -> &[u8] {
        &self.negotiated_alpn
    }

    /// Protect and send a bounded batch of queued application frames.
    pub async fn flush(&mut self, cx: &Cx) -> Result<usize, NativeQuicUdpConnectionError> {
        if cx.checkpoint().is_err() {
            return Err(NativeQuicUdpConnectionError::Cancelled);
        }
        let now = Instant::now();
        let now_micros = self.instant_micros(now);
        let max_frame_bytes = PROTECTED_1RTT_MAX_PACKET_BYTES
            .saturating_sub(protected_1rtt_packet_len(self.peer_cid, 0));
        self.connection
            .inner_mut()
            .set_one_rtt_frame_budget(max_frame_bytes);
        let mut packets = Vec::new();

        for _ in 0..MAX_PACKETS_PER_FLUSH {
            let frames = generate_congestion_admitted_1rtt_frames(
                cx,
                self.connection.inner_mut(),
                max_frame_bytes,
            )?;
            if frames.is_empty() {
                break;
            }
            let mut payload = BytesMut::new();
            super::connection::NativeQuicConnection::encode_frames(&frames, &mut payload)?;
            let assembled = assemble_protected_1rtt_packet(
                cx,
                self.peer_cid,
                self.connection.inner_mut(),
                &mut self.protection,
                &frames,
                payload.as_ref(),
                now_micros,
                frames.iter().any(is_ack_eliciting),
            )
            .await;
            let data = match assembled {
                Ok(data) => data,
                Err(error) => {
                    self.connection
                        .inner_mut()
                        .on_generated_frames_dropped(&frames)?;
                    return Err(error.into());
                }
            };
            packets.push(OutgoingPacket {
                dst_addr: self.peer_addr,
                data,
                send_time: Some(now),
            });
        }

        if packets.is_empty() {
            return Ok(0);
        }
        let expected = packets.len();
        let report = self.endpoint.send_batch(cx, &packets).await?;
        if report.packets_processed != expected || report.error.is_some() {
            return Err(NativeQuicUdpConnectionError::BatchSend(
                report.error.unwrap_or_else(|| {
                    format!(
                        "sent {} of {expected} protected packets",
                        report.packets_processed
                    )
                }),
            ));
        }
        Ok(expected)
    }

    /// Receive at most one bounded UDP batch, deliver authenticated 1-RTT
    /// payloads, service a due loss timer, and flush resulting ACK/application
    /// frames. A quiet timeout is reported as progress rather than an error so
    /// callers can compose their own explicit drive loop and cancellation scope.
    pub async fn drive_io_once(
        &mut self,
        cx: &Cx,
        receive_timeout: Duration,
    ) -> Result<NativeQuicUdpIoProgress, NativeQuicUdpConnectionError> {
        if cx.checkpoint().is_err() {
            return Err(NativeQuicUdpConnectionError::Cancelled);
        }
        let mut progress = NativeQuicUdpIoProgress::default();
        let received = if self.early_one_rtt_packets.is_empty() {
            let bounded_wait = self.receive_wait_duration(cx, receive_timeout)?;
            if bounded_wait.is_zero() {
                progress.receive_timed_out = true;
                self.service_due_loss_timer(cx)?;
                progress.packets_sent = self.flush(cx).await?;
                return Ok(progress);
            }
            match timeout(
                cx.now(),
                bounded_wait,
                self.endpoint.receive_batch(cx, RECEIVE_BATCH_SIZE),
            )
            .await
            {
                Ok(Ok(packets)) => packets,
                Ok(Err(error)) => return Err(error.into()),
                Err(_) => {
                    progress.receive_timed_out = true;
                    self.service_due_loss_timer(cx)?;
                    progress.packets_sent = self.flush(cx).await?;
                    return Ok(progress);
                }
            }
        } else {
            let early = std::mem::take(&mut self.early_one_rtt_packets);
            progress.early_packets_replayed = early.len();
            early
        };

        for packet in received {
            if packet.src_addr != self.peer_addr {
                progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                continue;
            }
            if packet.data.first().is_some_and(|byte| byte & 0x80 != 0) {
                progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                if !self.final_handshake_flight.is_empty()
                    && self.last_final_flight_retransmit.is_none_or(|last| {
                        packet.receive_time.saturating_duration_since(last)
                            >= FINAL_HANDSHAKE_FLIGHT_RESEND_INTERVAL
                    })
                {
                    let report = self
                        .endpoint
                        .send_batch(cx, &self.final_handshake_flight)
                        .await?;
                    if report.packets_processed != self.final_handshake_flight.len()
                        || report.error.is_some()
                    {
                        return Err(NativeQuicUdpConnectionError::BatchSend(
                            report.error.unwrap_or_else(|| {
                                "final handshake flight was only partially retransmitted"
                                    .to_string()
                            }),
                        ));
                    }
                    progress.handshake_flights_retransmitted =
                        progress.handshake_flights_retransmitted.saturating_add(1);
                    self.last_final_flight_retransmit = Some(packet.receive_time);
                }
                continue;
            }

            // Only the header-protection-invariant prefix is readable before
            // the peer's HP key unmasks the packet number (RFC 9001 §5.4);
            // `unprotect_1rtt_packet` does the unmask + AEAD in RFC order.
            let Ok(ProtectedHeaderPrefix::Short { dst_cid, .. }) =
                ProtectedHeaderPrefix::decode(&packet.data, self.local_cid.len())
            else {
                progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                continue;
            };
            if dst_cid != self.local_cid {
                progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                continue;
            }
            let unprotected =
                match unprotect_1rtt_packet(cx, self.local_cid, &mut self.protection, &packet.data)
                    .await
                {
                    Ok(unprotected) => unprotected,
                    Err(ConnectionRouterError::Cancelled) => {
                        return Err(NativeQuicUdpConnectionError::Cancelled);
                    }
                    Err(_) => {
                        progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                        continue;
                    }
                };
            let header = unprotected.header;
            let plaintext = unprotected.plaintext;
            self.connection
                .inner_mut()
                .on_datagram_received(cx, packet.data.len() as u64)?;
            let now_micros = self.instant_micros(packet.receive_time);
            if let Err(error) = self.connection.inner_mut().process_packet_payload(
                cx,
                PacketNumberSpace::ApplicationData,
                header.packet_number,
                &plaintext,
                now_micros,
            ) {
                if error.is_stream_reassembly_backpressure() {
                    // Do not ACK or park ahead of the packet that fills the
                    // hole. Reliable frames can return under a fresh number.
                    progress.packets_dropped = progress.packets_dropped.saturating_add(1);
                    continue;
                }
                return Err(error.into());
            }
            progress.packets_received = progress.packets_received.saturating_add(1);
        }

        self.service_due_loss_timer(cx)?;
        progress.packets_sent = self.flush(cx).await?;
        Ok(progress)
    }

    fn instant_micros(&self, instant: Instant) -> u64 {
        instant
            .checked_duration_since(self.clock_origin)
            .unwrap_or(Duration::ZERO)
            .as_micros()
            .min(u128::from(u64::MAX)) as u64
    }

    fn receive_wait_duration(
        &mut self,
        cx: &Cx,
        requested: Duration,
    ) -> Result<Duration, NativeQuicUdpConnectionError> {
        let now = Instant::now();
        let now_micros = self.instant_micros(now);
        let Some(deadline_micros) = self
            .connection
            .inner_mut()
            .pto_deadline_micros(cx, now_micros)?
        else {
            return Ok(requested);
        };
        Ok(requested.min(Duration::from_micros(
            deadline_micros.saturating_sub(now_micros),
        )))
    }

    fn service_due_loss_timer(&mut self, cx: &Cx) -> Result<(), NativeQuicUdpConnectionError> {
        let now_micros = self.instant_micros(Instant::now());
        let Some(deadline) = self
            .connection
            .inner_mut()
            .pto_deadline_micros(cx, now_micros)?
        else {
            return Ok(());
        };
        if deadline <= now_micros {
            self.connection.inner_mut().on_loss_timeout_expired(
                cx,
                PacketNumberSpace::ApplicationData,
                now_micros,
            )?;
        }
        Ok(())
    }
}

/// Connection configuration after the authenticated transport parameters of
/// both endpoints have been applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct BoundTransportParameters {
    /// Stream counts, connection-level limits and the DATAGRAM cap, each the
    /// smaller of the configured value and the negotiated one. `send_window`
    /// and `recv_window` stay the configured per-stream caps.
    config: NativeQuicConnectionConfig,
    /// Per-type initial send windows: the peer's `initial_max_stream_data_*`
    /// values capped by `config.send_window` (RFC 9000 §18.2).
    send_windows: StreamWindows,
    /// Per-type initial receive windows: this endpoint's
    /// `initial_max_stream_data_*` values capped by `config.recv_window`.
    recv_windows: StreamWindows,
}

/// Bind both endpoints' transport parameters onto the connection configuration.
///
/// Stream windows are kept per stream type. A locally opened bidirectional
/// stream sends against the peer's `initial_max_stream_data_bidi_remote` and
/// receives against the local `initial_max_stream_data_bidi_local`; a
/// peer-opened bidirectional stream uses the mirrored pair; unidirectional
/// streams use `initial_max_stream_data_uni` on their data-carrying side. A
/// parameter an endpoint omits is zero for that type only, so a peer that does
/// not advertise a unidirectional window still gets its full bidirectional
/// windows.
fn bind_transport_parameters(
    mut config: NativeQuicConnectionConfig,
    local: &TransportParameters,
    peer: &TransportParameters,
) -> BoundTransportParameters {
    config.max_local_bidi = config
        .max_local_bidi
        .min(peer.initial_max_streams_bidi.unwrap_or(0));
    config.max_local_uni = config
        .max_local_uni
        .min(peer.initial_max_streams_uni.unwrap_or(0));
    config.connection_send_limit = config
        .connection_send_limit
        .min(peer.initial_max_data.unwrap_or(0));
    config.connection_recv_limit = config
        .connection_recv_limit
        .min(local.initial_max_data.unwrap_or(0));

    let send_cap = config.send_window;
    let send_windows = StreamWindows {
        local_bidi: send_cap.min(peer.initial_max_stream_data_bidi_remote.unwrap_or(0)),
        remote_bidi: send_cap.min(peer.initial_max_stream_data_bidi_local.unwrap_or(0)),
        uni: send_cap.min(peer.initial_max_stream_data_uni.unwrap_or(0)),
    };
    let recv_cap = config.recv_window;
    let recv_windows = StreamWindows {
        local_bidi: recv_cap.min(local.initial_max_stream_data_bidi_local.unwrap_or(0)),
        remote_bidi: recv_cap.min(local.initial_max_stream_data_bidi_remote.unwrap_or(0)),
        uni: recv_cap.min(local.initial_max_stream_data_uni.unwrap_or(0)),
    };
    config.max_datagram_frame_size = config.max_datagram_frame_size.min(
        peer.max_datagram_frame_size
            .and_then(|value| usize::try_from(value).ok())
            .unwrap_or(0),
    );
    BoundTransportParameters {
        config,
        send_windows,
        recv_windows,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bytes::Bytes;
    use crate::net::atp::protocol::quic_frames::QuicFrame;
    use crate::net::atp::protocol::varint::VarInt;
    use crate::net::quic_native::connection::NativeQuicConnection;
    use crate::net::quic_native::connection_manager::{ConnectionRouter, RoutingResult};
    use crate::net::quic_native::handshake_driver::tests::{
        CA_CERT_PEM, LEAF_CERT_PEM, leaf_key, parse_one_cert,
    };
    use crate::net::quic_native::handshake_driver::{client_config, server_config};
    use crate::net::quic_native::{QuicConnectionState, StreamId};
    use futures_lite::future::{block_on, zip};
    use rustls::pki_types::ServerName;

    /// A peer that omits `initial_max_stream_data_uni` (as the managed-quiet
    /// loopback pair does) must keep full bidirectional windows. The previous
    /// binding took the minimum over all three per-type parameters with an
    /// omitted one counted as zero, which left every stream with zero send
    /// credit and failed the first `write_stream` with `Flow(Exhausted)`.
    #[test]
    fn bind_transport_parameters_keeps_bidi_windows_when_uni_is_omitted() {
        let config = NativeQuicConnectionConfig {
            max_local_bidi: 4,
            max_local_uni: 4,
            send_window: 1 << 18,
            recv_window: 1 << 18,
            connection_send_limit: 1 << 20,
            connection_recv_limit: 1 << 20,
            ..NativeQuicConnectionConfig::default()
        };
        let peer = TransportParameters {
            initial_max_data: Some(1 << 19),
            initial_max_stream_data_bidi_local: Some(1_000),
            initial_max_stream_data_bidi_remote: Some(2_000),
            initial_max_stream_data_uni: None,
            initial_max_streams_bidi: Some(2),
            ..TransportParameters::default()
        };
        let local = TransportParameters {
            initial_max_data: Some(1 << 21),
            initial_max_stream_data_bidi_local: Some(3_000),
            initial_max_stream_data_bidi_remote: Some(1 << 20),
            initial_max_stream_data_uni: None,
            initial_max_streams_bidi: Some(4),
            ..TransportParameters::default()
        };

        let bound = bind_transport_parameters(config, &local, &peer);

        // Send side: a locally opened bidi stream sends against the peer's
        // `bidi_remote`, a peer-opened one against the peer's `bidi_local`.
        assert_eq!(
            bound.send_windows,
            StreamWindows {
                local_bidi: 2_000,
                remote_bidi: 1_000,
                uni: 0,
            }
        );
        // Receive side mirrors the mapping and is capped by the configured
        // window: the local `bidi_remote` of 1 MiB clamps to 256 KiB.
        assert_eq!(
            bound.recv_windows,
            StreamWindows {
                local_bidi: 3_000,
                remote_bidi: 1 << 18,
                uni: 0,
            }
        );
        assert_eq!(bound.config.send_window, 1 << 18);
        assert_eq!(bound.config.recv_window, 1 << 18);
        assert_eq!(bound.config.max_local_bidi, 2);
        assert_eq!(bound.config.connection_send_limit, 1 << 19);
        assert_eq!(bound.config.connection_recv_limit, 1 << 20);
        assert_eq!(bound.config.max_datagram_frame_size, 0);

        // The windows reach the stream table: a client opening stream 0 gets
        // the peer's `bidi_remote` credit, not zero.
        let mut connection = QuicConnection::client(bound.config);
        connection
            .inner_mut()
            .set_initial_stream_windows(bound.send_windows, bound.recv_windows);
        let cx = Cx::for_testing();
        connection.begin_handshake(&cx).unwrap();
        connection.mark_handshake_keys_available(&cx).unwrap();
        connection.mark_app_keys_available(&cx).unwrap();
        // The production handoff records the rustls-verified server identity
        // before confirming; a client cannot confirm without it.
        connection.record_verified_server_identity();
        connection.confirm_handshake(&cx).unwrap();
        let stream = connection.open_bidi_stream(&cx).unwrap();
        assert_eq!(stream, StreamId(0));
        assert_eq!(
            connection
                .inner()
                .streams()
                .stream_send_credit_remaining(stream),
            2_000
        );
        assert_eq!(
            connection
                .inner()
                .streams()
                .stream(stream)
                .unwrap()
                .recv_credit
                .limit(),
            3_000
        );
    }

    fn assert_reassembly_recovered(cx: &Cx, connection: &mut NativeQuicConnection) {
        assert_eq!(connection.state(), QuicConnectionState::Established);
        assert_eq!(connection.datagrams_received(), 1);
        assert_eq!(connection.recv_datagram().as_deref(), Some(&b"once"[..]));
        assert!(connection.recv_datagram().is_none());
        let mut received = Vec::new();
        while received.len() < 2 {
            let bytes = connection.read_stream_bytes(cx, StreamId(0), 2).unwrap();
            assert!(!bytes.is_empty());
            received.extend_from_slice(&bytes);
        }
        assert_eq!(received, b"hx");
        assert!(!connection.is_stream_read_eof(StreamId(0)).unwrap());
        assert_eq!(
            connection
                .streams()
                .stream(StreamId(0))
                .unwrap()
                .recv_offset,
            2
        );
    }

    #[test]
    fn reassembly_backpressure_recovers_in_udp_owner_and_authenticated_router() {
        block_on(async {
            for routed in [false, true] {
                let cx = Cx::for_testing();
                let config = NativeQuicConnectionConfig::default();
                let parameters = TransportParameters {
                    initial_max_data: Some(config.connection_recv_limit),
                    initial_max_stream_data_bidi_local: Some(config.recv_window),
                    initial_max_stream_data_bidi_remote: Some(config.recv_window),
                    initial_max_stream_data_uni: Some(config.recv_window),
                    initial_max_streams_bidi: Some(config.max_local_bidi),
                    max_datagram_frame_size: Some(1200),
                    ..TransportParameters::default()
                };
                let mut parameters_bytes = Vec::new();
                parameters.encode(&mut parameters_bytes).unwrap();
                let client_socket = QuicUdpEndpoint::bind(
                    &cx,
                    "127.0.0.1:0".parse().unwrap(),
                    QuicUdpEndpointConfig::default(),
                )
                .await
                .unwrap();
                let server_socket = QuicUdpEndpoint::bind(
                    &cx,
                    "127.0.0.1:0".parse().unwrap(),
                    QuicUdpEndpointConfig::default(),
                )
                .await
                .unwrap();
                let address = server_socket.local_addr();
                let alpn = b"reassembly-test";
                let client_tls =
                    client_config(vec![parse_one_cert(CA_CERT_PEM)], vec![alpn.to_vec()]).unwrap();
                let server_tls = server_config(
                    vec![parse_one_cert(LEAF_CERT_PEM)],
                    leaf_key(),
                    vec![alpn.to_vec()],
                )
                .unwrap();
                let initial_cid = ConnectionId::new(b"initial").unwrap();
                let server_cid = ConnectionId::new(b"server").unwrap();
                let (client, server) = zip(
                    NativeQuicUdpConnection::connect(
                        &cx,
                        client_socket,
                        address,
                        QuicHandshakeDriver::client(
                            client_tls,
                            ServerName::try_from("localhost").unwrap(),
                            parameters_bytes.clone(),
                        )
                        .unwrap(),
                        initial_cid,
                        ConnectionId::new(b"client").unwrap(),
                        config,
                        alpn,
                    ),
                    NativeQuicUdpConnection::accept(
                        &cx,
                        server_socket,
                        QuicHandshakeDriver::server(server_tls, parameters_bytes).unwrap(),
                        initial_cid,
                        server_cid,
                        config,
                        alpn,
                    ),
                )
                .await;
                let mut client = client.unwrap();
                let mut server = server.unwrap();
                assert!(server.early_one_rtt_packets.is_empty());
                let id = StreamId(0);
                let connection = server.connection.inner_mut();
                connection.accept_remote_stream(&cx, id).unwrap();
                // Seed only the bounded capacity precondition. All three
                // admission/recovery packets use real TLS keys and UDP below.
                for fragment in 0..4095u64 {
                    connection
                        .receive_stream_bytes(
                            &cx,
                            id,
                            1 + fragment * 2,
                            Bytes::from_static(b"x"),
                            false,
                        )
                        .unwrap();
                }
                connection
                    .generate_frames(&cx, PacketNumberSpace::ApplicationData, 65535)
                    .unwrap();
                let overflow = vec![
                    QuicFrame::Datagram {
                        data: Bytes::from_static(b"once"),
                    },
                    QuicFrame::Stream {
                        stream_id: VarInt(id.0),
                        offset: Some(VarInt(8191)),
                        data: Bytes::from_static(b"z"),
                        fin: true,
                    },
                ];
                let repair = vec![QuicFrame::Stream {
                    stream_id: VarInt(id.0),
                    offset: Some(VarInt(0)),
                    data: Bytes::from_static(b"h"),
                    fin: false,
                }];
                let mut packets = Vec::new();
                for frames in [&overflow, &repair, &overflow] {
                    let mut payload = BytesMut::new();
                    NativeQuicConnection::encode_frames(frames, &mut payload).unwrap();
                    let data = assemble_protected_1rtt_packet(
                        &cx,
                        server_cid,
                        client.connection.inner_mut(),
                        &mut client.protection,
                        frames,
                        &payload,
                        1,
                        true,
                    )
                    .await
                    .unwrap();
                    packets.push(OutgoingPacket {
                        dst_addr: address,
                        data,
                        send_time: None,
                    });
                }
                let sent = client.endpoint.send_batch(&cx, &packets).await.unwrap();
                assert_eq!(sent.packets_processed, 3);
                assert!(sent.error.is_none());
                if routed {
                    let (mut router, mut endpoint, early) =
                        ConnectionRouter::from_authenticated_parts(
                            server.into_managed_parts(),
                            config,
                            1,
                            None,
                            Instant::now(),
                        );
                    assert!(early.is_empty());
                    let mut outcomes = Vec::new();
                    while outcomes.len() < 3 {
                        let packets = timeout(
                            crate::time::wall_now(),
                            Duration::from_secs(10),
                            endpoint.receive_batch(&cx, 3 - outcomes.len()),
                        )
                        .await
                        .unwrap()
                        .unwrap();
                        for packet in packets {
                            outcomes.push(router.route_packet(&cx, packet).await.unwrap());
                        }
                    }
                    assert!(matches!(&outcomes[0], RoutingResult::Drop { reason }
                        if reason == "stream reassembly backpressure"));
                    assert!(matches!(&outcomes[1], RoutingResult::Routed { .. }));
                    assert!(matches!(&outcomes[2], RoutingResult::Routed { .. }));
                    let connection = router.connection_mut_for_testing(&cx, server_cid).unwrap();
                    assert_reassembly_recovered(&cx, connection);
                    let frames = connection
                        .generate_frames(&cx, PacketNumberSpace::ApplicationData, 65535)
                        .unwrap();
                    assert!(frames.iter().any(|frame| matches!(frame,
                        QuicFrame::Ack { largest_acknowledged, first_ack_range, ack_ranges, .. }
                            if largest_acknowledged.value() == 2 && first_ack_range.value() == 1 && ack_ranges.is_empty()
                    )), "only packets 1 and 2 were admitted");
                } else {
                    let mut received = 0;
                    let mut dropped = 0;
                    for _ in 0..3 {
                        let progress = server
                            .drive_io_once(&cx, Duration::from_secs(10))
                            .await
                            .unwrap();
                        received += progress.packets_received;
                        dropped += progress.packets_dropped;
                        if received + dropped == 3 {
                            break;
                        }
                    }
                    assert_eq!((received, dropped), (2, 1));
                    assert_reassembly_recovered(&cx, server.connection.inner_mut());
                }
            }
        });
    }
}