コード例 #1
0
ファイル: Torrent.cs プロジェクト: WebTorrentX/WebTorrentX
        public async Task StartAsync()
        {
            var engine = new ClientEngine();

            MonoTorrent.Torrent torrent = null;

            if (File.Exists(TorrentFileName))
            {
                torrent = await MonoTorrent.Torrent.LoadAsync(TorrentFileName);
            }
            else if (!Url.StartsWith("magnet"))
            {
                torrent = MonoTorrent.Torrent.Load(new Uri(Url), Path.Combine(torrentDir, Path.GetTempFileName() + ".torrent"));
            }

            manager = torrent is null ? await engine.AddStreamingAsync(MonoTorrent.MagnetLink.Parse(Url), DownloadFolder)
                 : await engine.AddStreamingAsync(torrent, DownloadFolder);

            await manager.StartAsync();

            if (!manager.HasMetadata)
            {
                await manager.WaitForMetadataAsync();
            }
        }
コード例 #2
0
ファイル: Mytorrent.cs プロジェクト: codenumb/ytsmovies
        public async Task initDownloadMan()
        {
            string  torFilePath;
            Torrent torrent = null;
            bool    tmp     = false;

            /*Load fast resume file*/
            try
            {
                if (File.Exists(fastResumeFile))
                {
                    fastResume = BEncodedValue.Decode <BEncodedDictionary>(File.ReadAllBytes(fastResumeFile));
                }
            }
            catch
            {
                Debug.WriteLine("error here");
            }
            /*open torrentfile dir and load all the torrents*/
            if (System.IO.Directory.Exists(torrentsInfoPath))
            {
                string[] movies = System.IO.Directory.GetFiles(torrentsInfoPath);
                foreach (string m in movies)
                {
                    TorrentInfoModel torInfo = new TorrentInfoModel();
                    torInfo.ReadFromFile(m);
                    //readTorrentInfoFromFile(torInfo, m);
                    torFilePath = Path.Combine(torrentsPath, torInfo.torrentFileName);
                    if (System.IO.File.Exists(torFilePath))
                    {
                        Console.WriteLine("adding f= {0}", torFilePath);
                        torrent = await Torrent.LoadAsync(torFilePath);

                        TorrentManager manager = new TorrentManager(torrent, downloadsPath, new TorrentSettings());
                        manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.InfoHash.ToHex()]));
                        await engine.Register(manager);

                        torInfo.manager         = manager;
                        torInfo.torrentFileName = torFilePath;
                        TorrentInfoList.Add(torInfo);
                        if (torInfo.status == TorrentState.Downloading.ToString())
                        {
                            await manager.StartAsync();
                        }
                        initTorrentEvents(torInfo); // hook all the events
                        tmp = true;
                    }
                }
                if (tmp)
                {
                    monitorTimer.Enabled = true;
                }
                Debug.WriteLine("torrent init exit!");
                //await engine.StartAllAsync();
            }
        }
コード例 #3
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Torrent torrent = Torrent.Load(entry.FilePath + ".torrent");

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

            engine = new ClientEngine();

            // Register the manager with the engine
            this.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.StartAsync();

            while (progressBar1.Value < 100)
            {
                BeginInvoke(new MethodInvoker(delegate
                {
                    progressBar1.Value = (int)manager.Bitfield.PercentComplete;
                    label2.Text        = manager.Bitfield.PercentComplete.ToString() + "%";
                }));

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

            BeginInvoke(new MethodInvoker(delegate
            {
                button1.Text = "Загружено";
            }));



            manager.StopAsync();
            engine.Dispose();
            manager.Dispose();
        }
コード例 #4
0
        async Task StreamAsync(TorrentManager manager, CancellationToken token)
        {
            manager.PeerConnected += (o, e) => {
                lock (Listener)
                    Listener.WriteLine($"Connection succeeded: {e.Peer.Uri}");
            };
            manager.ConnectionAttemptFailed += (o, e) => {
                lock (Listener)
                    Listener.WriteLine(
                        $"Connection failed: {e.Peer.ConnectionUri} - {e.Reason}");
            };
            // Every time a piece is hashed, this is fired.
            manager.PieceHashed += delegate(object o, PieceHashedEventArgs e) {
                lock (Listener)
                    Listener.WriteLine($"Piece Hashed: {e.PieceIndex} - {(e.HashPassed ? "Pass" : "Fail")}");
            };

            // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
            manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs e) {
                lock (Listener)
                    Listener.WriteLine($"OldState: {e.OldState} NewState: {e.NewState}");
            };

            // Every time the tracker's state changes, this is fired
            manager.TrackerManager.AnnounceComplete += (sender, e) => {
                Listener.WriteLine($"{e.Successful}: {e.Tracker}");
            };

            await manager.StartAsync();

            PrintStatsAsync(token);
            await manager.WaitForMetadataAsync(token);

            var largestFile = manager.Files.OrderBy(t => t.Length).Last();
            var stream      = await manager.StreamProvider.CreateHttpStreamAsync(largestFile, token);

            var process = Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe", stream.Uri.ToString());

            using var disposer = token.Register(process.Kill);
            process.WaitForExit();
        }
