public static Torrent LoadFromFile(string filePath, string downloadPath) { object obj = BenCoding.DecodeFile(filePath); string name = Path.GetFileNameWithoutExtension(filePath); return(BEncodingObjectToTorrent(obj, name, downloadPath)); }
public static object DecodeFile(string path) { if (!File.Exists(path)) { throw new FileNotFoundException("unable to find file: " + path); } byte[] bytes = File.ReadAllBytes(path); return(BenCoding.Decode(bytes)); }
private void HandleResponse(IAsyncResult result) { byte[] data; using (HttpWebResponse response = (HttpWebResponse)_httpWebRequest.EndGetResponse(result)) { if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine("error reaching tracker " + this + ": " + response.StatusCode + " " + response.StatusDescription); return; } using (Stream stream = response.GetResponseStream()) { data = new byte[response.ContentLength]; stream?.Read(data, 0, Convert.ToInt32(response.ContentLength)); } } Dictionary <string, object> info = BenCoding.Decode(data) as Dictionary <string, object>; if (info == null) { Console.WriteLine("unable to decode tracker announce response"); return; } PeerRequestInterval = TimeSpan.FromSeconds((long)info["interval"]); byte[] peerInfo = (byte[])info["peers"]; List <IPEndPoint> peers = new List <IPEndPoint>(); for (int i = 0; i < peerInfo.Length / 6; i++) { int offset = i * 6; string address = peerInfo[offset] + "." + peerInfo[offset + 1] + "." + peerInfo[offset + 2] + "." + peerInfo[offset + 3]; int port = EndianBitConverter.Big.ToChar(peerInfo, offset + 4); peers.Add(new IPEndPoint(IPAddress.Parse(address), port)); } var handler = PeerListUpdated; handler?.Invoke(this, peers); }
public static void SaveToFile(Torrent torrent) { object obj = TorrentToBEncodingObject(torrent); BenCoding.EncodeToFile(obj, torrent.Name + ".torrent"); }
private Torrent(string name, string location, List <FileItem> files, List <string> trackers, int pieceSize, byte[] pieceHashes = null, int blockSize = 16384, bool?isPrivate = false) { Name = name; DownloadDirectory = location; Files = files; fileWriteLocks = new object[Files.Count]; for (int i = 0; i < this.Files.Count; i++) { fileWriteLocks[i] = new object(); } if (trackers != null) { foreach (string url in trackers) { Tracker tracker = new Tracker(url); Trackers.Add(tracker); tracker.PeerListUpdated += HandlePeerListUpdated; } } PieceSize = pieceSize; BlockSize = blockSize; IsPrivate = isPrivate; int count = Convert.ToInt32(Math.Ceiling(TotalSize / Convert.ToDouble(PieceSize))); PieceHashes = new byte[count][]; IsPieceVerified = new bool[count]; IsBlockAcquired = new bool[count][]; for (int i = 0; i < PieceCount; i++) { IsBlockAcquired[i] = new bool[GetBlockCount(i)]; } if (pieceHashes == null) { // this is a new torrent so we have to create the hashes from the files for (int i = 0; i < PieceCount; i++) { PieceHashes[i] = GetHash(i); } } else { for (int i = 0; i < PieceCount; i++) { PieceHashes[i] = new byte[20]; Buffer.BlockCopy(pieceHashes, i * 20, PieceHashes[i], 0, 20); } } object info = TorrentInfoToBEncodingObject(this); byte[] bytes = BenCoding.Encode(info); Infohash = SHA1.Create().ComputeHash(bytes); for (int i = 0; i < PieceCount; i++) { Verify(i); } }