Пример #1
0
        public virtual void Setup()
        {
            // Yes, this is horrible. Deal with it.
            rig   = TestRig.CreateMultiFile();
            peers = new List <PeerId>();

            manager = new PieceManager();
            manager.ChangePicker((standardPicker = new StandardPicker()), rig.Manager.Bitfield, rig.Manager.Torrent.Files);
            this.picker = manager.Picker;

            peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
            for (int i = 0; i < 20; i++)
            {
                PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager);
                p.SupportsFastPeer = true;
                peers.Add(p);
            }
        }
Пример #2
0
        /// <summary>
        /// Starts the TorrentManager
        /// </summary>
        public async Task StartAsync()
        {
            await ClientEngine.MainLoop;

            CheckRegisteredAndDisposed();

            Engine.Start();
            // If the torrent was "paused", then just update the state to Downloading and forcefully
            // make sure the peers begin sending/receiving again
            if (State == TorrentState.Paused)
            {
                Mode = new DownloadMode(this);
                return;
            }

            if (!HasMetadata)
            {
                Mode = new MetadataMode(this, torrentSave);
                StartDHT();
                return;
            }

            VerifyHashState();
            // If the torrent has not been hashed, we start the hashing process then we wait for it to finish
            // before attempting to start again
            if (!HashChecked)
            {
                if (State != TorrentState.Hashing)
                {
                    await HashCheckAsync(true);
                }
                return;
            }

            if (State == TorrentState.Seeding || State == TorrentState.Downloading)
            {
                return;
            }

            // We need to announce before going into Downloading mode, otherwise we will
            // send a regular announce instead of a 'Started' announce.
            if (TrackerManager.CurrentTracker != null)
            {
                if (TrackerManager.CurrentTracker.CanScrape)
                {
                    TrackerManager.Scrape();
                }
                TrackerManager.Announce(TorrentEvent.Started); // Tell server we're starting
            }

            if (Complete && Settings.InitialSeedingEnabled && ClientEngine.SupportsInitialSeed)
            {
                Mode = new InitialSeedingMode(this);
            }
            else
            {
                Mode = new DownloadMode(this);
            }

            Engine.Broadcast(this);

            StartDHT();

            StartTime = DateTime.Now;
            PieceManager.Reset();
        }
Пример #3
0
 internal void ChangePicker(PiecePicker picker)
 {
     Check.Picker(picker);
     PieceManager.ChangePicker(new IgnoringPicker(UnhashedPieces, picker), Bitfield, Torrent);
 }
Пример #4
0
        internal void ChangePicker(PiecePicker picker)
        {
            Check.Picker(picker);

            PieceManager.ChangePicker(picker, Bitfield, Torrent.Files);
        }
Пример #5
0
        /// <summary>
        /// Starts the TorrentManager
        /// </summary>
        public async Task StartAsync()
        {
            await ClientEngine.MainLoop;

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

            CheckRegisteredAndDisposed();

            Engine.Start();
            // If the torrent was "paused", then just update the state to Downloading and forcefully
            // make sure the peers begin sending/receiving again
            if (State == TorrentState.Paused)
            {
                Mode = new DownloadMode(this);
                return;
            }

            if (!HasMetadata)
            {
                Mode = new MetadataMode(this, torrentSave);
                StartDHT();
                return;
            }

            await VerifyHashState();

            // If the torrent has not been hashed, we start the hashing process then we wait for it to finish
            // before attempting to start again
            if (!HashChecked)
            {
                // Deliberately do not wait for the entire hash check to complete in this scenario.
                // Here we want to Task returned by this method to be 'Complete' as soon as the
                // TorrentManager moves to any state that is not Stopped. The idea is that 'StartAsync'
                // will simply kick off 'Hashing' mode, or 'MetadataMode', or 'InitialSeeding' mode
                // and then the user is free to call StopAsync etc whenever they want.
                if (State != TorrentState.Hashing)
                {
                    _ = HashCheckAsync(true);
                }
                return;
            }

            if (State == TorrentState.Seeding || State == TorrentState.Downloading)
            {
                return;
            }

            // We need to announce before going into Downloading mode, otherwise we will
            // send a regular announce instead of a 'Started' announce.
            if (TrackerManager.CurrentTracker != null)
            {
                if (TrackerManager.CurrentTracker.CanScrape)
                {
                    _ = TrackerManager.Scrape();
                }
                _ = TrackerManager.Announce(TorrentEvent.Started); // Tell server we're starting
            }

            if (Complete && Settings.InitialSeedingEnabled && ClientEngine.SupportsInitialSeed)
            {
                Mode = new InitialSeedingMode(this);
            }
            else
            {
                Mode = new DownloadMode(this);
            }

            Engine.Broadcast(this);

            StartDHT();

            StartTime = DateTime.Now;
            PieceManager.Reset();
        }