Exemplo n.º 1
0
        public static UdpTrackerMessage DecodeMessage(byte[] buffer, int offset, int count, MessageType type)
        {
            UdpTrackerMessage m = null;
            int action = type == MessageType.Request ? ReadInt(buffer, offset + 8) : ReadInt(buffer, offset);
            switch (action)
            {
                case 0:
                    if (type == MessageType.Request)
                        m = new ConnectMessage();
                    else
                        m = new ConnectResponseMessage();
                    break;
                case 1:
                    if (type == MessageType.Request)
                        m = new AnnounceMessage();
                    else
                        m = new AnnounceResponseMessage();
                    break;
                case 2:
                    if (type == MessageType.Request)
                        m = new ScrapeMessage();
                    else
                        m = new ScrapeResponseMessage();
                    break;
                case 3:
                    m = new ErrorMessage();
                    break;
                default:
                    throw new ProtocolException(string.Format("Invalid udp message received: {0}", buffer[offset]));
            }

            try
            {
                m.Decode(buffer, offset, count);
            }
            catch
            {
                m = new ErrorMessage(0, "Couldn't decode the tracker response");
            }
            return m;
        }
Exemplo n.º 2
0
 protected virtual void ReceiveError(ErrorMessage errorMessage)
 {
     throw new ProtocolException(string.Format("ErrorMessage from :{0}", endpoint.Address));
 }
Exemplo n.º 3
0
        protected virtual void ReceiveScrape(ScrapeMessage scrapeMessage)
        {
            var val = Handle(getCollection(scrapeMessage), endpoint.Address, true);

            UdpTrackerMessage m;
            byte[] data;
            if (val.ContainsKey(RequestParameters.FailureKey))
            {
                m = new ErrorMessage(scrapeMessage.TransactionId, val[RequestParameters.FailureKey].ToString());
            }
            else
            {
                var scrapes = new List<ScrapeDetails>();

                foreach (var keypair in val)
                {
                    var dict = (BEncodedDictionary) keypair.Value;
                    var seeds = 0;
                    var leeches = 0;
                    var complete = 0;
                    foreach (var keypair2 in dict)
                    {
                        switch (keypair2.Key.Text)
                        {
                            case "complete": //The current number of connected seeds
                                seeds = Convert.ToInt32(keypair2.Value.ToString());
                                break;
                            case "downloaded": //The total number of completed downloads
                                complete = Convert.ToInt32(keypair2.Value.ToString());
                                break;
                            case "incomplete":
                                leeches = Convert.ToInt32(keypair2.Value.ToString());
                                break;
                        }
                    }
                    var sd = new ScrapeDetails(seeds, leeches, complete);
                    scrapes.Add(sd);
                    if (scrapes.Count == 74) //protocole do not support to send more than 74 scrape at once...
                    {
                        m = new ScrapeResponseMessage(scrapeMessage.TransactionId, scrapes);
                        data = m.Encode();
                        listener.Send(data, data.Length, endpoint);
                        scrapes.Clear();
                    }
                }
                m = new ScrapeResponseMessage(scrapeMessage.TransactionId, scrapes);
            }
            data = m.Encode();
            listener.Send(data, data.Length, endpoint);
        }
Exemplo n.º 4
0
        //QUICKHACK: format bencoded val and get it back wereas must refactor tracker system to have more generic object...
        protected virtual void ReceiveAnnounce(AnnounceMessage announceMessage)
        {
            UdpTrackerMessage m;
            var dict = Handle(getCollection(announceMessage), endpoint.Address, false);
            if (dict.ContainsKey(RequestParameters.FailureKey))
            {
                m = new ErrorMessage(announceMessage.TransactionId, dict[RequestParameters.FailureKey].ToString());
            }
            else
            {
                var interval = TimeSpan.Zero;
                var leechers = 0;
                var seeders = 0;
                var peers = new List<Client.Peer>();
                foreach (var keypair in dict)
                {
                    switch (keypair.Key.Text)
                    {
                        case "complete":
                            seeders = Convert.ToInt32(keypair.Value.ToString()); //same as seeder?
                            break;

                        case "incomplete":
                            leechers = Convert.ToInt32(keypair.Value.ToString()); //same as leecher?
                            break;

                        case "interval":
                            interval = TimeSpan.FromSeconds(int.Parse(keypair.Value.ToString()));
                            break;

                        case "peers":
                            if (keypair.Value is BEncodedList) // Non-compact response
                                peers.AddRange(Client.Peer.Decode((BEncodedList) keypair.Value));
                            else if (keypair.Value is BEncodedString) // Compact response
                                peers.AddRange(Client.Peer.Decode((BEncodedString) keypair.Value));
                            break;

                        default:
                            break;
                    }
                }
                m = new AnnounceResponseMessage(announceMessage.TransactionId, interval, leechers, seeders, peers);
            }
            var data = m.Encode();
            listener.Send(data, data.Length, endpoint);
        }
Exemplo n.º 5
0
 protected override void ReceiveError(ErrorMessage errorMessage)
 {
     if (!IgnoreErrors)
         base.ReceiveError(errorMessage);
 }