Exemple #1
0
        private TorrentStatus GetAndCheckTorrentStatus(TorrentClient torrentClient,
                                                       CancellationToken cancellationToken)
        {
            var torrentClientStatus = torrentClient.GetStatus(cancellationToken);

            _logger.LogTrace("status = " + torrentClientStatus.Status);

            if (torrentClientStatus.Status != "ok")
            {
                throw new DownloadFailureException("Torrent client failure.");
            }

            Assert.IsNotNull(torrentClientStatus.Data);
            Assert.IsNotNull(torrentClientStatus.Data.Torrents);
            Assert.AreEqual(1, torrentClientStatus.Data.Torrents.Length);

            var torrentStatus = torrentClientStatus.Data.Torrents[0];

            if (!string.IsNullOrEmpty(torrentStatus.Error))
            {
                throw new DownloadFailureException("Torrent client failure: " + torrentStatus.Error);
            }

            return(torrentStatus);
        }
Exemple #2
0
        public void Download(CancellationToken cancellationToken)
        {
            Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");

            DebugLogger.Log("Downloading.");

            _lastProgress = 0.0;

            try
            {
                _torrentClient = new TorrentClient(new UnityTorrentClientProcessStartInfoProvider());
                DownloadTorrentFile(cancellationToken);
                AddTorrent();
                WaitForTorrentDownload(cancellationToken);
                MoveDownloadedFile();
            }
            finally
            {
                if (_torrentClient != null)
                {
                    _torrentClient.Dispose();
                }
                Cleanup();
            }
        }
Exemple #3
0
        public void Download(CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("Downloading...");
                _logger.LogTrace("torrentFilePath = " + _torrentFilePath);
                _logger.LogTrace("destinationFilePath = " + _destinationFilePath);
                _logger.LogTrace("destinationDirectoryPath = " + DestinationDirectoryPath);

                Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");

                using (var tempDir = new TemporaryDirectory(DestinationDirectoryPath))
                {
                    using (var torrentClient = new TorrentClient(new UnityTorrentClientProcessStartInfoProvider()))
                    {
                        torrentClient.AddTorrent(_torrentFilePath, tempDir.Path, cancellationToken);

                        var timeoutWatch = new Stopwatch();
                        timeoutWatch.Start();

                        var    status          = GetAndCheckTorrentStatus(torrentClient, cancellationToken);
                        double initialProgress = status.Progress;
                        _logger.LogTrace("initialProgress = " + status.Progress);
                        var waitHandle = new AutoResetEvent(false);

                        OnDownloadProgressChanged(0);

                        using (cancellationToken.Register(() => waitHandle.Set()))
                        {
                            bool finished = false;

                            do
                            {
                                cancellationToken.ThrowIfCancellationRequested();

                                status = GetAndCheckTorrentStatus(torrentClient, cancellationToken);

                                _logger.LogTrace("progress = " + status.Progress);

                                CheckTimeout(timeoutWatch, status.Progress, initialProgress);

                                OnDownloadProgressChanged((long)(_totalBytes * status.Progress));

                                if (status.IsSeeding)
                                {
                                    finished = true;
                                }
                                else
                                {
                                    waitHandle.WaitOne(UpdateInterval);
                                }
                            } while (!finished);
                        }

                        cancellationToken.ThrowIfCancellationRequested();

                        var downloadedFilePath = GetDownloadedFilePath();

                        if (File.Exists(_destinationFilePath))
                        {
                            File.Delete(_destinationFilePath);
                        }

                        File.Move(downloadedFilePath, _destinationFilePath);
                    }
                }

                // TODO: move file
            }
            catch (Exception e)
            {
                _logger.LogError("Downloading has failed.", e);
                throw;
            }
        }