/// <summary> /// Allows to start a file download easily /// </summary> /// <param name="magnet">Magnet-link to search file</param> /// <param name="fullSystemPath">optional complete file path to be saved</param> /// <param name="sources">optional set of sources to use</param> public DownloadItem DownloadFile(Magnet magnet, string fullSystemPath = null, IEnumerable <Source> sources = null) { if (string.IsNullOrEmpty(fullSystemPath)) { fullSystemPath = Path.Combine(Settings.PathDownload, magnet.FileName); } else { if (Directory.Exists(fullSystemPath)) { throw new ArgumentException("Provide full system path to the file, not the directory", fullSystemPath); } } if (!FileHelper.IsValidFilePath(fullSystemPath)) { throw new InvalidFileNameException("Storage path of the file is invalid") { FileName = fullSystemPath } } ; if (!FileHelper.IsValidFileName(Path.GetFileName(fullSystemPath))) { throw new InvalidFileNameException("Storage path of the file is invalid") { FileName = fullSystemPath } } ; if (!string.IsNullOrEmpty(magnet.FileName)) { if (!FileHelper.IsValidFileName(magnet.FileName)) { throw new InvalidFileNameException("Name of the file is invalid") { FileName = magnet.FileName } } ; } var di = new DownloadItem(magnet) { SaveTargets = new List <string> { fullSystemPath } }; if (sources != null) { di.Sources.AddRange(sources); } var result = SearchManager.GetHubResultByTTH(di.Magnet.TTH); if (result != null) { di.Sources.AddRange(result.Sources); } if (DownloadManager.AddDownload(di) && Active) { if (di.Sources.Count == 0) { lock (DownloadManager.SyncRoot) SearchManager.Search(di); } else { TransferManager.RequestTransfers(di); } } return(di); }
/// <summary> /// Performs engine control operations, need to be called periodically /// - Checks hub connections (and restores if needed) /// - Sends connection requests if needed /// - Sends search requests /// - Disconnects timeouted connections /// </summary> public void Update() { foreach (var hub in Hubs) { if (Settings.ReconnectTimeout != 0 && hub.IdleSeconds > Settings.ReconnectTimeout) { Logger.Info("{0}: Hub inactivity timeout reached [{1}]. Reconnecting", hub.Settings.HubName, Settings.ReconnectTimeout); hub.Disconnect(); hub.StartAsync(); } if (Settings.KeepAliveTimeout != 0 && hub.Active && hub.IdleSeconds > Settings.KeepAliveTimeout) { hub.KeepAlive(); } } // no need to do anything before we have at least one connection if (!Active) { return; } if (!Monitor.TryEnter(_updateSynRoot)) { Logger.Warn("Unable to update engine, it is locked"); return; } try { using (new PerfLimit("EngineUpdate requests", 300)) foreach (var downloadItem in DownloadManager.EnumeratesItemsForProcess()) { if (downloadItem.Priority == DownloadPriority.Pause) { continue; } if (TransferManager.RequestsAvailable) { TransferManager.RequestTransfers(downloadItem); } SearchManager.CheckItem(downloadItem); } using (new PerfLimit("EngineUpdate Transfers", 300)) TransferManager.Update(); using (new PerfLimit("EngineUpdate Searches", 300)) SearchManager.CheckPendingSearches(); } catch (Exception x) { Logger.Error("Exception when update: {0}", x.Message); } finally { Monitor.Exit(_updateSynRoot); } }