public byte[] ReadAuthenticationData() { return(_body.ReadWithLengthPrefix()); }
MqttBasePacket DecodeConnectPacket(IMqttPacketBodyReader body) { ThrowIfBodyIsEmpty(body); var protocolName = body.ReadStringWithLengthPrefix(); var protocolVersion = body.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 = body.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 = body.ReadTwoByteInteger(); packet.ClientId = body.ReadStringWithLengthPrefix(); if (willFlag) { packet.WillMessage = new MqttApplicationMessage { Topic = body.ReadStringWithLengthPrefix(), Payload = body.ReadWithLengthPrefix(), QualityOfServiceLevel = (MqttQualityOfServiceLevel)willQoS, Retain = willRetain }; } if (usernameFlag) { packet.Username = body.ReadStringWithLengthPrefix(); } if (passwordFlag) { packet.Password = body.ReadWithLengthPrefix(); } ValidateConnectPacket(packet); return(packet); }
private static MqttBasePacket DecodeConnectPacket(IMqttPacketBodyReader body) { ThrowIfBodyIsEmpty(body); var packet = new MqttConnectPacket(); var protocolName = body.ReadStringWithLengthPrefix(); var protocolVersion = body.ReadByte(); if (protocolName != "MQTT" && protocolVersion != 5) { throw new MqttProtocolViolationException("MQTT protocol name and version do not match MQTT v5."); } var connectFlags = body.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.WillMessage = new MqttApplicationMessage { QualityOfServiceLevel = (MqttQualityOfServiceLevel)willMessageQoS, Retain = willMessageRetainFlag }; } packet.KeepAlivePeriod = body.ReadTwoByteInteger(); var propertiesReader = new MqttV500PropertiesReader(body); while (propertiesReader.MoveNext()) { if (packet.Properties == null) { packet.Properties = new MqttConnectPacketProperties(); } if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval) { packet.Properties.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationMethod) { packet.Properties.AuthenticationMethod = propertiesReader.ReadAuthenticationMethod(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationData) { packet.Properties.AuthenticationData = propertiesReader.ReadAuthenticationData(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReceiveMaximum) { packet.Properties.ReceiveMaximum = propertiesReader.ReadReceiveMaximum(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.TopicAliasMaximum) { packet.Properties.TopicAliasMaximum = propertiesReader.ReadTopicAliasMaximum(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.MaximumPacketSize) { packet.Properties.MaximumPacketSize = propertiesReader.ReadMaximumPacketSize(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.RequestResponseInformation) { packet.Properties.RequestResponseInformation = propertiesReader.RequestResponseInformation(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.RequestProblemInformation) { packet.Properties.RequestProblemInformation = propertiesReader.RequestProblemInformation(); } else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) { if (packet.Properties.UserProperties == null) { packet.Properties.UserProperties = new List <MqttUserProperty>(); } propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); } else { propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttConnectPacket)); } } packet.ClientId = body.ReadStringWithLengthPrefix(); if (packet.WillMessage != null) { var willPropertiesReader = new MqttV500PropertiesReader(body); while (willPropertiesReader.MoveNext()) { if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.PayloadFormatIndicator) { packet.WillMessage.PayloadFormatIndicator = propertiesReader.ReadPayloadFormatIndicator(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.MessageExpiryInterval) { packet.WillMessage.MessageExpiryInterval = propertiesReader.ReadMessageExpiryInterval(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.TopicAlias) { packet.WillMessage.TopicAlias = propertiesReader.ReadTopicAlias(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.ResponseTopic) { packet.WillMessage.ResponseTopic = propertiesReader.ReadResponseTopic(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.CorrelationData) { packet.WillMessage.CorrelationData = propertiesReader.ReadCorrelationData(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.SubscriptionIdentifier) { if (packet.WillMessage.SubscriptionIdentifiers == null) { packet.WillMessage.SubscriptionIdentifiers = new List <uint>(); } packet.WillMessage.SubscriptionIdentifiers.Add(propertiesReader.ReadSubscriptionIdentifier()); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.ContentType) { packet.WillMessage.ContentType = propertiesReader.ReadContentType(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.WillDelayInterval) { // This is a special case! packet.Properties.WillDelayInterval = propertiesReader.ReadWillDelayInterval(); } else if (willPropertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) { if (packet.WillMessage.UserProperties == null) { packet.WillMessage.UserProperties = new List <MqttUserProperty>(); } propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); } else { propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttPublishPacket)); } } packet.WillMessage.Topic = body.ReadStringWithLengthPrefix(); packet.WillMessage.Payload = body.ReadWithLengthPrefix(); } if (usernameFlag) { packet.Username = body.ReadStringWithLengthPrefix(); } if (passwordFlag) { packet.Password = body.ReadWithLengthPrefix(); } return(packet); }