Trying to make torrent manager not depend on the whole metainfo

This commit is contained in:
Igor Katson 2021-07-03 09:13:09 +01:00
parent 897517521e
commit 9038630622
3 changed files with 14 additions and 24 deletions

View file

@ -14,7 +14,7 @@ use crate::{
lengths::{ChunkInfo, Lengths, ValidPieceIndex}, lengths::{ChunkInfo, Lengths, ValidPieceIndex},
peer_binary_protocol::Piece, peer_binary_protocol::Piece,
sha1w::ISha1, sha1w::ISha1,
torrent_metainfo::{FileIteratorName, TorrentMetaV1Owned}, torrent_metainfo::{FileIteratorName, TorrentMetaV1Info},
type_aliases::{PeerHandle, BF}, type_aliases::{PeerHandle, BF},
}; };
@ -48,7 +48,7 @@ pub fn update_hash_from_file<Sha1: ISha1>(
} }
pub struct FileOps<'a, Sha1> { pub struct FileOps<'a, Sha1> {
torrent: &'a TorrentMetaV1Owned, torrent: &'a TorrentMetaV1Info<ByteString>,
files: &'a [Arc<Mutex<File>>], files: &'a [Arc<Mutex<File>>],
lengths: &'a Lengths, lengths: &'a Lengths,
phantom_data: PhantomData<Sha1>, phantom_data: PhantomData<Sha1>,
@ -56,7 +56,7 @@ pub struct FileOps<'a, Sha1> {
impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> { impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
pub fn new( pub fn new(
torrent: &'a TorrentMetaV1Owned, torrent: &'a TorrentMetaV1Info<ByteString>,
files: &'a [Arc<Mutex<File>>], files: &'a [Arc<Mutex<File>>],
lengths: &'a Lengths, lengths: &'a Lengths,
) -> Self { ) -> Self {
@ -98,7 +98,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
let mut file_iterator = self let mut file_iterator = self
.files .files
.iter() .iter()
.zip(self.torrent.info.iter_filenames_and_lengths()) .zip(self.torrent.iter_filenames_and_lengths())
.enumerate() .enumerate()
.map(|(idx, (fd, (name, len)))| { .map(|(idx, (fd, (name, len)))| {
let full_file_required = if let Some(only_files) = only_files { let full_file_required = if let Some(only_files) = only_files {
@ -183,7 +183,6 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
if self if self
.torrent .torrent
.info
.compare_hash(piece_info.piece_index.get(), computed_hash.finish()) .compare_hash(piece_info.piece_index.get(), computed_hash.finish())
.unwrap() .unwrap()
{ {
@ -229,9 +228,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
let mut piece_remaining_bytes = piece_length as usize; let mut piece_remaining_bytes = piece_length as usize;
for (file_idx, (name, file_len)) in for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths().enumerate() {
self.torrent.info.iter_filenames_and_lengths().enumerate()
{
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -271,11 +268,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
absolute_offset = 0; absolute_offset = 0;
} }
match self match self.torrent.compare_hash(piece_index.get(), h.finish()) {
.torrent
.info
.compare_hash(piece_index.get(), h.finish())
{
Some(true) => { Some(true) => {
debug!("piece={} hash matches", piece_index); debug!("piece={} hash matches", piece_index);
Ok(true) Ok(true)
@ -304,7 +297,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
let mut absolute_offset = self.lengths.chunk_absolute_offset(&chunk_info); let mut absolute_offset = self.lengths.chunk_absolute_offset(&chunk_info);
let mut buf = result_buf; let mut buf = result_buf;
for (file_idx, file_len) in self.torrent.info.iter_file_lengths().enumerate() { for (file_idx, file_len) in self.torrent.iter_file_lengths().enumerate() {
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -358,9 +351,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
let mut buf = data.block.as_ref(); let mut buf = data.block.as_ref();
let mut absolute_offset = self.lengths.chunk_absolute_offset(&chunk_info); let mut absolute_offset = self.lengths.chunk_absolute_offset(&chunk_info);
for (file_idx, (name, file_len)) in for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths().enumerate() {
self.torrent.info.iter_filenames_and_lengths().enumerate()
{
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;

View file

@ -159,7 +159,7 @@ impl TorrentManager {
debug!("computed lengths: {:?}", &lengths); debug!("computed lengths: {:?}", &lengths);
info!("Doing initial checksum validation, this might take a while..."); info!("Doing initial checksum validation, this might take a while...");
let initial_check_results = FileOps::<Sha1>::new(&torrent, &files, &lengths) let initial_check_results = FileOps::<Sha1>::new(&torrent.info, &files, &lengths)
.initial_check(only_files.as_deref())?; .initial_check(only_files.as_deref())?;
info!( info!(
@ -176,7 +176,7 @@ impl TorrentManager {
let state = Arc::new(TorrentState { let state = Arc::new(TorrentState {
info_hash: torrent.info_hash, info_hash: torrent.info_hash,
torrent, torrent: torrent.info,
peer_id, peer_id,
locked: Arc::new(RwLock::new(TorrentStateLocked { locked: Arc::new(RwLock::new(TorrentStateLocked {
peers: Default::default(), peers: Default::default(),
@ -312,7 +312,7 @@ impl TorrentManager {
let mut event = Some(TrackerRequestEvent::Started); let mut event = Some(TrackerRequestEvent::Started);
loop { loop {
let request = TrackerRequest { let request = TrackerRequest {
info_hash: self.state.torrent.info_hash, info_hash: self.state.info_hash,
peer_id: self.state.peer_id, peer_id: self.state.peer_id,
port: 6778, port: 6778,
uploaded: self.state.get_uploaded(), uploaded: self.state.get_uploaded(),

View file

@ -27,7 +27,7 @@ use crate::{
peer_connection::{PeerConnection, PeerConnectionHandler, WriterRequest}, peer_connection::{PeerConnection, PeerConnectionHandler, WriterRequest},
peer_state::{InflightRequest, LivePeerState, PeerState}, peer_state::{InflightRequest, LivePeerState, PeerState},
spawn_utils::{spawn, BlockingSpawner}, spawn_utils::{spawn, BlockingSpawner},
torrent_metainfo::TorrentMetaV1Owned, torrent_metainfo::{TorrentMetaV1Info, TorrentMetaV1Owned},
type_aliases::{PeerHandle, Sha1, BF}, type_aliases::{PeerHandle, Sha1, BF},
}; };
@ -189,7 +189,7 @@ pub struct StatsSnapshot {
} }
pub struct TorrentState { pub struct TorrentState {
pub torrent: TorrentMetaV1Owned, pub torrent: TorrentMetaV1Info<ByteString>,
pub locked: Arc<RwLock<TorrentStateLocked>>, pub locked: Arc<RwLock<TorrentStateLocked>>,
pub files: Vec<Arc<Mutex<File>>>, pub files: Vec<Arc<Mutex<File>>>,
pub info_hash: [u8; 20], pub info_hash: [u8; 20],
@ -411,8 +411,7 @@ impl TorrentState {
state: self.clone(), state: self.clone(),
spawner: self.spawner, spawner: self.spawner,
}; };
let peer_connection = let peer_connection = PeerConnection::new(addr, self.info_hash, self.peer_id, handler);
PeerConnection::new(addr, self.torrent.info_hash, self.peer_id, handler);
spawn(format!("manage_peer({})", handle), async move { spawn(format!("manage_peer({})", handle), async move {
if let Err(e) = peer_connection.manage_peer(out_rx).await { if let Err(e) = peer_connection.manage_peer(out_rx).await {
debug!("error managing peer {}: {:#}", handle, e) debug!("error managing peer {}: {:#}", handle, e)