internal async Task HashCheckAsync(bool autoStart, bool setStoppedModeWhenDone) { if (!HasMetadata) { throw new TorrentException("A hashcheck cannot be performed if the TorrentManager was created with a Magnet link and the metadata has not been downloaded."); } await ClientEngine.MainLoop; if (!Mode.CanHashCheck) { throw new TorrentException(string.Format("A hashcheck can only be performed when the manager is stopped. State is: {0}", State)); } CheckRegisteredAndDisposed(); StartTime = DateTime.Now; // An IgnoringPicker is created to ensure pieces which *have not* been hash checked // are not requested from other peers. The intention is that files marked as DoNotDownload // will not be hashed, or downloaded. UnhashedPieces.SetAll(true); var hashingMode = new HashingMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings); Mode = hashingMode; try { await hashingMode.WaitForHashingToComplete(); hashingMode.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { return; } catch (Exception ex) { TrySetError(Reason.ReadFailure, ex); return; } HashChecked = true; if (autoStart) { await StartAsync(); } else if (setStoppedModeWhenDone) { Mode = new StoppedMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings); } }
internal async Task HashCheckAsync(bool autoStart, bool setStoppedModeWhenDone) { if (!HasMetadata) { throw new TorrentException("A hashcheck cannot be performed if the TorrentManager was created with a Magnet link and the metadata has not been downloaded."); } await ClientEngine.MainLoop; if (!Mode.CanHashCheck) { throw new TorrentException(string.Format("A hashcheck can only be performed when the manager is stopped. State is: {0}", State)); } CheckRegisteredAndDisposed(); StartTime = DateTime.Now; // An IgnoringPicker is created to ensure pieces which *have not* been hash checked // are not requested from other peers. The intention is that files marked as DoNotDownload // will not be hashed, or downloaded. UnhashedPieces.SetAll(true); var hashingMode = new HashingMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings); Mode = hashingMode; try { await hashingMode.WaitForHashingToComplete(); HashChecked = true; if (autoStart) { await StartAsync(); } else if (setStoppedModeWhenDone) { Mode = new StoppedMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings); } } catch { HashChecked = false; // If the hash check was cancelled (by virtue of a new Mode being set on the TorrentManager) then // we don't want to overwrite the Mode which was set. if (Mode == hashingMode) { Mode = new StoppedMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings); } } }
public void LoadFastResume(FastResume data) { Check.Data(data); CheckMetadata(); if (State != TorrentState.Stopped) { throw new InvalidOperationException("Can only load FastResume when the torrent is stopped"); } if (InfoHash != data.Infohash || Torrent.Pieces.Count != data.Bitfield.Length) { throw new ArgumentException("The fast resume data does not match this torrent", "fastResumeData"); } for (int i = 0; i < Torrent.Pieces.Count; i++) { OnPieceHashed(i, data.Bitfield[i]); } UnhashedPieces.From(data.UnhashedPieces); this.HashChecked = true; }