Skip to main content

SessionHandle

Struct SessionHandle 

Source
pub struct SessionHandle { /* private fields */ }
Expand description

Cloneable handle for interacting with a running session.

Implementations§

Source§

impl SessionHandle

Source

pub async fn start(settings: Settings) -> Result<Self>

Start a new session with the given settings and no plugins.

Source

pub async fn start_with_backend( settings: Settings, backend: Arc<dyn DiskIoBackend>, ) -> Result<Self>

Start a new session with a custom disk I/O backend and no plugins.

Source

pub async fn start_with_plugins( settings: Settings, plugins: Arc<Vec<Box<dyn ExtensionPlugin>>>, ) -> Result<Self>

Start a new session with the given settings and extension plugins.

Source

pub async fn start_with_plugins_and_backend( settings: Settings, plugins: Arc<Vec<Box<dyn ExtensionPlugin>>>, backend: Arc<dyn DiskIoBackend>, ) -> Result<Self>

Start a new session with the given settings, extension plugins, and a custom disk I/O backend.

Source

pub async fn start_with_transport( settings: Settings, factory: Arc<NetworkFactory>, ) -> Result<Self>

Start a new session with the given settings and a custom transport factory.

Uses default plugins (none) and default disk backend.

Source

pub async fn start_full( settings: Settings, plugins: Arc<Vec<Box<dyn ExtensionPlugin>>>, backend: Arc<dyn DiskIoBackend>, factory: Arc<NetworkFactory>, ) -> Result<Self>

Start a new session with all customizable parameters.

This is the most general constructor — all other start_* variants delegate to this method. The factory parameter controls how TCP listeners and connections are created: use crate::transport::NetworkFactory::tokio() for real networking or a custom factory for simulation.

Source

pub async fn add_torrent( &self, meta: TorrentMeta, storage: Option<Arc<dyn TorrentStorage>>, ) -> Result<Id20>

Add a torrent from parsed .torrent metadata (v1, v2, or hybrid).

Source

pub async fn add_torrent_with_dir( &self, meta: TorrentMeta, storage: Option<Arc<dyn TorrentStorage>>, download_dir: Option<PathBuf>, ) -> Result<Id20>

Add a torrent with an optional per-torrent download directory override.

Source

pub async fn add_magnet(&self, magnet: Magnet) -> Result<Id20>

Add a torrent from a magnet link (metadata fetched via BEP 9).

Source

pub async fn add_magnet_with_dir( &self, magnet: Magnet, download_dir: Option<PathBuf>, ) -> Result<Id20>

Add a magnet link with an optional per-torrent download directory override.

Source

pub async fn remove_torrent(&self, info_hash: Id20) -> Result<()>

Remove a torrent from the session.

Source

pub async fn pause_torrent(&self, info_hash: Id20) -> Result<()>

Pause a torrent.

Source

pub async fn resume_torrent(&self, info_hash: Id20) -> Result<()>

Resume a paused torrent.

Source

pub async fn torrent_stats(&self, info_hash: Id20) -> Result<TorrentStats>

Get statistics for a specific torrent.

Source

pub async fn torrent_info(&self, info_hash: Id20) -> Result<TorrentInfo>

Get metadata info for a specific torrent.

Source

pub async fn list_torrents(&self) -> Result<Vec<Id20>>

List all active torrent info hashes.

Source

pub async fn session_stats(&self) -> Result<SessionStats>

Get aggregate session statistics.

Source

pub fn subscribe(&self) -> Receiver<Alert>

Subscribe to all alerts passing the session-level mask.

Source

pub fn subscribe_filtered(&self, filter: AlertCategory) -> AlertStream

Subscribe with per-subscriber category filtering.

Source

pub async fn post_session_stats(&self) -> Result<()>

Trigger an immediate session stats snapshot and alert.

Source

pub fn counters(&self) -> &Arc<SessionCounters>

Access the shared atomic counters (read-only handle).

Source

pub fn set_alert_mask(&self, mask: AlertCategory)

Atomically update the session-level alert mask.

Source

pub fn alert_mask(&self) -> AlertCategory

Read the current session-level alert mask.

Source

pub async fn add_peers( &self, info_hash: Id20, peers: Vec<SocketAddr>, source: PeerSource, ) -> Result<()>

Add peers to a specific torrent by info hash.

Source

pub async fn shutdown(&self) -> Result<()>

Gracefully shut down the session and all torrents.

Source

pub async fn save_torrent_resume_data( &self, info_hash: Id20, ) -> Result<FastResumeData>

Save resume data for a specific torrent.

Source

pub async fn save_session_state(&self) -> Result<SessionState>

Save full session state (all torrent resume data + DHT node cache).

Source

pub async fn load_resume_state(&self) -> Result<ResumeLoadResult>

Load and restore torrents from per-torrent resume files on disk.

Scans the resume directory for .resume files, deserializes each one, reconstructs the torrent metadata, and re-adds it to the session. For resolved torrents (with stored info dict), the piece bitmap is restored. Unresolved magnets are re-added as magnet links.

§Errors

Returns crate::Error::Shutdown if the session has been shut down.

Source

pub async fn save_resume_state(&self) -> Result<usize>

Save per-torrent resume files for all dirty torrents.

Iterates every torrent in the session, checks the need_save_resume dirty flag, serializes resume data to disk, and clears the flag. Returns the number of files written.

§Errors

Returns [Error::Shutdown] if the session actor has stopped.

Source

pub async fn queue_position(&self, info_hash: Id20) -> Result<i32>

Get the queue position of a torrent. Returns -1 if not auto-managed.

Source

pub async fn set_queue_position(&self, info_hash: Id20, pos: i32) -> Result<()>

Set the absolute queue position of a torrent. Shifts other torrents.

Source

pub async fn queue_position_up(&self, info_hash: Id20) -> Result<()>

Move a torrent one position up (lower number = higher priority).

Source

pub async fn queue_position_down(&self, info_hash: Id20) -> Result<()>

Move a torrent one position down.

Source

pub async fn queue_position_top(&self, info_hash: Id20) -> Result<()>

Move a torrent to position 0 (highest priority).

Source

pub async fn queue_position_bottom(&self, info_hash: Id20) -> Result<()>

Move a torrent to the last position (lowest priority).

Source

pub async fn ban_peer(&self, ip: IpAddr) -> Result<()>

Ban a peer IP session-wide. All torrents will disconnect and refuse this IP.

Source

pub async fn unban_peer(&self, ip: IpAddr) -> Result<bool>

Remove a ban and clear strikes for an IP. Returns true if the IP was banned.

Source

pub async fn set_ip_filter(&self, filter: IpFilter) -> Result<()>

Replace the session-wide IP filter. Connected peers that are now blocked will be refused on subsequent connection attempts.

Source

pub async fn ip_filter(&self) -> Result<IpFilter>

Get a clone of the current IP filter.

Source

pub async fn settings(&self) -> Result<Settings>

Get a clone of the current session settings.

Source

pub async fn apply_settings(&self, settings: Settings) -> Result<()>

Apply new settings at runtime.

Validates the settings, updates rate limiters immediately, and stores the new settings. Sub-actor reconfiguration (disk, DHT, NAT) takes effect on next session restart.

Source

pub async fn banned_peers(&self) -> Result<Vec<IpAddr>>

Get the list of currently banned peer IPs.

Source

pub async fn move_torrent_storage( &self, info_hash: Id20, new_path: PathBuf, ) -> Result<()>

Move a torrent’s data files to a new download directory.

Source

pub async fn open_file( &self, info_hash: Id20, file_index: usize, ) -> Result<FileStream>

Opens a file stream for sequential reading (AsyncRead + AsyncSeek).

The returned FileStream reads data from a specific file within a torrent, blocking on pieces that haven’t been downloaded yet.

Source

pub async fn force_reannounce(&self, info_hash: Id20) -> Result<()>

Force all trackers for a torrent to re-announce immediately.

Source

pub async fn tracker_list(&self, info_hash: Id20) -> Result<Vec<TrackerInfo>>

Get the list of all configured trackers with their status for a torrent.

Source

pub async fn scrape( &self, info_hash: Id20, ) -> Result<Option<(String, ScrapeInfo)>>

Scrape trackers for seeder/leecher counts for a torrent.

Source

pub async fn set_file_priority( &self, info_hash: Id20, index: usize, priority: FilePriority, ) -> Result<()>

Set the download priority of a specific file within a torrent.

Source

pub async fn file_priorities( &self, info_hash: Id20, ) -> Result<Vec<FilePriority>>

