This commit is contained in:
Igor Katson 2024-02-18 20:16:18 +00:00
parent f5ccb8632b
commit 76b7d23149
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 17 additions and 24 deletions

View file

@ -13,10 +13,8 @@ use anyhow::{bail, Context};
use bencode::{bencode_serialize_to_writer, BencodeDeserializer};
use buffers::{ByteBuf, ByteBufT, ByteString};
use clone_to_owned::CloneToOwned;
use dht::{
Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig, RequestPeersStream,
};
use futures::{stream::FuturesUnordered, Stream, TryFutureExt};
use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig};
use futures::{stream::FuturesUnordered, TryFutureExt};
use librqbit_core::{
directories::get_configuration_directory,
magnet::Magnet,
@ -43,7 +41,7 @@ use crate::{
torrent_state::{
ManagedTorrentBuilder, ManagedTorrentHandle, ManagedTorrentState, TorrentStateLive,
},
tracker_comms::{self, TorrentStatsForTrackerDummy, TrackerComms},
tracker_comms::{self, TrackerComms},
type_aliases::PeerStream,
};
@ -368,18 +366,6 @@ async fn create_tcp_listener(
bail!("no free TCP ports in range {port_range:?}");
}
fn merge_peer_rx(
dht_rx: Option<RequestPeersStream>,
peer_rx: Option<impl Stream<Item = SocketAddr> + Unpin + Send + Sync + 'static>,
) -> Option<PeerStream> {
match (dht_rx, peer_rx) {
(Some(dht_rx), None) => Some(Box::new(dht_rx)),
(None, Some(peer_rx)) => Some(Box::new(peer_rx)),
(None, None) => None,
(Some(dht_rx), Some(peer_rx)) => Some(Box::new(dht_rx.merge(peer_rx))),
}
}
pub(crate) struct CheckedIncomingConnection {
pub addr: SocketAddr,
pub stream: tokio::net::TcpStream,
@ -1073,6 +1059,7 @@ impl Session {
}
}
// Get a peer stream from both DHT and trackers.
fn make_peer_rx(
&self,
info_hash: Id20,
@ -1095,8 +1082,14 @@ impl Session {
cancel,
announce_port,
);
let peer_rx = merge_peer_rx(dht_rx, peer_rx);
Ok(peer_rx)
// Merge DHT rx and tracker comms peer rx.
match (dht_rx, peer_rx) {
(Some(dht_rx), None) => Ok(Some(Box::new(dht_rx))),
(None, Some(peer_rx)) => Ok(Some(Box::new(peer_rx))),
(None, None) => Ok(None),
(Some(dht_rx), Some(peer_rx)) => Ok(Some(Box::new(dht_rx.merge(peer_rx)))),
}
}
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {