Esempio n. 1
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();
        }
        /// <summary>
        /// Starts the TorrentManager
        /// </summary>
        public void Start()
        {
            ClientEngine.MainLoop.QueueWait((MainLoopTask) delegate {
                CheckRegisteredAndDisposed();

                this.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 (this.State == TorrentState.Paused)
                {
                    Mode = new DownloadMode(this);
                    return;
                }

                if (!HasMetadata)
                {
                    Mode = new MetadataMode(this, torrentSave);
#if !DISABLE_DHT
                    StartDHT();
#endif
                    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)
                    {
                        HashCheck(true);
                    }
                    return;
                }

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

                if (TrackerManager.CurrentTracker != null)
                {
                    //if (this.trackerManager.CurrentTracker.CanScrape)
                    //    this.TrackerManager.Scrape();
                    this.trackerManager.Announce(TorrentEvent.Started); // Tell server we're starting
                }

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

#if !DISABLE_DHT
                StartDHT();
#endif
                this.startTime = DateTime.Now;
                this.pieceManager.Reset();

                ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromSeconds(2), delegate {
                    if (State != TorrentState.Downloading && State != TorrentState.Seeding)
                    {
                        return(false);
                    }
                    pieceManager.Picker.CancelTimedOutRequests();
                    return(true);
                });
            });
        }
Esempio n. 3
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();
        }