S
S
Sergey Null2019-09-28 20:14:39
C++ / C#
Sergey Null, 2019-09-28 20:14:39

How to work with MonoTorrent correctly?

I have a rather big problem with MonoTorrent.
The problem is that the latest version of the library (1.0.2) finds only 2-3 sits and, as I assume, these people are with some special torrent tracker.
If I try to create my own torrent using uTorrent 3.5.5 and feed it to the program, then it simply does not find more than one seed.
Poking a little, I saw that the torrent has a lot of parameters and perhaps one of them is responsible for this.
There is not much documentation for this library, but with a replacement under the name. + half of the functionality is very different from the old one.
BitSharp can't figure it out.

Here is the program code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Threading;
using MonoTorrent.BEncoding;
using MonoTorrent.Client.Encryption;
using MonoTorrent.Client.Tracker;
using MonoTorrent.Client;
using MonoTorrent;

namespace TEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static string _programPath;
        static string _downloadPath;
        static string _fastResumeFile;
        static string _torrentPath;
        static ClientEngine _engine;
        static TorrentManager _manager;

        private void Button1_Click(object sender, EventArgs e)
        {
            _programPath = Environment.CurrentDirectory;
            OpenFileDialog OPF = new OpenFileDialog();
            OPF.Filter = "Torrent|*.torrent";
            if (OPF.ShowDialog() == DialogResult.OK)
            {
                _torrentPath = OPF.FileName;
            }
            FolderBrowserDialog OPF1 = new FolderBrowserDialog();
            if (OPF1.ShowDialog() == DialogResult.OK)
            {
                _downloadPath = OPF1.SelectedPath;
            }
            _fastResumeFile = _programPath + "\temp.data";

            Thread Download = new Thread(new ThreadStart(DoDownload));
            Download.Start();
        }

        private void DoDownload()
        {
            //int _port;
            //_port = 31337;

            Torrent _torrent;
            EngineSettings _engineSettings = new EngineSettings();
            TorrentSettings _torrentDef = new TorrentSettings();

            _engineSettings.SavePath = _downloadPath;
            //_engineSettings.ListenPort = _port;

            _engine = new ClientEngine(_engineSettings);

            BEncodedDictionary _fastResume;

            try
            {
                _fastResume = BEncodedValue.Decode<BEncodedDictionary>(File.ReadAllBytes(_fastResumeFile));
            }
            catch
            {
                _fastResume = new BEncodedDictionary();
            }

            try
            {
                _torrent = Torrent.Load(_torrentPath);
            }
            catch
            {
                label1.Text += "Decoding error\n";
                _engine.Dispose();
                return;
            }

            Invoke((Action)(() =>
            {
                //Инвормация о торренте
                label1.Text = "";
                label1.Text += "Created by: " + _torrent.CreatedBy + "\n";
                label1.Text += "Creation date: " + _torrent.CreationDate + "\n";
                label1.Text += "Comment: " + _torrent.Comment + "\n";
                label1.Text += "Publish URL: " + _torrent.PublisherUrl + "\n";
                label1.Text += "Size: " + _torrent.Size / 1049600 + "\n";
            }));

            //if (_fastResume.ContainsKey(_torrent.InfoHash))
            //    _manager = new TorrentManager(_torrent, _downloadPath, _torrentDef, new FastResume((BEncodedDictionary)_fastResume[_torrent.InfoHash]));
            //else
            //{
            //string Test = Convert.ToString(_torrent.SHA1);
            _manager = new TorrentManager(_torrent, _downloadPath, _torrentDef);
            //}

            _engine.Register(_manager);
            _manager.StartAsync();

            Thread SpeedTest = new Thread(new ThreadStart(Speed));
            SpeedTest.Start();


            /*
            ClientEngine engine;
            string savePath;
            // Create a basic ClientEngine without changing any settings
            engine = new ClientEngine(new EngineSettings());
            savePath = _downloadPath;
            // Open the .torrent file
            Torrent torrent* = Torrent.Load("C:\\123.torrent");
            

            // Create the manager which will download the torrent to savePath
            // using the default settings.
            TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());

            // Register the manager with the engine
            engine.Register(manager);

            // Begin the download. It is not necessary to call HashCheck on the manager
            // before starting the download. If a hash check has not been performed, the
            // manager will enter the Hashing state and perform a hash check before it
            // begins downloading.

            // If the torrent is fully downloaded already, calling 'Start' will place
            // the manager in the Seeding state.
            manager.Start();
            */
        }

        private void Speed()
        {
            int i = 0;
            bool _running = true;

            while (_running)
            {
                if ((i++) % 10 == 0)
                {
                    if (_manager.State == TorrentState.Stopped)
                    {
                        _running = false;
                    }

                    for (Int32 b = 1; b != 0; b++)
                    {
                        Invoke((Action)(() =>
                        {
                            label2.Text = "";
                            label2.Text += "Total Download Rate:   " + _engine.TotalDownloadSpeed / 1024.0 + " " + "kB/sec" + "\n";
                            label2.Text += "Total Upload Rate:   " + _engine.TotalUploadSpeed / 1024.0 + " " + "kB/sec" + "\n";
                            label2.Text += "Open Connections:   " + _engine.ConnectionManager.OpenConnections + "\n" + "\n";

                            label2.Text += "Name:   " + _manager.Torrent.Name + "\n";
                            label2.Text += "Progress:   " + _manager.Progress + "\n";
                            label2.Text += "Download Speed:   " + _manager.Monitor.DownloadSpeed / 1024.0 + " " + "kB/s" + "\n";
                            label2.Text += "Upload Speed:   " + _manager.Monitor.UploadSpeed / 1024.0 + " " + "kB/s" + "\n";
                            label2.Text += "Total Downloaded:   " + _manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0) + " " + "MB" + "\n";
                            label2.Text += "Total Uploaded:   " + _manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0) + " " + "MB" + "\n";
                            //label2.Text += "Tracker Status:   " + _manager.TrackerManager.CurrentTracker.State + "\n";
                            //label2.Text += "Warning Message:   " + _manager.TrackerManager.CurrentTracker.WarningMessage + "\n";
                            //label2.Text += "Failure Message:   " + _manager.TrackerManager.CurrentTracker.FailureMessage + "\n";
                        }));

                        System.Threading.Thread.Sleep(2000);
                    }
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2019-09-28
@tsklab

Start simple: 1024*1024=1048576

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question