Skip to content

Commit 5afddd2

Browse files
committed
feat(onion_client): handle friend requests
1 parent 6549f52 commit 5afddd2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/toxcore/onion/client/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ error_kind! {
6464
#[doc = "Failed to handle DHT `PublicKey` announce."]
6565
#[fail(display = "Failed to handle DHT PublicKey announce")]
6666
DhtPkAnnounce,
67+
#[doc = "Failed to send a friend request."]
68+
#[fail(display = "Failed to send a friend request")]
69+
FriendRequest,
6770
}
6871
}
6972

src/toxcore/onion/client/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ use crate::toxcore::time::*;
4040
/// key is a DHT key.
4141
type 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.
4448
const MAX_ONION_FRIEND_NODES: u8 = 8;
4549

@@ -303,6 +307,9 @@ struct OnionClientState {
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.
305309
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

308315
impl OnionClientState {
@@ -313,6 +320,7 @@ impl OnionClientState {
313320
announce_requests: RequestQueue::new(ANNOUNCE_TIMEOUT),
314321
friends: HashMap::new(),
315322
dht_pk_tx: OptionalSink::new(),
323+
friend_request_tx: OptionalSink::new(),
316324
}
317325
}
318326
}
@@ -541,13 +549,15 @@ impl OnionClient {
541549
Ok(payload) => payload,
542550
Err(e) => return Either::A(future::err(e.context(HandleDataResponseErrorKind::InvalidInnerPayload).into()))
543551
};
544-
match iner_payload {
552+
let future = match iner_payload {
545553
OnionDataResponseInnerPayload::DhtPkAnnounce(dht_pk_announce) =>
546-
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)
547555
.map_err(|e| e.context(HandleDataResponseErrorKind::DhtPkAnnounce).into())),
548-
OnionDataResponseInnerPayload::FriendRequest(_) =>
549-
Either::A(future::ok(()))
550-
}
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)
551561
}
552562

553563
/// Add new node to random nodes pool to use them to build random paths.
@@ -988,6 +998,7 @@ mod tests {
988998
announce_requests: RequestQueue::new(Duration::from_secs(42)),
989999
friends: HashMap::new(),
9901000
dht_pk_tx: OptionalSink::new(),
1001+
friend_request_tx: OptionalSink::new(),
9911002
};
9921003

9931004
let _onion_client_state_c = onion_client_state.clone();

0 commit comments

Comments
 (0)