public void Write_And_Read_Multiple_Times() { var writer = new MqttBufferWriter(4096, 65535); writer.WriteString("A relative short string."); writer.WriteBinaryData(new byte[1234]); writer.WriteByte(0x01); writer.WriteByte(0x02); writer.WriteVariableByteInteger(5647382); writer.WriteString("A relative short string."); writer.WriteVariableByteInteger(8574489); writer.WriteBinaryData(new byte[48]); writer.WriteByte(2); writer.WriteByte(0x02); writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug"); writer.WriteBinaryData(new byte[3]); var readPayload = new ArraySegment <byte>(writer.GetBuffer(), 0, writer.Length).ToArray(); var reader = new MqttBufferReader(); reader.SetBuffer(readPayload, 0, readPayload.Length); for (var i = 0; i < 100000; i++) { reader.Seek(0); reader.ReadString(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadVariableByteInteger(); reader.ReadString(); reader.ReadVariableByteInteger(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadString(); reader.ReadBinaryData(); } }
public void Read_100_000_Messages() { var reader = new MqttBufferReader(); reader.SetBuffer(_readPayload, 0, _readPayload.Length); for (var i = 0; i < 100000; i++) { reader.Seek(0); reader.ReadString(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadVariableByteInteger(); reader.ReadString(); reader.ReadVariableByteInteger(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadString(); reader.ReadBinaryData(); } }
MqttPacket DecodeConnectPacket(ArraySegment <byte> body) { ThrowIfBodyIsEmpty(body); _bufferReader.SetBuffer(body.Array, body.Offset, body.Count); var packet = new MqttConnectPacket { // If the Request Problem Information is absent, the value of 1 is used. RequestProblemInformation = true }; var protocolName = _bufferReader.ReadString(); var protocolVersion = _bufferReader.ReadByte(); if (protocolName != "MQTT" && protocolVersion != 5) { throw new MqttProtocolViolationException("MQTT protocol name and version do not match MQTT v5."); } var connectFlags = _bufferReader.ReadByte(); var cleanSessionFlag = (connectFlags & 0x02) > 0; var willMessageFlag = (connectFlags & 0x04) > 0; var willMessageQoS = (byte)((connectFlags >> 3) & 3); var willMessageRetainFlag = (connectFlags & 0x20) > 0; var passwordFlag = (connectFlags & 0x40) > 0; var usernameFlag = (connectFlags & 0x80) > 0; packet.CleanSession = cleanSessionFlag; if (willMessageFlag) { packet.WillFlag = true; packet.WillQoS = (MqttQualityOfServiceLevel)willMessageQoS; packet.WillRetain = willMessageRetainFlag; } packet.KeepAlivePeriod = _bufferReader.ReadTwoByteInteger(); var propertiesReader = new MqttV5PropertiesReader(_bufferReader); while (propertiesReader.MoveNext()) { if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval) { packet.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationMethod) { packet.AuthenticationMethod = propertiesReader.ReadAuthenticationMethod(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationData) { packet.AuthenticationData = propertiesReader.ReadAuthenticationData(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReceiveMaximum) { packet.ReceiveMaximum = propertiesReader.ReadReceiveMaximum(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.TopicAliasMaximum) { packet.TopicAliasMaximum = propertiesReader.ReadTopicAliasMaximum(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.MaximumPacketSize) { packet.MaximumPacketSize = propertiesReader.ReadMaximumPacketSize(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.RequestResponseInformation) { packet.RequestResponseInformation = propertiesReader.RequestResponseInformation(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.RequestProblemInformation) { packet.RequestProblemInformation = propertiesReader.RequestProblemInformation(); } else { propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttConnectPacket)); } } packet.UserProperties = propertiesReader.CollectedUserProperties; packet.ClientId = _bufferReader.ReadString(); if (packet.WillFlag) { var willPropertiesReader = new MqttV5PropertiesReader(_bufferReader); while (willPropertiesReader.MoveNext()) { if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.PayloadFormatIndicator) { packet.WillPayloadFormatIndicator = willPropertiesReader.ReadPayloadFormatIndicator(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.MessageExpiryInterval) { packet.WillMessageExpiryInterval = willPropertiesReader.ReadMessageExpiryInterval(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.ResponseTopic) { packet.WillResponseTopic = willPropertiesReader.ReadResponseTopic(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.CorrelationData) { packet.WillCorrelationData = willPropertiesReader.ReadCorrelationData(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.ContentType) { packet.WillContentType = willPropertiesReader.ReadContentType(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.WillDelayInterval) { packet.WillDelayInterval = willPropertiesReader.ReadWillDelayInterval(); } else { willPropertiesReader.ThrowInvalidPropertyIdException(typeof(MqttPublishPacket)); } } packet.WillTopic = _bufferReader.ReadString(); packet.WillMessage = _bufferReader.ReadBinaryData(); packet.WillUserProperties = willPropertiesReader.CollectedUserProperties; } if (usernameFlag) { packet.Username = _bufferReader.ReadString(); } if (passwordFlag) { packet.Password = _bufferReader.ReadBinaryData(); } return(packet); }
MqttPacket DecodeConnectPacket(ArraySegment <byte> body) { ThrowIfBodyIsEmpty(body); _bufferReader.SetBuffer(body.Array, body.Offset, body.Count); var protocolName = _bufferReader.ReadString(); var protocolVersion = _bufferReader.ReadByte(); if (protocolName != "MQTT" && protocolName != "MQIsdp") { throw new MqttProtocolViolationException("MQTT protocol name do not match MQTT v3."); } if (protocolVersion != 3 && protocolVersion != 4) { throw new MqttProtocolViolationException("MQTT protocol version do not match MQTT v3."); } var packet = new MqttConnectPacket(); var connectFlags = _bufferReader.ReadByte(); if ((connectFlags & 0x1) > 0) { throw new MqttProtocolViolationException("The first bit of the Connect Flags must be set to 0."); } packet.CleanSession = (connectFlags & 0x2) > 0; var willFlag = (connectFlags & 0x4) > 0; var willQoS = (connectFlags & 0x18) >> 3; var willRetain = (connectFlags & 0x20) > 0; var passwordFlag = (connectFlags & 0x40) > 0; var usernameFlag = (connectFlags & 0x80) > 0; packet.KeepAlivePeriod = _bufferReader.ReadTwoByteInteger(); packet.ClientId = _bufferReader.ReadString(); if (willFlag) { packet.WillFlag = true; packet.WillQoS = (MqttQualityOfServiceLevel)willQoS; packet.WillRetain = willRetain; packet.WillTopic = _bufferReader.ReadString(); packet.WillMessage = _bufferReader.ReadBinaryData(); } if (usernameFlag) { packet.Username = _bufferReader.ReadString(); } if (passwordFlag) { packet.Password = _bufferReader.ReadBinaryData(); } ValidateConnectPacket(packet); return(packet); }
public byte[] ReadAuthenticationData() { return(_body.ReadBinaryData()); }