private async Task Run(FileInfo torrent) { var(dto, benc) = await BaseCommand.GetTorrentFileDtoAsync(torrent); var infoHash = benc["info"] !.Hash(); byte[] peerId = CreateRandomPeerId(); Console.WriteLine($"torrent download v2 hash: {Convert.ToHexString(infoHash)}"); var endpoints = await DownloadCommand.GetTrackerPeersAsync(dto, infoHash, peerId); Console.WriteLine($"endpoints: {endpoints.Length}"); foreach (var peerEndpoint in endpoints) { try { using var peer = await PeerConnection.EstablishConnection(peerEndpoint, TimeSpan.FromSeconds(1.5)); peer.MessageReceived += msg => Console.WriteLine($"{peerEndpoint} => {msg.GetType()}"); await peer.SendMessage( new Handshake(new byte[8], infoHash, peerId) ); await peer.SendMessage(new Unchoke()); await peer.SendMessage(new Interested()); await Task.Delay(20000); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.Error.WriteLine($"[{peerEndpoint}]: ERR\n{e.Message} {e.StackTrace}\n"); Console.ResetColor(); continue; } } }