Ejemplo n.º 1
0
        public void Parse_ValidHeader_With_PacketIdentifier_ReturnObject()
        {
            //Arrange
            const ushort expectedPacketId = 5;

            byte[] fixHeader = CreateMessage(ControlPacketTypes.PUBLISH, 10, null, (byte)Flag.Qos1, expectedPacketId);

            MessageHeader actualHeader;

            var stream = new MemoryStream(fixHeader);

            //Act
            using (BinaryReader br = new BinaryReader(stream))
            {
                actualHeader = MessageHeader.Parse(br);
            }
            //Assert
            Assert.IsNotNull(actualHeader);
            Assert.AreEqual(expectedPacketId, actualHeader.PacketIdentifier);
        }
Ejemplo n.º 2
0
        public void Parse_ValidHeader_With_ComplexVariableLength_ReturnObject()
        {
            //Arrange
            const int expectedLength = 128;

            byte[] fixHeader = CreateMessage(ControlPacketTypes.CONNECT, 128, 1);

            MessageHeader actualHeader;

            var stream = new MemoryStream(fixHeader);

            //Act
            using (BinaryReader br = new BinaryReader(stream))
            {
                actualHeader = MessageHeader.Parse(br);
            }
            //Assert
            Assert.IsNotNull(actualHeader);
            Assert.IsTrue(actualHeader.Length == expectedLength);
        }
Ejemplo n.º 3
0
        public bool Parse(IByteBuffer buffer)
        {
            try
            {
                Header = MessageHeader.Parse(buffer);
                //消息体由协议序列化后的内容构成
                if (Header.MessageBodyLen > 0)
                {
                    this.BodyContent = new byte[Header.MessageBodyLen];
                    buffer.ReadBytes(this.BodyContent);
                }
                else//保证protobuffer对象默认状态下可反序列化(protobuffer序列化对象默认值时,字节数组为0)
                {
                    this.BodyContent = new byte[0];
                }
                return(true);
            }
            catch (Exception)
            {
            }

            return(false);
        }