Пример #1
0
        void Initialise(string savePath, string baseDirectory, IList <RawTrackerTier> announces)
        {
            this.Bitfield = new BitField(HasMetadata ? Torrent.Pieces.Count : 1);
            this.PartialProgressSelector = new BitField(HasMetadata ? Torrent.Pieces.Count : 1);
            this.UnhashedPieces          = new BitField(HasMetadata ? Torrent.Pieces.Count : 1).SetAll(true);
            this.SavePath            = Path.Combine(savePath, baseDirectory);
            this.finishedPieces      = new Queue <HaveMessage>();
            this.Monitor             = new ConnectionMonitor();
            this.InactivePeerManager = new InactivePeerManager(this);
            this.Peers        = new PeerManager();
            this.PieceManager = new PieceManager(this);
            SetTrackerManager(new TrackerManager(new TrackerRequestFactory(this), announces));

            Mode = new StoppedMode(this, null, null, null);
            CreateRateLimiters();


            if (HasMetadata)
            {
                foreach (TorrentFile file in Torrent.Files)
                {
                    file.FullPath = Path.Combine(SavePath, file.Path);
                }
            }
        }
        void Initialise(string savePath, string baseDirectory, IList <RawTrackerTier> announces)
        {
            this.bitfield            = new BitField(HasMetadata ? torrent.Pieces.Count : 1);
            this.savePath            = Path.Combine(savePath, baseDirectory);
            this.finishedPieces      = new Queue <int>();
            this.monitor             = new ConnectionMonitor();
            this.inactivePeerManager = new InactivePeerManager(this);
            this.peers          = new PeerManager();
            this.pieceManager   = new PieceManager();
            this.trackerManager = new TrackerManager(this, InfoHash, announces);

            Mode = new StoppedMode(this);
            CreateRateLimiters();

            PieceHashed += delegate(object o, PieceHashedEventArgs e) {
                PieceManager.UnhashedPieces[e.PieceIndex] = false;
            };

            if (HasMetadata)
            {
                foreach (TorrentFile file in torrent.Files)
                {
                    file.FullPath = Path.Combine(SavePath, file.Path);
                }
            }
        }
Пример #3
0
        void Initialise(string savePath, string baseDirectory, IList <RawTrackerTier> announces)
        {
            this.Bitfield            = new BitField(HasMetadata ? Torrent.Pieces.Count : 1);
            this.SavePath            = Path.Combine(savePath, baseDirectory);
            this.finishedPieces      = new Queue <HaveMessage>();
            this.Monitor             = new ConnectionMonitor();
            this.InactivePeerManager = new InactivePeerManager(this);
            this.Peers          = new PeerManager();
            this.PieceManager   = new PieceManager();
            this.TrackerManager = new TrackerManager(new TrackerRequestFactory(this), announces);

            Mode = new StoppedMode(this);
            CreateRateLimiters();

            TrackerManager.AnnounceComplete += async(o, e) => {
                if (e.Successful)
                {
                    await ClientEngine.MainLoop;

                    Peers.BusyPeers.Clear();
                    int count = await AddPeersAsync(e.Peers);

                    RaisePeersFound(new TrackerPeersAdded(this, count, e.Peers.Count, e.Tracker));
                }
            };

            if (HasMetadata)
            {
                foreach (TorrentFile file in Torrent.Files)
                {
                    file.FullPath = Path.Combine(SavePath, file.Path);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Stops the TorrentManager
        /// </summary>
        public void Stop()
        {
            if (State == TorrentState.Error)
            {
                error = null;
                Mode  = new StoppedMode(this);
                return;
            }

            if (Mode is StoppingMode)
            {
                return;
            }

            ClientEngine.MainLoop.QueueWait(delegate
            {
                if (State != TorrentState.Stopped)
                {
#if !DISABLE_DHT
                    engine.DhtEngine.PeersFound -= DhtPeersFound;
#endif
                    Mode = new StoppingMode(this);
                }
            });
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
        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);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Starts a hashcheck. If forceFullScan is false, the library will attempt to load fastresume data
        /// before performing a full scan, otherwise fast resume data will be ignored and a full scan will be started
        /// </summary>
        /// <param name="forceFullScan">True if a full hash check should be performed ignoring fast resume data</param>
        public async Task HashCheckAsync(bool autoStart)
        {
            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;

            var hashingMode = new HashingMode(this);

            Mode = hashingMode;
            try {
                await hashingMode.WaitForHashingToComplete();

                HashChecked = true;
                if (autoStart)
                {
                    await StartAsync();
                }
                else
                {
                    Mode = new StoppedMode(this);
                }
            } 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);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Stops the TorrentManager
        /// </summary>
        public async Task StopAsync()
        {
            await ClientEngine.MainLoop;

            if (State == TorrentState.Error)
            {
                Error = null;
                Mode  = new StoppedMode(this);
                return;
            }

            if (Mode is StoppingMode)
            {
                return;
            }

            if (State != TorrentState.Stopped)
            {
                Engine.DhtEngine.PeersFound -= DhtPeersFound;
                Mode = new StoppingMode(this);
            }
        }
Пример #9
0
        void Initialise(string savePath, string baseDirectory, IList <RawTrackerTier> announces)
        {
            this.Bitfield            = new BitField(HasMetadata ? Torrent.Pieces.Count : 1);
            this.SavePath            = Path.Combine(savePath, baseDirectory);
            this.finishedPieces      = new Queue <int>();
            this.Monitor             = new ConnectionMonitor();
            this.InactivePeerManager = new InactivePeerManager(this);
            this.Peers          = new PeerManager();
            this.PieceManager   = new PieceManager();
            this.TrackerManager = new TrackerManager(this, InfoHash, announces);

            Mode = new StoppedMode(this);
            CreateRateLimiters();

            if (HasMetadata)
            {
                foreach (TorrentFile file in Torrent.Files)
                {
                    file.FullPath = Path.Combine(SavePath, file.Path);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Stops the TorrentManager
        /// </summary>
        public async Task StopAsync()
        {
            await ClientEngine.MainLoop;

            if (State == TorrentState.Error)
            {
                Error = null;
                Mode  = new StoppedMode(this);
                return;
            }

            if (State != TorrentState.Stopped)
            {
                Engine.DhtEngine.PeersFound -= DhtPeersFound;
                var stoppingMode = new StoppingMode(this);
                Mode = stoppingMode;

                await stoppingMode.WaitForStoppingToComplete();

                Mode = new StoppedMode(this);
                Engine.Stop();
            }
        }
Пример #11
0
        /// <summary>
        /// Stops the TorrentManager. The returned task completes as soon as the manager has fully stopped.
        /// </summary>
        public async Task StopAsync()
        {
            await ClientEngine.MainLoop;

            if (Mode is StoppingMode)
            {
                throw new TorrentException("The manager cannot be stopped while it is already in the Stopping state.");
            }

            if (State == TorrentState.Error)
            {
                Error = null;
                Mode  = new StoppedMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings);
            }
            else if (State != TorrentState.Stopped)
            {
                var stoppingMode = new StoppingMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings);
                Mode = stoppingMode;
                await stoppingMode.WaitForStoppingToComplete();

                Mode = new StoppedMode(this, Engine.DiskManager, Engine.ConnectionManager, Engine.Settings);
                Engine.Stop();
            }
        }