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);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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
        }
Пример #6
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);
        }