Get the current per-file priorities for a torrent.

Source

pub async fn set_download_limit( &self, info_hash: Id20, bytes_per_sec: u64, ) -> Result<()>

Set the per-torrent download rate limit in bytes/sec (0 = unlimited).

Source

pub async fn set_upload_limit( &self, info_hash: Id20, bytes_per_sec: u64, ) -> Result<()>

Set the per-torrent upload rate limit in bytes/sec (0 = unlimited).

Source

pub async fn download_limit(&self, info_hash: Id20) -> Result<u64>

Get the current per-torrent download rate limit in bytes/sec (0 = unlimited).

Source

pub async fn upload_limit(&self, info_hash: Id20) -> Result<u64>

Get the current per-torrent upload rate limit in bytes/sec (0 = unlimited).

Source

pub async fn set_sequential_download( &self, info_hash: Id20, enabled: bool, ) -> Result<()>

Enable or disable sequential (in-order) piece downloading for a torrent.

Source

pub async fn is_sequential_download(&self, info_hash: Id20) -> Result<bool>

Query whether sequential downloading is enabled for a torrent.

Source

pub async fn set_super_seeding( &self, info_hash: Id20, enabled: bool, ) -> Result<()>

Enable or disable BEP 16 super seeding mode for a torrent.

Source

pub async fn is_super_seeding(&self, info_hash: Id20) -> Result<bool>

Query whether BEP 16 super seeding mode is enabled for a torrent.

Source

pub async fn set_seed_mode(&self, info_hash: Id20, enabled: bool) -> Result<()>

Enable or disable user-requested seed-only mode for a torrent (M159).

When enabled is true, the engine stops scheduling new block requests and cancels all in-flight requests for the torrent, but continues to serve uploads to interested peers. This is distinct from “naturally seeding” (all pieces downloaded): it represents an explicit user toggle layered on top of the download state.

Toggling back to false resumes normal piece scheduling.

§Errors

Returns crate::Error::TorrentNotFound if info_hash is not registered in the session, or crate::Error::Shutdown if the session actor has terminated.

Source

pub async fn add_tracker(&self, info_hash: Id20, url: String) -> Result<()>

Add a new tracker URL to a torrent.

The URL is validated and deduplicated by the tracker manager.

Source

pub async fn replace_trackers( &self, info_hash: Id20, urls: Vec<String>, ) -> Result<()>

Replace all tracker URLs for a torrent.

Source

pub async fn force_recheck(&self, info_hash: Id20) -> Result<()>

Trigger a full piece verification (force recheck) for a torrent.

Clears all piece completion data, re-verifies every piece, and transitions to Seeding or Downloading depending on the result. Returns after the recheck is complete.

Source

pub async fn rename_file( &self, info_hash: Id20, file_index: usize, new_name: String, ) -> Result<()>

Rename a file within a torrent on disk.

Changes the filename of the specified file (by index) to new_name. The file stays in the same directory; only the filename component changes. Fires a FileRenamed alert on success.

Source

pub async fn set_max_connections( &self, info_hash: Id20, limit: usize, ) -> Result<()>

Set the per-torrent maximum number of connections (0 = use global default).

Source

pub async fn max_connections(&self, info_hash: Id20) -> Result<usize>

Get the current per-torrent maximum connection limit (0 = use global default).

Source

pub async fn set_max_uploads(&self, info_hash: Id20, limit: usize) -> Result<()>

Set the per-torrent maximum number of upload slots (unchoke slots).

Source

pub async fn max_uploads(&self, info_hash: Id20) -> Result<usize>

Get the current per-torrent maximum upload slots (unchoke slots).

Source

pub async fn get_peer_info(&self, info_hash: Id20) -> Result<Vec<PeerInfo>>

Get per-peer details for all connected peers of a torrent.

Source

pub async fn get_download_queue( &self, info_hash: Id20, ) -> Result<Vec<PartialPieceInfo>>

Get in-flight piece download status for a torrent (the download queue).

Source

pub async fn have_piece(&self, info_hash: Id20, index: u32) -> Result<bool>

Check whether a specific piece has been downloaded for a torrent.

Source

pub async fn piece_availability(&self, info_hash: Id20) -> Result<Vec<u32>>

Get per-piece availability counts from connected peers for a torrent.

Source

pub async fn file_progress(&self, info_hash: Id20) -> Result<Vec<u64>>

