public void Remove() { if (IsEmpty) { return; } active = false; string torrent = Path.Combine(torrentDir, handle.QueryStatus().Name + ".torrent"); if (File.Exists(torrent)) { File.Delete(torrent); } string fastResume = Path.Combine(fastResumeDir, handle.QueryStatus().Name + ".fastresume"); if (File.Exists(fastResume)) { File.Delete(fastResume); } }
public TorrentInfo(TorrentHandle t) { RanCommand = false; this.Torrent = t; this.StatusData = t.QueryStatus(); this.MainAppWindow = (App.Current.MainWindow as MainWindow); this.PickedMovieData = new IMDBSRSerializeable <IMovieDBSearchResult>(); this.Trackers = new ObservableCollection <TrackerInfo>(); if (t.HasMetadata) { SetupTorrent(); } PopulateTrackerList(); }
internal static ITorrent CreateFromHandle(TorrentHandle handle, ITorrentMetadataRepository metadataRepository) { using (handle) using (var file = handle.TorrentFile) using (var status = handle.QueryStatus()) { var t = new Torrent { InfoHash = handle.InfoHash.ToHex(), Name = status.Name, SavePath = status.SavePath, Size = file == null ? -1 : file.TotalSize, Progress = status.Progress, DownloadSpeed = status.DownloadRate, UploadSpeed = status.UploadRate, TotalDownloadedBytes = status.TotalDownload, TotalUploadedBytes = status.TotalUpload, State = (Common.BitTorrent.TorrentState)(int) status.State, Paused = status.Paused, Files = new ITorrentFile[file == null ? 0 : file.NumFiles], Peers = handle.GetPeerInfo().Select(Peer.CreateFromPeerInfo).ToArray() }; t.Label = metadataRepository.GetLabel(t.InfoHash); // If no torrent file (ie. downloading metadata) if (file == null) { return(t); } var progresses = handle.GetFileProgresses(); var priorities = handle.GetFilePriorities(); for (var i = 0; i < file.NumFiles; i++) { var entry = file.FileAt(i); var torrentFile = TorrentFile.CreateFromEntry(entry, progresses[i], priorities[i]); t.Files[i] = torrentFile; } return(t); } }
internal static ITorrent CreateFromHandle(TorrentHandle handle, ITorrentMetadataRepository metadataRepository) { using (handle) using (var file = handle.TorrentFile) using (var status = handle.QueryStatus()) { var t = new Torrent { InfoHash = handle.InfoHash.ToHex(), Name = status.Name, SavePath = status.SavePath, Size = file == null ? -1 : file.TotalSize, Progress = status.Progress, DownloadSpeed = status.DownloadRate, UploadSpeed = status.UploadRate, TotalDownloadedBytes = status.TotalDownload, TotalUploadedBytes = status.TotalUpload, State = (Common.BitTorrent.TorrentState) (int) status.State, Paused = status.Paused, Files = new ITorrentFile[file == null ? 0 : file.NumFiles], Peers = handle.GetPeerInfo().Select(Peer.CreateFromPeerInfo).ToArray() }; t.Label = metadataRepository.GetLabel(t.InfoHash); // If no torrent file (ie. downloading metadata) if (file == null) return t; var progresses = handle.GetFileProgresses(); var priorities = handle.GetFilePriorities(); for (var i = 0; i < file.NumFiles; i++) { var entry = file.FileAt(i); var torrentFile = TorrentFile.CreateFromEntry(entry, progresses[i], priorities[i]); t.Files[i] = torrentFile; } return t; } }
public void AddTorrentFromFile() { try { if (!IsDisposed) { byte[] bytes = File.ReadAllBytes(torrentFilePath); ti = new TorrentInfo(bytes); using (cMainForm.session) { cMainForm.session.ListenOn(6881, 6889); #region Torrent Parameters AddTorrentParams addParams = new AddTorrentParams(); if (unlimitedDownloadSpeed == false) { addParams.DownloadLimit = maxDownloadSpeed * 1024;//Transform byte to kB } if (unlimitedUploadSpeed == false) { addParams.UploadLimit = maxUploadSpeed * 1024; } addParams.TorrentInfo = ti; addParams.SavePath = string.Concat(saveFilePath, @"\", ti.Name); //This is weird, check it out later string resumeFile = Path.ChangeExtension(torrentFilePath, "resume"); if (File.Exists(resumeFile)) { addParams.ResumeData = File.ReadAllBytes(resumeFile);// Loading the resume data will load all torrents settings } #endregion // Add a torrent to the session and get a `TorrentHandle` in return. There is only one session that contains many handles. handle = cMainForm.session.AddTorrent(addParams); handle.SetPriority(128); if (handle.NeedSaveResumeData() == true) { SaveResumeData(); } SetLimitString(); cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.AddToMainList(ti.Name, ti.TotalSize, torrentIndex)); while (true) { if (pause == true) { PauseHandle(); } else if (handle.IsPaused == true && pause == false) { ResumeHandle(); } // Get a `TorrentStatus` instance from the handle. TorrentStatus status = handle.QueryStatus(); IEnumerable <PeerInfo> connectedPeers; connectedPeers = handle.GetPeerInfo(); if (status.IsSeeding) { break; } elapsedTime = status.ActiveTime; downloaded = status.Progress * 100; uploaded = status.TotalUpload * 100; uploadSpeed = status.UploadRate; downloadSpeed = status.DownloadRate; currentStatus = status.State.ToString(); UpdateProgressBar(downloaded); if (connectedPeers.Count() > 0 && cMainForm.mainForm.mainToolStripBottom.SelectedIndex == 2)//2 is client tab { cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.AddToClientList(connectedPeers, torrentIndex)); } if (cMainForm.mainForm.mainToolStripBottom.SelectedIndex == 0)//0 is info tab { cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.UpdateInfoTabData(downloaded.ToString(), uploaded.ToString(), uploadSpeed, downloadSpeed, formattedDownLimit, formattedUpLimit, currentStatus, elapsedTime.ToString(), torrentIndex)); } ; cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.EditMainList(downloaded.ToString(), uploadSpeed, downloadSpeed, currentStatus, torrentIndex)); Thread.Sleep(100); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "AddTorrentFromFile -- " + ex, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void AddTorrentFromMagnet() { try { if (!IsDisposed) { using (cMainForm.session) { MagnetLink magnet = null; try { magnet = new MagnetLink(magnetLink); } catch { MessageBox.Show("Invalid magnet link", "Error"); return; } cMainForm.session.ListenOn(6881, 6889); #region Torrent Parameters AddTorrentParams addParams = new AddTorrentParams(); if (unlimitedDownloadSpeed == false) { addParams.DownloadLimit = maxDownloadSpeed * 1024;//Transform byte to kB } if (unlimitedUploadSpeed == false) { addParams.UploadLimit = maxUploadSpeed * 1024; } if (magnet.Name != null) { addParams.Name = magnet.Name; } addParams.Url = magnetLink; addParams.SavePath = saveFilePath; //This is weird, check it out later #endregion // Add a torrent to the session and get a `TorrentHandle` in return. There is only one session that contains many handles. handle = cMainForm.session.AddTorrent(addParams); handle.SetPriority(128); SetLimitString(); cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.AddToMainList(magnet.Name, handle.QueryStatus().TotalPayloadDownload, torrentIndex)); //ti doesn't exist here, you need to use info hash to get the name and size while (true) { if (pause == true) { PauseHandle(); } else if (handle.IsPaused == true && pause == false) { ResumeHandle(); } // Get a `TorrentStatus` instance from the handle. TorrentStatus status = handle.QueryStatus(); IEnumerable <PeerInfo> connectedPeers; connectedPeers = handle.GetPeerInfo(); if (status.IsSeeding) //If download is finished { break; } elapsedTime = status.ActiveTime; downloaded = status.Progress * 100; uploadSpeed = status.UploadRate; downloadSpeed = status.DownloadRate; currentStatus = status.State.ToString(); UpdateProgressBar(downloaded); if (connectedPeers.Count() > 0 && cMainForm.mainForm.mainToolStripBottom.SelectedIndex == 2)//2 is client tab { cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.AddToClientList(connectedPeers, torrentIndex)); } if (cMainForm.mainForm.mainToolStripBottom.SelectedIndex == 0)//0 is info tab { cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.UpdateInfoTabData(downloaded.ToString(), uploaded.ToString(), uploadSpeed, downloadSpeed, formattedDownLimit, formattedUpLimit, currentStatus, elapsedTime.ToString(), torrentIndex)); } if (cMainForm.mainForm.mainToolStripBottom.SelectedIndex == 1) //1 is file tab { cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.AddFilesToList(GetFilePriorities(), torrentIndex)); //Should send list of files instead of priorities but i don't know how to get the files :( } cMainForm.mainForm.RunOnUIThread(() => cMainForm.mainForm.EditMainList(downloaded.ToString(), uploadSpeed, downloadSpeed, currentStatus, torrentIndex)); Thread.Sleep(1); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "AddTorrentFromMagnet -- " + ex, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Download a movie /// </summary> /// <param name="movie">The movie to download</param> private async Task DownloadMovie(MovieFullDetails movie) { using (Session session = new Session()) { IsDownloadingMovie = true; // Inform subscribers we're actually loading a movie OnDownloadingMovie(new EventArgs()); // Listening to a port which is randomly between 6881 and 6889 session.ListenOn(6881, 6889); var addParams = new AddTorrentParams { // Where do we save the video file SavePath = Constants.MovieDownloads, // At this time, no quality selection is available in the interface, so we take the lowest Url = movie.Torrents.Aggregate((i1, i2) => (i1.SizeBytes < i2.SizeBytes ? i1 : i2)).Url }; TorrentHandle handle = session.AddTorrent(addParams); // We have to download sequentially, so that we're able to play the movie without waiting handle.SequentialDownload = true; bool alreadyBuffered = false; while (IsDownloadingMovie) { TorrentStatus status = handle.QueryStatus(); double progress = status.Progress * 100.0; // Inform subscribers of our progress OnLoadingMovieProgress(new MovieLoadingProgressEventArgs(progress, status.DownloadRate / 1024)); // We consider 2% of progress is enough to start playing if (progress >= Constants.MinimumBufferingBeforeMoviePlaying && !alreadyBuffered) { try { // We're looking for video file foreach (string directory in Directory.GetDirectories(Constants.MovieDownloads)) { foreach ( string filePath in Directory.GetFiles(directory, "*" + Constants.VideoFileExtension) ) { // Inform subscribers we have finished buffering the movie OnBufferedMovie(new MovieBufferedEventArgs(filePath)); alreadyBuffered = true; } } } catch (Exception e) { Console.WriteLine(e.Message); } } // Let sleep for a second before updating the torrent status await Task.Delay(1000, CancellationDownloadingToken.Token).ContinueWith(_ => { if (CancellationDownloadingToken.IsCancellationRequested && session != null) { OnStoppedDownloadingMovie(new EventArgs()); IsDownloadingMovie = false; session.RemoveTorrent(handle, true); } }).ConfigureAwait(false); } } }