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);
        }
 private void CompleteScrape(UdpTrackerMessage message, object state)
 {
     var error = message as ErrorMessage;
     if (error != null)
     {
         FailureMessage = error.Error;
         DoScrapeComplete(false, state);
     }
     else
     {
         //response.Scrapes not used for moment
         //ScrapeResponseMessage response = (ScrapeResponseMessage)message;
         DoScrapeComplete(true, state);
     }
 }
        private void CompleteAnnounce(UdpTrackerMessage message, object state)
        {
            var error = message as ErrorMessage;
            if (error != null)
            {
                FailureMessage = error.Error;
                DoAnnounceComplete(false, state, new List<Peer>());
            }
            else
            {
                var response = (AnnounceResponseMessage) message;
                DoAnnounceComplete(true, state, response.Peers);

                //TODO seeders and leechers is not used in event.
            }
        }
Beispiel #4
0
 void Check(UdpTrackerMessage message, MessageType type)
 {
     byte[] e = message.Encode();
     Assert.AreEqual(e.Length, message.ByteLength, "#1");
     Assert.IsTrue(Toolbox.ByteMatch(e, UdpTrackerMessage.DecodeMessage(e, 0, e.Length, type).Encode()), "#2");
 }
 private void Check(UdpTrackerMessage message, MessageType type)
 {
     var e = message.Encode();
     Assert.Equal(e.Length, message.ByteLength);
     Assert.True(Toolbox.ByteMatch(e, UdpTrackerMessage.DecodeMessage(e, 0, e.Length, type).Encode()));
 }