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);
        }
        public void AnnounceMessageTest()
        {
            AnnounceMessage m = new AnnounceMessage(0, 12345, announceparams);
            AnnounceMessage d = (AnnounceMessage)UdpTrackerMessage.DecodeMessage(m.Encode(), 0, m.ByteLength, MessageType.Request);
            Check(m, MessageType.Request);

            Assert.AreEqual(1, m.Action);
            Assert.AreEqual(m.Action, d.Action);
            Assert.IsTrue(Toolbox.ByteMatch(m.Encode(), d.Encode()));
            Assert.AreEqual(12345, d.ConnectionId);
        }
        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 NameValueCollection getCollection(AnnounceMessage announceMessage)
 {
     var res = new NameValueCollection();
     res.Add("info_hash", announceMessage.Infohash.UrlEncode());
     res.Add("peer_id", announceMessage.PeerId);
     res.Add("port", announceMessage.Port.ToString());
     res.Add("uploaded", announceMessage.Uploaded.ToString());
     res.Add("downloaded", announceMessage.Downloaded.ToString());
     res.Add("left", announceMessage.Left.ToString());
     res.Add("compact", "1"); //hardcode
     res.Add("numwant", announceMessage.NumWanted.ToString());
     res.Add("ip", announceMessage.Ip.ToString());
     res.Add("key", announceMessage.Key.ToString());
     res.Add("event", announceMessage.TorrentEvent.ToString().ToLower());
     return res;
 }
        //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);
        }
 protected override void ReceiveAnnounce(AnnounceMessage announceMessage)
 {
     if (!IgnoreAnnounces)
         base.ReceiveAnnounce(announceMessage);
 }