Esempio n. 1
0
        public void Test_GetMessageType()
        {
            Assert.AreEqual(MessageType.Response, DHTMessage.GetMessageType("r"));
            Assert.AreEqual(MessageType.Query, DHTMessage.GetMessageType("q"));
            Assert.AreEqual(MessageType.Error, DHTMessage.GetMessageType("e"));

            Assert.AreEqual(MessageType.Unknown, DHTMessage.GetMessageType("xyz"));
            Assert.AreEqual(MessageType.Unknown, DHTMessage.GetMessageType(null));
        }
Esempio n. 2
0
        public static DHTMessage ParseBuffer(byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0 || buffer[0] != 'd')
            {
                return(null);
            }

            var data = fParser.Parse <BDictionary>(buffer);

            var bsY = data.Get <BString>("y");

            if (bsY == null)
            {
                return(null);
            }

            string      strMsgType = bsY.ToString();
            MessageType msgType    = DHTMessage.GetMessageType(strMsgType);

            DHTMessage result;

            switch (msgType)
            {
            case MessageType.Response:
                result = new DHTResponseMessage(msgType, QueryType.None, data);
                break;

            case MessageType.Query:
                result = new DHTQueryMessage(msgType, QueryType.None, data);
                break;

            case MessageType.Error:
                result = new DHTErrorMessage(msgType, QueryType.None, data);
                break;

            default:
                result = new DHTMessage(msgType, QueryType.None, data);
                break;
            }

            result.Parse();
            return(result);
        }