public static DHTMessage CreateGetPeersResponse(
            BString transactionID, DHTId nodeId, DHTId infoHash,
            IList <IDHTPeer> peersList, IList <DHTNode> nodesList)
        {
            BList values = DHTNode.CompactPeers(peersList);
            var   nodes  = new BString(DHTNode.CompactNodes(nodesList));

            BDictionary sendData = new BDictionary();

            sendData.Add("t", transactionID);
            sendData.Add("y", "r");

            var r = new BDictionary();

            r.Add("id", nodeId.ToBencodedString());
            r.Add("token", new BString(infoHash.Data.SubArray(0, 2)));
            if (values != null)
            {
                r.Add("values", values);
            }
            r.Add("nodes", nodes);
            sendData.Add("r", r);

            return(new DHTMessage(MessageType.Response, QueryType.None, sendData));
        }
        public static DHTMessage CreatePingResponse(BString transactionID, DHTId nodeId)
        {
            BDictionary sendData = new BDictionary();

            sendData.Add("y", "r");
            sendData.Add("t", transactionID);

            var r = new BDictionary();

            r.Add("id", nodeId.ToBencodedString());
            sendData.Add("r", r);

            return(new DHTMessage(MessageType.Response, QueryType.None, sendData));
        }
        public static DHTMessage CreatePingQuery(BString transactionID, DHTId nodeId)
        {
            BDictionary sendData = new BDictionary();

            sendData.Add("t", transactionID);
            sendData.Add("y", "q");
            sendData.Add("q", "ping");

            var args = new BDictionary();

            args.Add("id", nodeId.ToBencodedString());
            sendData.Add("a", args);

            return(new DHTMessage(MessageType.Query, QueryType.Ping, sendData));
        }
        public static DHTMessage CreateAnnouncePeerResponse(BString transactionID, DHTId nodeId, IList <DHTNode> nodesList)
        {
            var nodes = new BString(DHTNode.CompactNodes(nodesList));

            BDictionary sendData = new BDictionary();

            sendData.Add("y", "r");
            sendData.Add("t", transactionID);

            var r = new BDictionary();

            r.Add("id", nodeId.ToBencodedString());
            r.Add("nodes", nodes);
            sendData.Add("r", r);

            return(new DHTMessage(MessageType.Response, QueryType.None, sendData));
        }
        public static DHTMessage CreateAnnouncePeerQuery(BString transactionID, DHTId nodeId, DHTId infoHash,
                                                         byte implied_port, int port, BString token)
        {
            BDictionary sendData = new BDictionary();

            sendData.Add("t", transactionID);
            sendData.Add("y", "q");
            sendData.Add("q", "announce_peer");

            var args = new BDictionary();

            args.Add("id", nodeId.ToBencodedString());
            args.Add("implied_port", new BNumber(implied_port));
            args.Add("info_hash", infoHash.ToBencodedString());
            args.Add("port", new BNumber(port));
            args.Add("token", token);
            sendData.Add("a", args);

            return(new DHTMessage(MessageType.Query, QueryType.AnnouncePeer, sendData));
        }
        public static DHTMessage CreateGetPeersQuery(BString transactionID, DHTId nodeId, DHTId infoHash)
        {
            BDictionary sendData = new BDictionary();

            sendData.Add("t", transactionID);
            sendData.Add("y", "q");
            sendData.Add("q", "get_peers");

            #if IP6
            var want = new BList();
            want.Add("n6");
            sendData.Add("want", want);
            #endif

            var args = new BDictionary();
            args.Add("id", nodeId.ToBencodedString());
            args.Add("info_hash", infoHash.ToBencodedString());
            sendData.Add("a", args);

            return(new DHTMessage(MessageType.Query, QueryType.GetPeers, sendData));
        }
        public static DHTMessage CreateFindNodeQuery(BString transactionID, DHTId nodeId, DHTId targetNodeId)
        {
            BDictionary sendData = new BDictionary();

            sendData.Add("t", transactionID);
            sendData.Add("y", "q");
            sendData.Add("q", "find_node");

            #if IP6
            var want = new BList();
            want.Add("n6");
            sendData.Add("want", want);
            #endif

            var args = new BDictionary();
            args.Add("id", nodeId.ToBencodedString());
            args.Add("target", targetNodeId.ToBencodedString());
            sendData.Add("a", args);

            return(new DHTMessage(MessageType.Query, QueryType.FindNode, sendData));
        }