@@ -23,9 +23,7 @@ use crate::{
2323 crypto:: { self , Keys , UnsupportedVersion } ,
2424 frame,
2525 packet:: { Header , Packet , PacketDecodeError , PacketNumber , PartialDecode } ,
26- shared:: {
27- ConnectionEvent , ConnectionEventInner , ConnectionId , EcnCodepoint , EndpointEvent , IssuedCid ,
28- } ,
26+ shared:: { ConnectionEvent , ConnectionId , EcnCodepoint , EndpointEvent , IssuedCid } ,
2927 transport_parameters:: TransportParameters ,
3028 ResetToken , RetryToken , Side , Transmit , TransportConfig , TransportError , INITIAL_MTU ,
3129 MAX_CID_SIZE , MIN_INITIAL_SIZE , RESET_TOKEN_SIZE ,
@@ -81,25 +79,23 @@ impl Endpoint {
8179
8280 /// Process events from [`Connection`]s that have returned `true` from [`Connection::poll_endpoint_events`]
8381 ///
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 > {
8685 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 ) ;
8988 }
9089 }
9190 None
9291 }
9392
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 {
9994 use EndpointEvent :: * ;
10095 match event {
10196 NeedIdentifiers ( n) => {
102- return Some ( self . send_new_identifiers ( ch, n) ) ;
97+ self . send_new_identifiers ( ch, n) ;
98+ return true ;
10399 }
104100 ResetToken ( remote, token) => {
105101 if let Some ( old) = self . connections [ ch] . reset_token . replace ( ( remote, token) ) {
@@ -114,7 +110,8 @@ impl Endpoint {
114110 trace ! ( "peer retired CID {}: {}" , seq, cid) ;
115111 self . index . retire ( & cid) ;
116112 if allow_more_cids {
117- return Some ( self . send_new_identifiers ( ch, 1 ) ) ;
113+ self . send_new_identifiers ( ch, 1 ) ;
114+ return true ;
118115 }
119116 }
120117 }
@@ -129,7 +126,27 @@ impl Endpoint {
129126 }
130127 }
131128 }
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+ } )
133150 }
134151
135152 /// Process an incoming UDP datagram
@@ -196,16 +213,16 @@ impl Endpoint {
196213
197214 let addresses = FourTuple { remote, local_ip } ;
198215 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 {
202219 now,
203220 remote : addresses. remote ,
204221 ecn,
205222 first_decode,
206223 remaining,
207- } ) ,
208- ) ) ;
224+ } ) ;
225+ return Some ( DatagramEvent :: ConnectionEvent ( ch ) ) ;
209226 }
210227
211228 //
@@ -375,7 +392,7 @@ impl Endpoint {
375392 Ok ( ( ch, conn) )
376393 }
377394
378- fn send_new_identifiers ( & mut self , ch : ConnectionHandle , num : u64 ) -> ConnectionEvent {
395+ fn send_new_identifiers ( & mut self , ch : ConnectionHandle , num : u64 ) {
379396 let mut ids = vec ! [ ] ;
380397 for _ in 0 ..num {
381398 let id = self . new_cid ( ch) ;
@@ -389,7 +406,9 @@ impl Endpoint {
389406 reset_token : ResetToken :: new ( & * self . config . reset_key , & id) ,
390407 } ) ;
391408 }
392- ConnectionEvent ( ConnectionEventInner :: NewIdentifiers ( ids) )
409+ _ = self . connections [ ch]
410+ . events
411+ . send ( ConnectionEvent :: NewIdentifiers ( ids) ) ;
393412 }
394413
395414 /// Generate a connection ID for `ch`
@@ -603,6 +622,7 @@ impl Endpoint {
603622 ) -> Connection {
604623 let mut rng_seed = [ 0 ; 32 ] ;
605624 self . rng . fill_bytes ( & mut rng_seed) ;
625+ let ( send, recv) = mpsc:: channel ( ) ;
606626 let conn = Connection :: new (
607627 self . config . clone ( ) ,
608628 server_config,
@@ -619,6 +639,7 @@ impl Endpoint {
619639 self . allow_mtud ,
620640 rng_seed,
621641 EndpointEvents :: new ( ch, self . event_send . clone ( ) ) ,
642+ recv,
622643 ) ;
623644
624645 let id = self . connections . insert ( ConnectionMeta {
@@ -627,6 +648,7 @@ impl Endpoint {
627648 loc_cids : iter:: once ( ( 0 , loc_cid) ) . collect ( ) ,
628649 addresses,
629650 reset_token : None ,
651+ events : send,
630652 } ) ;
631653 debug_assert_eq ! ( id, ch. 0 , "connection handle allocation out of sync" ) ;
632654
@@ -836,6 +858,7 @@ pub(crate) struct ConnectionMeta {
836858 /// Reset token provided by the peer for the CID we're currently sending to, and the address
837859 /// being sent to
838860 reset_token : Option < ( SocketAddr , ResetToken ) > ,
861+ events : mpsc:: Sender < ConnectionEvent > ,
839862}
840863
841864/// Internal identifier for a `Connection` currently associated with an endpoint
@@ -864,8 +887,8 @@ impl IndexMut<ConnectionHandle> for Slab<ConnectionMeta> {
864887/// Event resulting from processing a single datagram
865888#[ allow( clippy:: large_enum_variant) ] // Not passed around extensively
866889pub 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 ) ,
869892 /// The datagram has resulted in starting a new `Connection`
870893 NewConnection ( ConnectionHandle , Connection ) ,
871894 /// Response generated directly by the endpoint
0 commit comments