Beispiel #1
0
        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);
        }