Beispiel #1
0
        private void StartTorrentManager(PrioritizedTorrentManager torrentManager)
        {
            var frKey = torrentManager.Torrent?.InfoHash.ToHex();

            try
            {
                if (!string.IsNullOrEmpty(frKey) && FastResume.ContainsKey(frKey))
                {
                    torrentManager.LoadFastResume(new FastResume((BEncodedDictionary)FastResume[frKey]));
                }
            }
            catch (InvalidOperationException ex)
            {
                FastResume.Remove(frKey);
            }

            Engine.Register(torrentManager).Wait();

            torrentManager.TorrentStateChanged += TorrentManager_TorrentStateChanged;
            //torrentManager.PeersFound += TorrentManager_PeersFound;

            if (torrentManager.Complete || Settings.MaxConcurrent == 0)
            {
                torrentManager.StartAsync().Wait();
            }
            else if (manageDownloadsTimer == null)
            {
                manageDownloadsTimer = new Timer((x) => { ManageActiveDownloads(); }, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(-1));
            }
            else
            {
                manageDownloadsTimer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(-1));
            }
        }
Beispiel #2
0
        public void Download(Uri magnetUri, int priority = 0)
        {
            var magnetLink = MagnetLink.FromUri(magnetUri);
            PrioritizedTorrentManager torrentManager = null;

            lock (listLock)
            {
                if (this.Torrents.ContainsKey(magnetUri))
                {
                    return;
                }

                var torrent = GetTorrentFile(magnetLink);
                if (torrent != null)
                {
                    torrentManager = new PrioritizedTorrentManager(priority, torrent, Path.Combine(this.DownloadPath, magnetLink.Name), new TorrentSettings());
                }
                else
                {
                    torrentManager = new PrioritizedTorrentManager(priority, magnetLink, Path.Combine(this.DownloadPath, magnetLink.Name), new TorrentSettings(), Path.Combine(this.CachePath, $"{magnetLink.InfoHash.ToHex()}.torrent"));
                }

                //torrentManager = new PrioritizedTorrentManager(priority, magnetLink, Path.Combine(this.DownloadPath, magnetLink.Name), new TorrentSettings(), Path.Combine(this.CachePath, $"{magnetLink.InfoHash.ToHex()}.torrent"));

                this.Torrents.Add(magnetUri, torrentManager);
            }

            StartTorrentManager(torrentManager);
        }
Beispiel #3
0
        public IDownloadInfo Stop(Uri magnetUri, bool deleteFiles = false, bool remove = false)
        {
            if (magnetUri == null)
            {
                return(null);
            }

            PrioritizedTorrentManager torrentManager = null;

            lock (listLock)
            {
                torrentManager = this.Torrents.GetValueByKey(magnetUri);
                if (torrentManager == null)
                {
                    return(null);
                }

                if (remove)
                {
                    torrentManager.TorrentStateChanged -= TorrentManager_TorrentStateChanged;
                    this.Torrents.Remove(magnetUri);

                    try
                    {
                        var torrentPath = (!string.IsNullOrEmpty(torrentManager.Torrent.TorrentPath)) ? torrentManager.Torrent.TorrentPath : GetTorrentPath(torrentManager.Torrent.InfoHash);
                        File.Delete(torrentPath);
                    }
                    catch
                    {
                        // If we fail to delete the torrent file, it's no big deal - don't want to crash because of it.
                    }
                }
            }

            if (!torrentManager.State.Is(TorrentState.Stopped, TorrentState.Stopping))
            {
                torrentManager.StopAsync().Wait();
                Engine.Unregister(torrentManager).Wait();
                ManageActiveDownloads();
            }

            if (deleteFiles)
            {
                for (var i = 1; i <= 3; i++)
                {
                    try
                    {
                        if (torrentManager.SavePath != this.DownloadPath && Directory.Exists(torrentManager.SavePath))
                        {
                            Directory.Delete(torrentManager.SavePath, true);
                        }
                        break;
                    }
                    catch (IOException e) when((e.HResult & 0x0000FFFF) == 32)  // ERROR_SHARING_VIOLATION
                    {
                        if (i == 1)
                        {
                            // Maybe this helps with sharing violation, we shouldn't be causing any locks to the files in the SavePath ourselves but locks are happening and MonoTorrent supposedly closes all streams when you stop a torrent so..
                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                        }

                        Task.Delay(2500 * i).Wait();
                    }
                }
            }

            return(new DownloadInfo(magnetUri, torrentManager, torrentManager.Priority));
        }