Пример #1
0
        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);
            }
        }
Пример #2
0
        public static object DecodeFile(string path)
        {
            byte[] bytes = File.ReadAllBytes(path);

            return(BEncoding.Decode(bytes));
        }