コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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);
            }
        }