MqttPacket DecodePublishPacket(ReceivedMqttPacket receivedMqttPacket) { ThrowIfBodyIsEmpty(receivedMqttPacket.Body); _bufferReader.SetBuffer(receivedMqttPacket.Body.Array, receivedMqttPacket.Body.Offset, receivedMqttPacket.Body.Count); var retain = (receivedMqttPacket.FixedHeader & 0x1) > 0; var qualityOfServiceLevel = (MqttQualityOfServiceLevel)((receivedMqttPacket.FixedHeader >> 1) & 0x3); var dup = (receivedMqttPacket.FixedHeader & 0x8) > 0; var topic = _bufferReader.ReadString(); ushort packetIdentifier = 0; if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { packetIdentifier = _bufferReader.ReadTwoByteInteger(); } var packet = new MqttPublishPacket { PacketIdentifier = packetIdentifier, Retain = retain, Topic = topic, QualityOfServiceLevel = qualityOfServiceLevel, Dup = dup }; if (!_bufferReader.EndOfStream) { packet.Payload = _bufferReader.ReadRemainingData(); } return(packet); }
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); }