Beispiel #1
0
        public static QueryType TryGetQuery(BDictionary message, out Query query)
        {
            if (message.TryGetValue("q", out BString type))
            {
                switch (type.Text)
                {
                case "ping":
                    if (PingQuery.TryParse(message, out PingQuery pingQuery))
                    {
                        query = pingQuery;
                        return(QueryType.ping);
                    }
                    break;

                case "find_node":
                    if (FindNodeQuery.TryParse(message, out FindNodeQuery findNodeQuery))
                    {
                        query = findNodeQuery;
                        return(QueryType.find_node);
                    }
                    break;

                case "get_peers":
                    if (GetPeersQuery.TryParse(message, out GetPeersQuery getPeersQuery))
                    {
                        query = getPeersQuery;
                        return(QueryType.get_peers);
                    }
                    break;

                case "announce_peer":
                    if (AnnouncePeerQuery.TryParse(message, out AnnouncePeerQuery announcePeerQuery))
                    {
                        query = announcePeerQuery;
                        return(QueryType.announce_peer);
                    }
                    break;

                default:
                    query = null;
                    return(QueryType.Unknown);
                }
            }

            query = null;
            return(QueryType.Invalid);
        }
        public void AnnouncePeerQuery_Encoding()
        {
            TransactionID     transactionId = new TransactionID(Encoding.UTF8.GetBytes("aa"));
            NodeID            senderId      = new NodeID(Encoding.UTF8.GetBytes("abcdefghij0123456789"));
            InfoHash          infoHash      = new InfoHash(Encoding.UTF8.GetBytes("mnopqrstuvwxyz123456"));
            PeerToken         token         = new PeerToken(Encoding.UTF8.GetBytes("aoeusnth"));
            int               port          = 6881;
            bool              impliedPort   = true;
            AnnouncePeerQuery testQuery     = new AnnouncePeerQuery(transactionId, senderId, infoHash, token, port, impliedPort);

            byte[] bencodedMessageBytes = testQuery.Construct().Encode();

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.announce_peer, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is AnnouncePeerQuery);
            Assert.Equal(testQuery.TransactionID, query.TransactionID);
            Assert.Equal(testQuery.SenderNode, ((AnnouncePeerQuery)query).SenderNode);
            Assert.Equal(testQuery.InfoHash, ((AnnouncePeerQuery)query).InfoHash);
            Assert.Equal(testQuery.Token, ((AnnouncePeerQuery)query).Token);
            Assert.Equal(testQuery.Port, ((AnnouncePeerQuery)query).Port);
            Assert.Equal(testQuery.ImpliedPort, ((AnnouncePeerQuery)query).ImpliedPort);

            Assert.Equal(TestBencodedMessage, Encoding.UTF8.GetString(bencodedMessageBytes));
        }