Exemplo n.º 1
0
        private BencodedDictionary GetMetadata()
        {
            var metadata = BencodingParser.Decode(_data) as BencodedDictionary;

            CheckMetadata(metadata);
            return(metadata);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the Torrent.Client.TrackerResponse class with the reponse specified as a string.
        /// </summary>
        /// <exception cref="Torrent.Client.TorrentException">Thrown when the string containing the tracker response is invalid.</exception>
        /// <param name="bencoded">A bencoded string containing the tracker response.</param>
        public TrackerResponse(byte[] bencoded)
        {
            Contract.Requires(bencoded != null);

            var response = (BencodedDictionary)BencodingParser.Decode(bencoded);

            if (response.ContainsKey("failure reason"))
            {
                FailureReason = (BencodedString)response["failure reason"];
            }
            else
            {
                if (response.ContainsKey("warning message"))
                {
                    WarningMessage = (BencodedString)response["warning message"];
                }
                if (response.ContainsKey("min interval"))
                {
                    MinInterval = (BencodedInteger)response["min interval"];
                }
                if (response.ContainsKey("tracker id"))
                {
                    TrackerID = (BencodedString)response["tracker id"];
                }
                if (response.ContainsKey("complete"))
                {
                    Complete = (BencodedInteger)response["complete"];
                }
                if (response.ContainsKey("incomplete"))
                {
                    Incomplete = (BencodedInteger)response["incomplete"];
                }
                if (response.ContainsKey("interval"))
                {
                    Interval = (BencodedInteger)response["interval"];
                }

                if (!response.ContainsKey("peers"))
                {
                    throw new TorrentException("Tracker response does not contain peers list.");
                }

                if (response["peers"] is BencodedList) // Peers Dictionary model
                {
                    if (!((BencodedList)response["peers"]).Any())
                    {
                        throw new TorrentException("Peers list is empty.");
                    }

                    Endpoints = new List <IPEndPoint>();

                    foreach (BencodedDictionary peer in (BencodedList)response["peers"])
                    {
                        Endpoints.Add(PeerEndpoint.FromBencoded(peer));
                    }
                }
                else if (response["peers"] is BencodedString) // Peers Binary model
                {
                    if (!((BencodedString)response["peers"]).Any())
                    {
                        throw new TorrentException("Peers list is empty.");
                    }

                    byte[] byteData = ((BencodedString)response["peers"]).Select(c => (byte)c).ToArray();
                    Endpoints = byteData.Batch(6).Select(p => PeerEndpoint.FromBytes(p.ToArray())).ToList();
                }
                else
                {
                    throw new TorrentException("Cannot read peers list.");
                }
            }
        }