@@ -8,27 +8,25 @@ use tokio::{
88} ;
99use tokio_util:: codec:: { FramedRead , FramedWrite } ;
1010use ts_http_util:: Client as _;
11- use ts_keys:: NodeKeyPair ;
11+ use ts_keys:: { NodeKeyPair , NodePublicKey } ;
1212use ts_packet:: PacketMut ;
13- use ts_transport:: { BatchRecvIter , BatchSendIter , PeerId , UnderlayTransport } ;
13+ use ts_transport:: { BatchRecvIter , BatchSendIter , UnderlayTransport } ;
1414use url:: Url ;
1515
1616use crate :: {
1717 Error , ServerConnInfo , frame,
1818 frame:: { ClientInfo , FrameType , PeerGone , Ping , RawFrame , ServerInfo , ServerKey } ,
19- peer_lookup:: PeerLookup ,
2019} ;
2120
2221type DefaultIo = ts_http_util:: Upgraded ;
2322
2423/// Type alias for the default derp client over upgraded HTTP on a tokio executor.
25- pub type DefaultClient < Lookup > = Client < DefaultIo , Lookup > ;
24+ pub type DefaultClient = Client < DefaultIo > ;
2625
27- /// Asynchronous DERP transport for a single DERP region .
28- pub struct Client < Io , Lookup > {
26+ /// Single-region DERP client .
27+ pub struct Client < Io > {
2928 read_conn : Mutex < FramedRead < ReadHalf < Io > , frame:: Codec > > ,
3029 write_conn : Mutex < FramedWrite < WriteHalf < Io > , frame:: Codec > > ,
31- peer_lookup : Lookup ,
3230}
3331
3432/// Establish and upgrade a http connection to the derp region.
@@ -56,18 +54,13 @@ pub async fn connect<'c>(
5654 Ok ( Some ( upgraded) )
5755}
5856
59- impl < Io , Lookup > Client < Io , Lookup >
57+ impl < Io > Client < Io >
6058where
6159 Io : AsyncRead + AsyncWrite ,
62- Lookup : PeerLookup ,
6360{
6461 /// Perform a derp handshake over the given transport and return a [`Client`].
6562 #[ tracing:: instrument( skip_all) ]
66- pub async fn handshake (
67- conn : Io ,
68- node_keypair : & NodeKeyPair ,
69- peer_lookup : Lookup ,
70- ) -> Result < Self , Error > {
63+ pub async fn handshake ( conn : Io , node_keypair : & NodeKeyPair ) -> Result < Self , Error > {
7164 let ( read_conn, write_conn) = tokio:: io:: split ( conn) ;
7265
7366 let mut fw = FramedWrite :: new ( write_conn, frame:: Codec ) ;
@@ -121,10 +114,15 @@ where
121114 Ok ( Self {
122115 read_conn : Mutex :: new ( fr) ,
123116 write_conn : Mutex :: new ( fw) ,
124- peer_lookup,
125117 } )
126118 }
127119
120+ /// Send a message to a nodekey on the derp server.
121+ pub async fn send_one ( & self , node_key : NodePublicKey , msg : & [ u8 ] ) -> Result < ( ) , Error > {
122+ self . send_frame_with_extra ( & frame:: SendPacket { dest : node_key } , msg)
123+ . await
124+ }
125+
128126 /// Send a frame to the derp server.
129127 pub async fn send_frame (
130128 & self ,
@@ -151,7 +149,7 @@ where
151149
152150 /// Waits for a single data packet from a peer to arrive via this DERP server and returns it.
153151 /// DERP control messages (KeepAlive, Ping, etc) are handled inline and are not returned.
154- pub async fn recv_one ( & self ) -> Result < ( PeerId , PacketMut ) , Error > {
152+ pub async fn recv_one ( & self ) -> Result < ( NodePublicKey , PacketMut ) , Error > {
155153 // DERP exchanges control messages (KeepAlives, Pings, etc) in-band with data messages
156154 // (SendPacket, RecvPacket, etc). The caller only cares about the payloads of data
157155 // messages, so we recv_one_raw() in a loop to handle any control messages while waiting
@@ -199,12 +197,8 @@ where
199197 }
200198 FrameType :: RecvPacket => {
201199 let ( recv, payload) = frame. as_type :: < frame:: RecvPacket > ( ) . unwrap ( ) ;
202- let Some ( id) = self . peer_lookup . key_to_id ( & recv. src ) else {
203- tracing:: trace!( src = %recv. src, "no known peer for node key" ) ;
204- continue ;
205- } ;
206200
207- return Ok ( ( id , payload. into ( ) ) ) ;
201+ return Ok ( ( recv . src , payload. into ( ) ) ) ;
208202 }
209203 t => {
210204 return Err ( Error :: UnexpectedRecvFrameType ( t) ) ;
@@ -214,29 +208,25 @@ where
214208 }
215209}
216210
217- impl < Lookup > Client < DefaultIo , Lookup >
218- where
219- Lookup : PeerLookup ,
220- {
211+ impl Client < DefaultIo > {
221212 /// Connect to and handshake with the derp server with the given URL over HTTP.
222213 pub async fn connect < ' c > (
223214 region : impl IntoIterator < Item = & ' c ServerConnInfo > ,
224215 node_keypair : & NodeKeyPair ,
225- lookup : Lookup ,
226216 ) -> Result < Self , Error > {
227217 let conn = connect ( region) . await ?. unwrap ( ) ;
228218
229- Client :: handshake ( conn, node_keypair, lookup ) . await
219+ Client :: handshake ( conn, node_keypair) . await
230220 }
231221}
232222
233- impl < Io , Lookup > fmt:: Debug for Client < Io , Lookup > {
223+ impl < Io > fmt:: Debug for Client < Io > {
234224 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
235225 fmt:: Display :: fmt ( self , f)
236226 }
237227}
238228
239- impl < Io , Lookup > fmt:: Display for Client < Io , Lookup > {
229+ impl < Io > fmt:: Display for Client < Io > {
240230 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
241231 f. debug_tuple ( "Client" ) . finish ( )
242232 }
@@ -291,36 +281,27 @@ fn decrypt_server_info(
291281 Ok ( sip)
292282}
293283
294- impl < Io , Lookup > UnderlayTransport for Client < Io , Lookup >
284+ impl < Io > UnderlayTransport for Client < Io >
295285where
296286 Io : AsyncRead + AsyncWrite + Send ,
297- Lookup : PeerLookup ,
298287{
299- type PeerKey = PeerId ;
288+ type PeerKey = NodePublicKey ;
300289 type Error = Error ;
301290
302- #[ tracing:: instrument( fields( %self ) ) ]
303- async fn recv ( & self ) -> impl BatchRecvIter < Self :: PeerKey , Error = Self :: Error > {
304- [ self . recv_one ( ) . await . map ( |( k, pkt) | ( k, [ pkt] ) ) ]
305- }
306-
307- /// Send a batch of packets to a peer via this DERP server.
308291 async fn send (
309292 & self ,
310- peer_packets : impl BatchSendIter < Self :: PeerKey > ,
293+ packet_batch : impl BatchSendIter < Self :: PeerKey > ,
311294 ) -> Result < ( ) , Self :: Error > {
312- for ( peer, packets) in peer_packets. batch_iter ( ) {
313- let Some ( node_key) = self . peer_lookup . id_to_key ( peer) else {
314- tracing:: warn!( peer_id = %peer, "no node key known for peer" ) ;
315- continue ;
316- } ;
317-
318- for packet in packets {
319- self . send_frame_with_extra ( & frame:: SendPacket { dest : node_key } , packet. as_ref ( ) )
320- . await ?;
295+ for ( key, pkt) in packet_batch. batch_iter ( ) {
296+ for pkt in pkt {
297+ self . send_one ( key, pkt. as_ref ( ) ) . await ?;
321298 }
322299 }
323300
324301 Ok ( ( ) )
325302 }
303+
304+ async fn recv ( & self ) -> impl BatchRecvIter < Self :: PeerKey , Error = Self :: Error > {
305+ [ self . recv_one ( ) . await . map ( |( k, pkt) | ( k, [ pkt] ) ) ]
306+ }
326307}
0 commit comments