@@ -40,6 +40,10 @@ use crate::toxcore::time::*;
4040/// key is a DHT key.
4141type DhtPkTx = mpsc:: UnboundedSender < ( PublicKey , PublicKey ) > ;
4242
43+ /// Shorthand for the transmit half of the message channel for sending friend
44+ /// requests when we get them. The key is a long term key.
45+ type FriendRequestTx = mpsc:: UnboundedSender < ( PublicKey , FriendRequest ) > ;
46+
4347/// Number of friend's close nodes to store.
4448const MAX_ONION_FRIEND_NODES : u8 = 8 ;
4549
@@ -302,7 +306,10 @@ struct OnionClientState {
302306 friends : HashMap < PublicKey , OnionFriend > ,
303307 /// Sink to send DHT `PublicKey` when it gets known. The first key is a long
304308 /// term key, the second key is a DHT key.
305- dht_pk_tx : Option < DhtPkTx > ,
309+ dht_pk_tx : OptionalSink < DhtPkTx > ,
310+ /// Sink to send friend requests when we get them. The key is a long term
311+ /// key.
312+ friend_request_tx : OptionalSink < FriendRequestTx > ,
306313}
307314
308315impl OnionClientState {
@@ -312,7 +319,8 @@ impl OnionClientState {
312319 announce_list : Kbucket :: new ( MAX_ONION_ANNOUNCE_NODES ) ,
313320 announce_requests : RequestQueue :: new ( ANNOUNCE_TIMEOUT ) ,
314321 friends : HashMap :: new ( ) ,
315- dht_pk_tx : None ,
322+ dht_pk_tx : OptionalSink :: new ( ) ,
323+ friend_request_tx : OptionalSink :: new ( ) ,
316324 }
317325 }
318326}
@@ -360,7 +368,7 @@ impl OnionClient {
360368
361369 /// Set sink to send DHT `PublicKey` when it gets known.
362370 pub fn set_dht_pk_sink ( & self , dht_pk_tx : DhtPkTx ) {
363- self . state . lock ( ) . dht_pk_tx = Some ( dht_pk_tx) ;
371+ self . state . lock ( ) . dht_pk_tx . set ( dht_pk_tx) ;
364372 }
365373
366374 /// Check if a node was pinged recently.
@@ -511,11 +519,7 @@ impl OnionClient {
511519 friend. dht_pk = Some ( dht_pk_announce. dht_pk ) ;
512520 friend. last_seen = Some ( clock_now ( ) ) ;
513521
514- let dht_pk_future = if let Some ( ref dht_pk_tx) = state. dht_pk_tx {
515- Either :: A ( send_to ( dht_pk_tx, ( friend_pk, dht_pk_announce. dht_pk ) ) )
516- } else {
517- Either :: B ( future:: ok ( ( ) ) )
518- } ;
522+ let dht_pk_future = send_to ( & state. dht_pk_tx , ( friend_pk, dht_pk_announce. dht_pk ) ) ;
519523
520524 let futures = dht_pk_announce. nodes . into_iter ( ) . map ( |node| match node. ip_port . protocol {
521525 ProtocolType :: UDP => {
@@ -545,13 +549,15 @@ impl OnionClient {
545549 Ok ( payload) => payload,
546550 Err ( e) => return Either :: A ( future:: err ( e. context ( HandleDataResponseErrorKind :: InvalidInnerPayload ) . into ( ) ) )
547551 } ;
548- match iner_payload {
552+ let future = match iner_payload {
549553 OnionDataResponseInnerPayload :: DhtPkAnnounce ( dht_pk_announce) =>
550- Either :: B ( self . handle_dht_pk_announce ( payload. real_pk , dht_pk_announce)
554+ Either :: A ( self . handle_dht_pk_announce ( payload. real_pk , dht_pk_announce)
551555 . map_err ( |e| e. context ( HandleDataResponseErrorKind :: DhtPkAnnounce ) . into ( ) ) ) ,
552- OnionDataResponseInnerPayload :: FriendRequest ( _) =>
553- Either :: A ( future:: ok ( ( ) ) )
554- }
556+ OnionDataResponseInnerPayload :: FriendRequest ( friend_request) =>
557+ Either :: B ( send_to ( & self . state . lock ( ) . friend_request_tx , ( payload. real_pk , friend_request) )
558+ . map_err ( |e| e. context ( HandleDataResponseErrorKind :: FriendRequest ) . into ( ) ) )
559+ } ;
560+ Either :: B ( future)
555561 }
556562
557563 /// Add new node to random nodes pool to use them to build random paths.
@@ -991,7 +997,8 @@ mod tests {
991997 announce_list : Kbucket :: new ( 8 ) ,
992998 announce_requests : RequestQueue :: new ( Duration :: from_secs ( 42 ) ) ,
993999 friends : HashMap :: new ( ) ,
994- dht_pk_tx : None ,
1000+ dht_pk_tx : OptionalSink :: new ( ) ,
1001+ friend_request_tx : OptionalSink :: new ( ) ,
9951002 } ;
9961003
9971004 let _onion_client_state_c = onion_client_state. clone ( ) ;
0 commit comments