Get per-file bytes-downloaded progress for a torrent.

Source

pub async fn info_hashes(&self, info_hash: Id20) -> Result<InfoHashes>

Get the torrent’s identity hashes (v1 and/or v2).

Source

pub async fn torrent_file( &self, info_hash: Id20, ) -> Result<Option<TorrentMetaV1>>

Get the full v1 metainfo for a torrent.

Returns None for magnet links before metadata has been received.

Source

pub async fn torrent_file_v2( &self, info_hash: Id20, ) -> Result<Option<TorrentMetaV2>>

Get the full v2 metainfo for a torrent.

Returns None if the torrent is not a v2/hybrid torrent, or for magnet links before metadata has been received.

Source

pub async fn force_dht_announce(&self, info_hash: Id20) -> Result<()>

Force an immediate DHT announce for a torrent.

Source

pub async fn force_lsd_announce(&self, info_hash: Id20) -> Result<()>

Force an immediate LSD (Local Service Discovery) announce for a torrent.

LSD is a session-level component — this does not go through the torrent actor.

Source

pub async fn read_piece(&self, info_hash: Id20, index: u32) -> Result<Bytes>

Read all data for a specific piece from disk.

Source

pub async fn flush_cache(&self, info_hash: Id20) -> Result<()>

Flush the disk write cache for a torrent.

Source

pub async fn is_valid(&self, info_hash: Id20) -> bool

Check if a torrent exists in the session and its handle is still valid.

Source

pub async fn clear_error(&self, info_hash: Id20) -> Result<()>

Clear the error state on a torrent, resuming it if it was paused due to error.

Source

pub async fn file_status(&self, info_hash: Id20) -> Result<Vec<FileStatus>>

Get per-file open/mode status for a torrent.

Source

pub async fn flags(&self, info_hash: Id20) -> Result<TorrentFlags>

Read the current torrent flags as a crate::types::TorrentFlags bitflag set.

Source

pub async fn set_flags( &self, info_hash: Id20, flags: TorrentFlags, ) -> Result<()>

Set (enable) the specified torrent flags.

Source

pub async fn unset_flags( &self, info_hash: Id20, flags: TorrentFlags, ) -> Result<()>

Unset (disable) the specified torrent flags.

Source

pub async fn connect_peer( &self, info_hash: Id20, addr: SocketAddr, ) -> Result<()>

Immediately initiate a peer connection for a torrent.

Source

pub async fn dht_put_immutable(&self, value: Vec<u8>) -> Result<Id20>

Store an immutable item in the DHT (BEP 44).

Returns the SHA-1 target hash of the stored value.

Source

pub async fn dht_get_immutable(&self, target: Id20) -> Result<Option<Vec<u8>>>

Retrieve an immutable item from the DHT (BEP 44).

Returns Some(value) if found, None otherwise.

Source

pub async fn dht_put_mutable( &self, keypair_bytes: [u8; 32], value: Vec<u8>, seq: i64, salt: Vec<u8>, ) -> Result<Id20>

Store a mutable item in the DHT (BEP 44).

keypair_bytes is a 32-byte Ed25519 seed. Returns the target hash.

Source

pub async fn dht_get_mutable( &self, public_key: [u8; 32], salt: Vec<u8>, ) -> Result<Option<(Vec<u8>, i64)>>

Retrieve a mutable item from the DHT (BEP 44).

Returns Some((value, seq)) if found, None otherwise.

Source

pub async fn list_torrent_summaries(&self) -> Result<Vec<TorrentSummary>>

List all torrents as lightweight summaries.

Fetches stats for each active torrent and converts to TorrentSummary. Torrents that fail the stats query (e.g. shutting down) are silently skipped.

Source

pub async fn add_magnet_uri(&self, uri: &str) -> Result<InfoHashes>

Add a torrent from a magnet URI string.

Parses the URI, extracts info hashes, and adds the magnet to the session. Returns the info hashes (v1 and/or v2) for the added torrent.

Source

pub async fn add_torrent_bytes(&self, bytes: &[u8]) -> Result<InfoHashes>

Add a torrent from raw .torrent file bytes.

Auto-detects v1, v2, or hybrid format. Returns the info hashes for the added torrent.

Trait Implementations§

Source§

impl Clone for SessionHandle

Source§

fn clone(&self) -> SessionHandle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more