private static void DownloadTorrent(string[] args) { if (args.Length < 4) { Console.Error.WriteLine("You must pass at least 3 arguments to create a torrent: torrent path, download path and listen port"); return; } string torrentPath = args[1].Trim(); string downloadPath = args[2].Trim(); string listenPortText = args[3].Trim(); torrentPath = Path.GetFullPath(torrentPath); downloadPath = Path.GetFullPath(downloadPath); int listenPort; if (!int.TryParse(listenPortText, out listenPort) || listenPort < 0) { Console.Error.WriteLine("The listen port is invalid: {0}", listenPortText); return; } if (!File.Exists(torrentPath)) { Console.Error.WriteLine("The torrent doesn't exist: {0}", torrentPath); return; } Prefs.Peer.ListenPort = listenPort; Prefs.Torrent.AllocateFullFileSizes = true; var torrentMetaData = new TorrentMetaData(); torrentMetaData.LoadFromFile(torrentPath); var consoleLogger = new ConsoleLogger(); consoleLogger.Level = LogLevel.Info; var fileLogger = new FileLogger("swifter.log"); fileLogger.Level = LogLevel.Debug; var groupLogger = new GroupLogger(consoleLogger, fileLogger); Log.Logger = groupLogger; TorrentEngine.Initialize(); try { var torrent = new Torrent(torrentMetaData, downloadPath); torrent.Start(); while (true) { var keyInfo = Console.ReadKey(true); if (keyInfo.Key == ConsoleKey.Escape) { break; } if (keyInfo.Key == ConsoleKey.Enter) { string peerEndPointText = Console.ReadLine(); var peerEndPoint = ParseEndPoint(peerEndPointText); if (peerEndPoint != null) { var peerInfo = new PeerInfo(peerEndPoint); torrent.AddPeer(peerInfo); } else { Log.LogError("[Console] Invalid end-point: {0}", peerEndPointText); } } else if (keyInfo.Key == ConsoleKey.Spacebar) { string downloadRate = Torrent.GetHumanReadableSpeed(torrent.SessionDownloadRate); string uploadRate = Torrent.GetHumanReadableSpeed(torrent.SessionUploadRate); string downloadLeft = Torrent.GetHumanReadableSize(torrent.BytesLeftToDownload); Log.LogInfo("[Console] Download Speed: {0} Upload Speed: {1} Left : {2}", downloadRate, uploadRate, downloadLeft); } } torrent.Stop(); } finally { TorrentEngine.Uninitialize(); } }
private async void OnTimedEvent(object source, ElapsedEventArgs e) { try { if (TorrentEngine == null) { throw new Exception("Torrent Engine has not been started yet"); } TimerWorker.Stop(); this.CurrentStatus = QueueProcessorStatus.Seeding; Logger.LogInformation($"Running AT: {DateTime.UtcNow} UTC"); using (var scope = ScopeFactory.CreateScope()) { var Db = scope.ServiceProvider.GetRequiredService <HostedRepository>(); var allQueues = Db.BitClientProcessorQueues.Where(x => x.ExecutionStatus == ExecutionStatus.Queued).ToList(); if (allQueues != null && allQueues.Count > 0) { foreach (var queue in allQueues) { //Mark as Seeding queue.ExecutionStatus = ExecutionStatus.Seeding; queue.LastUpdatedTimeUTC = DateTime.UtcNow; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); try { Logger.LogInformation($"Processing, Queue Tracking ID:{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); #region ExecutionMain // Load a .torrent file into memory if (queue.TorrentFileBytes == null) { throw new Exception("Torrent Content in Bytes is Empty"); } Torrent torrent = await Torrent.LoadAsync(queue.TorrentFileBytes); // Set all the files to not download //foreach (TorrentFile file in torrent.Files) // file.Priority = Priority.High; ////Set First File Prioroty //torrent.Files[1].Priority = Priority.Highest; string subPath = $"\\{queue.UserId}\\{queue.TrackingId}".ToUpper(); string saveDirectory = GetTorrentDownloadPath() + subPath; UserTorrentManager manager = new UserTorrentManager(torrent, saveDirectory, new TorrentSettings()); //Asssign manager.UserId = queue.UserId; manager.TrackingId = queue.TrackingId; manager.AvailableDownloadPath = $"{this.options.TorrentDownloadPath}{subPath}"; await TorrentEngine.Register(manager); Db.UserTorrentManagers.Enqueue(manager); // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading) PiecePicker picker = new StandardPicker(); picker = new PriorityPicker(picker); await manager.ChangePickerAsync(picker); await this.TorrentEngine.StartAll(); #endregion queue.ExecutionFeedBack = "Torrent added to Queue Successfully"; queue.ExecutionStatus = ExecutionStatus.Processed; Logger.LogInformation($"Torrent added to Queue Successfully, Tracking Id :{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); } catch (Exception ex) { queue.ErrorsCount++; if (queue.ErrorsCount >= 5) { queue.ExecutionStatus = ExecutionStatus.ErrorOccurred; queue.ExecutionFeedBack = ex.Message; } else { queue.ExecutionStatus = ExecutionStatus.Queued; queue.ExecutionFeedBack = $"Requeued Attempt ({queue.ErrorsCount})"; } Logger.LogError($"ERROR: {ex.Message}, Tracking Id :{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); } //**************** DONE ********************* stopWatch.Stop(); queue.LastUpdatedTimeUTC = DateTime.UtcNow; queue.ExecutionInMiliseconds = stopWatch.ElapsedMilliseconds; } } //Exception Count Reset _ExceptionCounts = 0; } } catch (Exception ex) { _ExceptionCounts++; Logger.LogError($"Processing Exception: {ex.Message}"); } finally { ContinueTimer(); } }