This one makes it better for sure

This commit is contained in:
Igor Katson 2024-02-26 22:59:54 +00:00
parent 15d17355b5
commit f42007f436
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 204 additions and 194 deletions

View file

@ -4,6 +4,7 @@ use std::{
io::{BufReader, BufWriter, Read}, io::{BufReader, BufWriter, Read},
net::SocketAddr, net::SocketAddr,
path::PathBuf, path::PathBuf,
pin::Pin,
str::FromStr, str::FromStr,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
@ -24,7 +25,7 @@ use bencode::{bencode_serialize_to_writer, BencodeDeserializer};
use buffers::{ByteBuf, ByteBufT, ByteString}; use buffers::{ByteBuf, ByteBufT, ByteString};
use clone_to_owned::CloneToOwned; use clone_to_owned::CloneToOwned;
use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig}; use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig};
use futures::{stream::FuturesUnordered, TryFutureExt}; use futures::{stream::FuturesUnordered, Future, FutureExt, TryFutureExt};
use librqbit_core::{ use librqbit_core::{
directories::get_configuration_directory, directories::get_configuration_directory,
magnet::Magnet, magnet::Magnet,
@ -377,8 +378,9 @@ pub(crate) struct CheckedIncomingConnection {
impl Session { impl Session {
/// Create a new session. The passed in folder will be used as a default unless overriden per torrent. /// Create a new session. The passed in folder will be used as a default unless overriden per torrent.
pub async fn new(output_folder: PathBuf) -> anyhow::Result<Arc<Self>> { #[inline(never)]
Self::new_with_opts(output_folder, SessionOptions::default()).await pub fn new(output_folder: PathBuf) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>>>> {
Self::new_with_opts(output_folder, SessionOptions::default())
} }
pub fn default_persistence_filename() -> anyhow::Result<PathBuf> { pub fn default_persistence_filename() -> anyhow::Result<PathBuf> {
@ -391,10 +393,12 @@ impl Session {
} }
/// Create a new session with options. /// Create a new session with options.
pub async fn new_with_opts( #[inline(never)]
pub fn new_with_opts(
output_folder: PathBuf, output_folder: PathBuf,
mut opts: SessionOptions, mut opts: SessionOptions,
) -> anyhow::Result<Arc<Self>> { ) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>>>> {
async move {
let peer_id = opts.peer_id.unwrap_or_else(generate_peer_id); let peer_id = opts.peer_id.unwrap_or_else(generate_peer_id);
let token = CancellationToken::new(); let token = CancellationToken::new();
@ -479,6 +483,8 @@ impl Session {
Ok(session) Ok(session)
} }
.boxed()
}
async fn task_persistence(self: Arc<Self>) -> anyhow::Result<()> { async fn task_persistence(self: Arc<Self>) -> anyhow::Result<()> {
// Populate initial from the state filename // Populate initial from the state filename
@ -738,11 +744,12 @@ impl Session {
} }
/// Add a torrent to the session. /// Add a torrent to the session.
pub async fn add_torrent( pub fn add_torrent<'a>(
&self, &'a self,
add: AddTorrent<'_>, add: AddTorrent<'a>,
opts: Option<AddTorrentOptions>, opts: Option<AddTorrentOptions>,
) -> anyhow::Result<AddTorrentResponse> { ) -> Pin<Box<dyn Future<Output = anyhow::Result<AddTorrentResponse>> + Send + 'a>> {
async move {
// Magnet links are different in that we first need to discover the metadata. // Magnet links are different in that we first need to discover the metadata.
let span = error_span!("add_torrent"); let span = error_span!("add_torrent");
let _ = span.enter(); let _ = span.enter();
@ -763,8 +770,8 @@ impl Session {
let (info_hash, info, trackers, peer_rx, initial_peers) = match add { let (info_hash, info, trackers, peer_rx, initial_peers) = match add {
AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => { AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => {
let magnet = let magnet = Magnet::parse(&magnet)
Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?; .context("provided path is not a valid magnet URL")?;
let info_hash = magnet let info_hash = magnet
.as_id20() .as_id20()
.context("magnet link didn't contain a BTv1 infohash")?; .context("magnet link didn't contain a BTv1 infohash")?;
@ -869,6 +876,8 @@ impl Session {
) )
.await .await
} }
.boxed()
}
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn main_torrent_info( async fn main_torrent_info(

View file

@ -60,6 +60,7 @@ pub struct InitLoggingResult {
pub line_broadcast: LineBroadcast, pub line_broadcast: LineBroadcast,
} }
#[inline(never)]
pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResult> { pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResult> {
let stderr_filter = EnvFilter::builder() let stderr_filter = EnvFilter::builder()
.with_default_directive( .with_default_directive(