MqttPacket DecodePublishPacket(byte header, ArraySegment <byte> body) { ThrowIfBodyIsEmpty(body); _bufferReader.SetBuffer(body.Array, body.Offset, body.Count); var retain = (header & 1) > 0; var qos = (MqttQualityOfServiceLevel)((header >> 1) & 3); var dup = ((header >> 3) & 1) > 0; var packet = new MqttPublishPacket { Topic = _bufferReader.ReadString(), Retain = retain, QualityOfServiceLevel = qos, Dup = dup }; if (qos > 0) { packet.PacketIdentifier = _bufferReader.ReadTwoByteInteger(); } var propertiesReader = new MqttV5PropertiesReader(_bufferReader); while (propertiesReader.MoveNext()) { if (propertiesReader.CurrentPropertyId == MqttPropertyId.PayloadFormatIndicator) { packet.PayloadFormatIndicator = propertiesReader.ReadPayloadFormatIndicator(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.MessageExpiryInterval) { packet.MessageExpiryInterval = propertiesReader.ReadMessageExpiryInterval(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.TopicAlias) { packet.TopicAlias = propertiesReader.ReadTopicAlias(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ResponseTopic) { packet.ResponseTopic = propertiesReader.ReadResponseTopic(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.CorrelationData) { packet.CorrelationData = propertiesReader.ReadCorrelationData(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.SubscriptionIdentifier) { if (packet.SubscriptionIdentifiers == null) { packet.SubscriptionIdentifiers = new List <uint>(); } packet.SubscriptionIdentifiers.Add(propertiesReader.ReadSubscriptionIdentifier()); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ContentType) { packet.ContentType = propertiesReader.ReadContentType(); } else { propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttPublishPacket)); } } packet.UserProperties = propertiesReader.CollectedUserProperties; if (!_bufferReader.EndOfStream) { packet.Payload = _bufferReader.ReadRemainingData(); } return(packet); }
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); }