@@ -23,9 +23,7 @@ use crate::{
23
23
crypto:: { self , Keys , UnsupportedVersion } ,
24
24
frame,
25
25
packet:: { Header , Packet , PacketDecodeError , PacketNumber , PartialDecode } ,
26
- shared:: {
27
- ConnectionEvent , ConnectionEventInner , ConnectionId , EcnCodepoint , EndpointEvent , IssuedCid ,
28
- } ,
26
+ shared:: { ConnectionEvent , ConnectionId , EcnCodepoint , EndpointEvent , IssuedCid } ,
29
27
transport_parameters:: TransportParameters ,
30
28
ResetToken , RetryToken , Side , Transmit , TransportConfig , TransportError , INITIAL_MTU ,
31
29
MAX_CID_SIZE , MIN_INITIAL_SIZE , RESET_TOKEN_SIZE ,
@@ -81,25 +79,23 @@ impl Endpoint {
81
79
82
80
/// Process events from [`Connection`]s that have returned `true` from [`Connection::poll_endpoint_events`]
83
81
///
84
- /// May return a `ConnectionEvent` for any `Connection`. Call until `None` is returned.
85
- pub fn handle_events ( & mut self ) -> Option < ( ConnectionHandle , ConnectionEvent ) > {
82
+ /// May return the [`ConnectionHandle`] of a [`Connection`] for which
83
+ /// [`Connection::handle_events`] must be called. Call until `None` is returned.
84
+ pub fn handle_events ( & mut self ) -> Option < ConnectionHandle > {
86
85
while let Ok ( ( ch, event) ) = self . event_recv . try_recv ( ) {
87
- if let Some ( response ) = self . handle_event ( ch, event) {
88
- return Some ( ( ch , response ) ) ;
86
+ if self . handle_event ( ch, event) {
87
+ return Some ( ch ) ;
89
88
}
90
89
}
91
90
None
92
91
}
93
92
94
- fn handle_event (
95
- & mut self ,
96
- ch : ConnectionHandle ,
97
- event : EndpointEvent ,
98
- ) -> Option < ConnectionEvent > {
93
+ fn handle_event ( & mut self , ch : ConnectionHandle , event : EndpointEvent ) -> bool {
99
94
use EndpointEvent :: * ;
100
95
match event {
101
96
NeedIdentifiers ( n) => {
102
- return Some ( self . send_new_identifiers ( ch, n) ) ;
97
+ self . send_new_identifiers ( ch, n) ;
98
+ return true ;
103
99
}
104
100
ResetToken ( remote, token) => {
105
101
if let Some ( old) = self . connections [ ch] . reset_token . replace ( ( remote, token) ) {
@@ -114,7 +110,8 @@ impl Endpoint {
114
110
trace ! ( "peer retired CID {}: {}" , seq, cid) ;
115
111
self . index . retire ( & cid) ;
116
112
if allow_more_cids {
117
- return Some ( self . send_new_identifiers ( ch, 1 ) ) ;
113
+ self . send_new_identifiers ( ch, 1 ) ;
114
+ return true ;
118
115
}
119
116
}
120
117
}
@@ -129,7 +126,27 @@ impl Endpoint {
129
126
}
130
127
}
131
128
}
132
- None
129
+ false
130
+ }
131
+
132
+ #[ cfg( test) ]
133
+ pub ( crate ) fn decode_packet (
134
+ & self ,
135
+ datagram : BytesMut ,
136
+ ) -> Result < PartialDecode , PacketDecodeError > {
137
+ PartialDecode :: new (
138
+ datagram,
139
+ self . local_cid_generator . cid_len ( ) ,
140
+ & self . config . supported_versions ,
141
+ self . config . grease_quic_bit ,
142
+ )
143
+ . map ( |( packet, rest) | {
144
+ assert ! (
145
+ rest. is_none( ) ,
146
+ "capturing decoded coalesced packets in tests is unimplemented"
147
+ ) ;
148
+ packet
149
+ } )
133
150
}
134
151
135
152
/// Process an incoming UDP datagram
@@ -196,16 +213,16 @@ impl Endpoint {
196
213
197
214
let addresses = FourTuple { remote, local_ip } ;
198
215
if let Some ( ch) = self . index . get ( & addresses, & first_decode) {
199
- return Some ( DatagramEvent :: ConnectionEvent (
200
- ch ,
201
- ConnectionEvent ( ConnectionEventInner :: Datagram {
216
+ _ = self . connections [ ch . 0 ]
217
+ . events
218
+ . send ( ConnectionEvent :: Datagram {
202
219
now,
203
220
remote : addresses. remote ,
204
221
ecn,
205
222
first_decode,
206
223
remaining,
207
- } ) ,
208
- ) ) ;
224
+ } ) ;
225
+ return Some ( DatagramEvent :: ConnectionEvent ( ch ) ) ;
209
226
}
210
227
211
228
//
@@ -375,7 +392,7 @@ impl Endpoint {
375
392
Ok ( ( ch, conn) )
376
393
}
377
394
378
- fn send_new_identifiers ( & mut self , ch : ConnectionHandle , num : u64 ) -> ConnectionEvent {
395
+ fn send_new_identifiers ( & mut self , ch : ConnectionHandle , num : u64 ) {
379
396
let mut ids = vec ! [ ] ;
380
397
for _ in 0 ..num {
381
398
let id = self . new_cid ( ch) ;
@@ -389,7 +406,9 @@ impl Endpoint {
389
406
reset_token : ResetToken :: new ( & * self . config . reset_key , & id) ,
390
407
} ) ;
391
408
}
392
- ConnectionEvent ( ConnectionEventInner :: NewIdentifiers ( ids) )
409
+ _ = self . connections [ ch]
410
+ . events
411
+ . send ( ConnectionEvent :: NewIdentifiers ( ids) ) ;
393
412
}
394
413
395
414
/// Generate a connection ID for `ch`
@@ -603,6 +622,7 @@ impl Endpoint {
603
622
) -> Connection {
604
623
let mut rng_seed = [ 0 ; 32 ] ;
605
624
self . rng . fill_bytes ( & mut rng_seed) ;
625
+ let ( send, recv) = mpsc:: channel ( ) ;
606
626
let conn = Connection :: new (
607
627
self . config . clone ( ) ,
608
628
server_config,
@@ -619,6 +639,7 @@ impl Endpoint {
619
639
self . allow_mtud ,
620
640
rng_seed,
621
641
EndpointEvents :: new ( ch, self . event_send . clone ( ) ) ,
642
+ recv,
622
643
) ;
623
644
624
645
let id = self . connections . insert ( ConnectionMeta {
@@ -627,6 +648,7 @@ impl Endpoint {
627
648
loc_cids : iter:: once ( ( 0 , loc_cid) ) . collect ( ) ,
628
649
addresses,
629
650
reset_token : None ,
651
+ events : send,
630
652
} ) ;
631
653
debug_assert_eq ! ( id, ch. 0 , "connection handle allocation out of sync" ) ;
632
654
@@ -836,6 +858,7 @@ pub(crate) struct ConnectionMeta {
836
858
/// Reset token provided by the peer for the CID we're currently sending to, and the address
837
859
/// being sent to
838
860
reset_token : Option < ( SocketAddr , ResetToken ) > ,
861
+ events : mpsc:: Sender < ConnectionEvent > ,
839
862
}
840
863
841
864
/// Internal identifier for a `Connection` currently associated with an endpoint
@@ -864,8 +887,8 @@ impl IndexMut<ConnectionHandle> for Slab<ConnectionMeta> {
864
887
/// Event resulting from processing a single datagram
865
888
#[ allow( clippy:: large_enum_variant) ] // Not passed around extensively
866
889
pub enum DatagramEvent {
867
- /// The datagram is redirected to its `Connection`
868
- ConnectionEvent ( ConnectionHandle , ConnectionEvent ) ,
890
+ /// [`Connection::handle_events`] must be called on the associated [ `Connection`]
891
+ ConnectionEvent ( ConnectionHandle ) ,
869
892
/// The datagram has resulted in starting a new `Connection`
870
893
NewConnection ( ConnectionHandle , Connection ) ,
871
894
/// Response generated directly by the endpoint
0 commit comments