コード例 #5
0
        private async void ToggleWillDownload()
        {
            if (WillDownload == true)
            {
                await TorrentClientAPI.engine.Register(Tmanager);

                await Tmanager.StartAsync();
            }
            else
            {
                try
                {
                    await Tmanager.StopAsync();

                    await TorrentClientAPI.engine.Unregister(Tmanager);
                }
                catch (Exception)
                {
                }
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: swagfin/BitSharp
        private async static void BegingTorrentDownload()
        {
            try
            {
                Console.WriteLine("Processing Torrent......");

                EngineSettings settings = new EngineSettings();
                settings.AllowedEncryption = EncryptionTypes.All;
                settings.SavePath          = Path.Combine(Environment.CurrentDirectory, "torrents");

                if (!Directory.Exists(settings.SavePath))
                {
                    Directory.CreateDirectory(settings.SavePath);
                }

                //End Point settings moved
                settings.ReportedAddress = new IPEndPoint(IPAddress.Any, 6969);

                var engine = new ClientEngine(settings);

                //engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, 6969));

                Torrent torrent = Torrent.Load("C:/9D4A9495BE35D97B13E60D143F37CC38378D8233.torrent");

                TorrentManager manager = new TorrentManager(torrent, engine.Settings.SavePath, new TorrentSettings());

                await engine.Register(manager);

                await manager.StartAsync();

                Console.WriteLine("COMPLTED...SUCCESS");

                //Lunch Direvtory
                // Process.Start(settings.SavePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR OCCURRED: {ex.Message}");
            }
        }
コード例 #7
0
        private static async Task StartEngine(IEnumerable <Uri> magnetUris)
        {
            // Create the settings which the engine will use
            // downloadsPath - this is the path where we will save all the files to
            // port - this is the port we listen for connections on
            EngineSettings engineSettings = new EngineSettings
            {
                SavePath   = downloadsPath,
                ListenPort = port
            };

            //engineSettings.GlobalMaxUploadSpeed = 30 * 1024;
            //engineSettings.GlobalMaxDownloadSpeed = 100 * 1024;
            //engineSettings.MaxReadRate = 1 * 1024 * 1024;

            // Create the default settings which a torrent will have.
            TorrentSettings torrentDefaults = new TorrentSettings();

            // Create an instance of the engine.
            engine = new ClientEngine(engineSettings);

            byte[] nodes = Array.Empty <byte>();
            try
            {
                nodes = File.ReadAllBytes(dhtNodeFile);
            }
            catch
            {
                Console.WriteLine("No existing dht nodes could be loaded");
            }

            DhtEngine dht = new DhtEngine(new IPEndPoint(IPAddress.Any, port));
            await engine.RegisterDhtAsync(dht);

            // This starts the Dht engine but does not wait for the full initialization to
            // complete. This is because it can take up to 2 minutes to bootstrap, depending
            // on how many nodes time out when they are contacted.
            await engine.DhtEngine.StartAsync(nodes);

            // If the SavePath does not exist, we want to create it.
            if (!Directory.Exists(engine.Settings.SavePath))
            {
                Directory.CreateDirectory(engine.Settings.SavePath);
            }

            // If the torrentsPath does not exist, we want to create it
            if (!Directory.Exists(torrentsPath))
            {
                Directory.CreateDirectory(torrentsPath);
            }

            BEncodedDictionary fastResume;

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

            // For each magnet uri, load it into the engine.
            foreach (Uri magnetUri in magnetUris)
            {
                MagnetLink magnet = MagnetLink.FromUri(magnetUri);

                // When any preprocessing has been completed, you create a TorrentManager
                // which you then register with the engine.
                TorrentManager manager = new TorrentManager(magnet, downloadsPath, torrentDefaults, torrentsPath);
                if (fastResume.ContainsKey(magnet.InfoHash.ToHex()))
                {
                    manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[magnet.InfoHash.ToHex()]));
                }
                await engine.Register(manager);

                // Store the torrent manager in our list so we can access it later
                torrents.Add(manager);
                manager.PeersFound += manager_PeersFound;
            }

            // If we loaded no torrents, just exit.
            if (torrents.Count == 0)
            {
                Console.WriteLine("No torrents found");
                engine.Dispose();
                return;
            }

            // For each torrent manager we loaded and stored in our list, hook into the events
            // in the torrent manager and start the engine.
            foreach (TorrentManager manager in torrents)
            {
                manager.PeerConnected += (o, e) => {
                    lock (listener)
                        listener.WriteLine($"Connection succeeded: {e.Peer.Uri}");
                };
                manager.ConnectionAttemptFailed += (o, e) => {
                    lock (listener)
                        listener.WriteLine(
                            $"Connection failed: {e.Peer.ConnectionUri} - {e.Reason} - {e.Peer.AllowedEncryption}");
                };
                // Every time a piece is hashed, this is fired.
                manager.PieceHashed += (object o, PieceHashedEventArgs e) =>
                {
                    lock (listener)
                        listener.WriteLine($"Piece Hashed: {e.PieceIndex} - {(e.HashPassed ? "Pass" : "Fail")}");
                };

                // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
                manager.TorrentStateChanged += (object o, TorrentStateChangedEventArgs e) =>
                {
                    lock (listener)
                        listener.WriteLine($"OldState: {e.OldState} NewState: {e.NewState}");
                };

                // Every time the tracker's state changes, this is fired
                manager.TrackerManager.AnnounceComplete += (sender, e) => {
                    listener.WriteLine($"{e.Successful}: {e.Tracker}");
                };

                // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding
                await manager.StartAsync();
            }

            // While the torrents are still running, print out some stats to the screen.
            // Details for all the loaded torrent managers are shown.
            int           i       = 0;
            bool          running = true;
            StringBuilder sb      = new StringBuilder(1024);

            while (running)
            {
                if ((i++) % 10 == 0)
                {
                    sb.Remove(0, sb.Length);
                    running = torrents.Exists(m => m.State != TorrentState.Stopped);

                    AppendFormat(sb, "Total Download Rate: {0:0.00}kB/sec", engine.TotalDownloadSpeed / 1024.0);
                    AppendFormat(sb, "Total Upload Rate:   {0:0.00}kB/sec", engine.TotalUploadSpeed / 1024.0);
                    AppendFormat(sb, "Disk Read Rate:      {0:0.00} kB/s", engine.DiskManager.ReadRate / 1024.0);
                    AppendFormat(sb, "Disk Write Rate:     {0:0.00} kB/s", engine.DiskManager.WriteRate / 1024.0);
                    AppendFormat(sb, "Total Read:         {0:0.00} kB", engine.DiskManager.TotalRead / 1024.0);
                    AppendFormat(sb, "Total Written:      {0:0.00} kB", engine.DiskManager.TotalWritten / 1024.0);
                    AppendFormat(sb, "Open Connections:    {0}", engine.ConnectionManager.OpenConnections);

                    foreach (TorrentManager manager in torrents)
                    {
                        AppendSeparator(sb);
                        AppendFormat(sb, "State:           {0}", manager.State);
                        AppendFormat(sb, "Name:            {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name);
                        AppendFormat(sb, "Progress:           {0:0.00}", manager.Progress);
                        AppendFormat(sb, "Download Speed:     {0:0.00} kB/s", manager.Monitor.DownloadSpeed / 1024.0);
                        AppendFormat(sb, "Upload Speed:       {0:0.00} kB/s", manager.Monitor.UploadSpeed / 1024.0);
                        AppendFormat(sb, "Total Downloaded:   {0:0.00} MB", manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0));
                        AppendFormat(sb, "Total Uploaded:     {0:0.00} MB", manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0));
                        MonoTorrent.Client.Tracker.ITracker tracker = manager.TrackerManager.CurrentTracker;
                        //AppendFormat(sb, "Tracker Status:     {0}", tracker == null ? "<no tracker>" : tracker.State.ToString());
                        AppendFormat(sb, "Warning Message:    {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage);
                        AppendFormat(sb, "Failure Message:    {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage);
                        if (manager.PieceManager != null)
                        {
                            AppendFormat(sb, "Current Requests:   {0}", await manager.PieceManager.CurrentRequestCountAsync());
                        }

                        foreach (PeerId p in await manager.GetPeersAsync())
                        {
                            AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Uri,
                                         p.Monitor.DownloadSpeed / 1024.0,
                                         p.AmRequestingPiecesCount,
                                         p.Monitor.UploadSpeed / 1024.0);
                        }

                        AppendFormat(sb, "", null);
                        if (manager.Torrent != null)
                        {
                            foreach (TorrentFile file in manager.Torrent.Files)
                            {
                                AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete);
                            }
                        }
                    }
                    Console.Clear();
                    Console.WriteLine(sb.ToString());
                    listener.ExportTo(Console.Out);
                }

                Thread.Sleep(500);
            }
        }
コード例 #8
0
 public Task Start() => manager.StartAsync();