Пример #1
0
        public void ScrapeResponseMessage_TryDecode()
        {
            ScrapeResponseMessage message;

            byte[] data = "00000002 00003300 00000001 00000002 00000003 00000004 00000005 00000006".Replace(" ", string.Empty).ToByteArray();

            if (ScrapeResponseMessage.TryDecode(data, 0, out message))
            {
                Assert.AreEqual(32, message.Length);
                Assert.AreEqual(2, (int)message.Action);
                Assert.AreEqual(13056, message.TransactionId);
                Assert.AreEqual(2, message.Scrapes.Count());
                Assert.AreEqual(1, message.Scrapes.ElementAt(0).SeedersCount);
                Assert.AreEqual(2, message.Scrapes.ElementAt(0).CompleteCount);
                Assert.AreEqual(3, message.Scrapes.ElementAt(0).LeechesCount);
                Assert.AreEqual(4, message.Scrapes.ElementAt(1).SeedersCount);
                Assert.AreEqual(5, message.Scrapes.ElementAt(1).CompleteCount);
                Assert.AreEqual(6, message.Scrapes.ElementAt(1).LeechesCount);
                CollectionAssert.AreEqual(data, message.Encode());
            }
            else
            {
                Assert.Fail();
            }
        }
Пример #2
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);
        }
        protected virtual async Task ReceiveScrape(UdpClient client, ScrapeMessage scrapeMessage, IPEndPoint remotePeer)
        {
            BEncodedDictionary val = Handle(getCollection(scrapeMessage), remotePeer.Address, true);

            UdpTrackerMessage m;

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

                foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in val)
                {
                    var dict     = (BEncodedDictionary)keypair.Value;
                    int seeds    = 0;
                    int leeches  = 0;
                    int complete = 0;
                    foreach (KeyValuePair <BEncodedString, BEncodedValue> 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();
                        await client.SendAsync(data, data.Length, remotePeer);

                        scrapes.Clear();
                    }
                }
                m = new ScrapeResponseMessage(scrapeMessage.TransactionId, scrapes);
            }
            data = m.Encode();
            await client.SendAsync(data, data.Length, remotePeer);
        }
Пример #4
0
        public void ScrapeResponseTest()
        {
            List <ScrapeDetails> details = new List <ScrapeDetails>();

            details.Add(new ScrapeDetails(1, 2, 3));
            details.Add(new ScrapeDetails(4, 5, 6));
            details.Add(new ScrapeDetails(7, 8, 9));

            ScrapeResponseMessage m = new ScrapeResponseMessage(12345, details);
            ScrapeResponseMessage d = (ScrapeResponseMessage)UdpTrackerMessage.DecodeMessage(m.Encode(), 0, m.ByteLength, MessageType.Response);

            Check(m, MessageType.Response);

            Assert.AreEqual(2, m.Action);
            Assert.AreEqual(m.Action, d.Action);
            Assert.IsTrue(Toolbox.ByteMatch(m.Encode(), d.Encode()));
            Assert.AreEqual(12345, d.TransactionId);
        }
        public static UdpTrackerMessage DecodeMessage(byte[] buffer, int offset, int count, MessageType type)
        {
            UdpTrackerMessage m = null;
            var 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;
        }
Пример #6
0
        public void ScrapeResponseTest()
        {
            var details = new List <ScrapeDetails>
            {
                new ScrapeDetails(1, 2, 3),
                new ScrapeDetails(4, 5, 6),
                new ScrapeDetails(7, 8, 9)
            };

            var expectedMessage = new ScrapeResponseMessage(12345, details);
            var actualMessage   =
                (ScrapeResponseMessage)
                UdpTrackerMessage.DecodeMessage(expectedMessage.Encode(), 0, expectedMessage.ByteLength, MessageType.Response);

            Check(expectedMessage, MessageType.Response);

            Assert.AreEqual(2, expectedMessage.Action);
            Assert.AreEqual(expectedMessage.Action, actualMessage.Action);
            Assert.IsTrue(Toolbox.ByteMatch(expectedMessage.Encode(), actualMessage.Encode()));
            Assert.AreEqual(12345, actualMessage.TransactionId);
        }
Пример #7
0
        public void ScrapeResponseTest()
        {
            var details = new List<ScrapeDetails>
                              {
                                  new ScrapeDetails(1, 2, 3),
                                  new ScrapeDetails(4, 5, 6),
                                  new ScrapeDetails(7, 8, 9)
                              };

            var expectedMessage = new ScrapeResponseMessage(12345, details);
            var actualMessage =
                (ScrapeResponseMessage)
                UdpTrackerMessage.DecodeMessage(expectedMessage.Encode(), 0, expectedMessage.ByteLength, MessageType.Response);
            Check(expectedMessage, MessageType.Response);

            Assert.AreEqual(2, expectedMessage.Action);
            Assert.AreEqual(expectedMessage.Action, actualMessage.Action);
            Assert.IsTrue(Toolbox.ByteMatch(expectedMessage.Encode(), actualMessage.Encode()));
            Assert.AreEqual(12345, actualMessage.TransactionId);
        }
        protected virtual void ReceiveScrape(ScrapeMessage scrapeMessage)
        {
            BEncodedDictionary 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
            {
                List <ScrapeDetails> scrapes = new List <ScrapeDetails>();

                foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in val)
                {
                    BEncodedDictionary dict = (BEncodedDictionary)keypair.Value;
                    int seeds    = 0;
                    int leeches  = 0;
                    int complete = 0;
                    foreach (KeyValuePair <BEncodedString, BEncodedValue> 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;
                        }
                    }
                    ScrapeDetails 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();
#if NETSTANDARD1_5
                        listener.SendAsync(data, data.Length, endpoint);
#else
                        listener.Send(data, data.Length, endpoint);
#endif
                        scrapes.Clear();
                    }
                }
                m = new ScrapeResponseMessage(scrapeMessage.TransactionId, scrapes);
            }
            data = m.Encode();
#if NETSTANDARD1_5
            listener.SendAsync(data, data.Length, endpoint);
#else
            listener.Send(data, data.Length, endpoint);
#endif
        }
Пример #9
0
        protected virtual void ReceiveScrape(ScrapeMessage scrapeMessage)
        {
            BEncodedDictionary 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
            {
                List<ScrapeDetails> scrapes = new List<ScrapeDetails>();

                foreach (KeyValuePair<BEncodedString, BEncodedValue> keypair in val)
                {
                    BEncodedDictionary dict = (BEncodedDictionary)keypair.Value;
                    int seeds = 0;
                    int leeches = 0;
                    int complete = 0;
                    foreach (KeyValuePair<BEncodedString, BEncodedValue> 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;
                        }
                    }
                    ScrapeDetails 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);
        }