private void EncodeString_ReturnTrue(string testcase) { var testbytes = GetBytes($"{testcase.Length}:{testcase}"); var bytes = BEncoding.Encode(testcase); Debug.Assert(bytes.SequenceEqual(testbytes)); }
public static Torrent LoadFromFile(string filePath, string downloadPath) { object obj = BEncoding.DecodeFile(filePath); string name = Path.GetFileNameWithoutExtension(filePath); return(BEncodingObjectToTorrent(obj, name, downloadPath)); }
private void EncodeInt_ReturnTrue(long testcase) { var resultBytes = Encoding.UTF8.GetBytes($"i{testcase}e"); var bytes = BEncoding.Encode(testcase); Debug.Assert(bytes.SequenceEqual(resultBytes)); }
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; if (handler != null) { handler(this, peers); } }
public static object DecodeFile(string path) { byte[] bytes = File.ReadAllBytes(path); return(BEncoding.Decode(bytes)); }
public static void SaveToFile(Torrent torrent) { object obj = TorrentToBEncodingObject(torrent); BEncoding.EncodeToFile(obj, torrent.Name + ".torrent"); }
public 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); } }
private void EncodeDictionary_ReturnTrue(IDictionary <string, object> input, string expected) { var encoded = GetString(BEncoding.Encode(input)); Debug.Assert(encoded == expected); }
private void EncodeList_Return_True(IList <object> list, string expected) { var returnResult = GetString(BEncoding.Encode(list)); Debug.Assert(returnResult == expected); }