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); }
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); }
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); }