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