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

View file

@ -159,7 +159,7 @@ impl TorrentManager {
debug!("computed lengths: {:?}", &lengths);
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())?;
info!(
@ -176,7 +176,7 @@ impl TorrentManager {
let state = Arc::new(TorrentState {
info_hash: torrent.info_hash,
torrent,
torrent: torrent.info,
peer_id,
locked: Arc::new(RwLock::new(TorrentStateLocked {
peers: Default::default(),
@ -312,7 +312,7 @@ impl TorrentManager {
let mut event = Some(TrackerRequestEvent::Started);
loop {
let request = TrackerRequest {
info_hash: self.state.torrent.info_hash,
info_hash: self.state.info_hash,
peer_id: self.state.peer_id,
port: 6778,
uploaded: self.state.get_uploaded(),

View file

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