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); } }