static byte EncodeSubscribePacket(MqttSubscribePacket packet, IMqttPacketWriter packetWriter) { if (!packet.TopicFilters.Any()) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3]."); } if (!packet.PacketIdentifier.HasValue) { throw new MqttProtocolViolationException("Subscribe packet has no packet identifier."); } packetWriter.Write(packet.PacketIdentifier.Value); if (packet.TopicFilters?.Count > 0) { foreach (var topicFilter in packet.TopicFilters) { packetWriter.WriteWithLengthPrefix(topicFilter.Topic); packetWriter.Write((byte)topicFilter.QualityOfServiceLevel); } } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02)); }
private static byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet, IMqttPacketWriter packetWriter) { if (packet.TopicFilters?.Any() != true) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2]."); } ThrowIfPacketIdentifierIsInvalid(packet); packetWriter.Write(packet.PacketIdentifier.Value); var propertiesWriter = new MqttV500PropertiesWriter(); if (packet.Properties != null) { propertiesWriter.WriteUserProperties(packet.Properties.UserProperties); } propertiesWriter.WriteTo(packetWriter); foreach (var topicFilter in packet.TopicFilters) { packetWriter.WriteWithLengthPrefix(topicFilter); } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02)); }
static byte EncodeSubscribePacket(MqttSubscribePacket packet, IMqttPacketWriter packetWriter) { if (packet.TopicFilters?.Any() != true) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3]."); } ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet); packetWriter.Write(packet.PacketIdentifier); var propertiesWriter = new MqttV500PropertiesWriter(); if (packet.Properties != null) { if (packet.Properties.SubscriptionIdentifier > 0) { propertiesWriter.WriteSubscriptionIdentifier(packet.Properties.SubscriptionIdentifier); } propertiesWriter.WriteUserProperties(packet.Properties.UserProperties); } propertiesWriter.WriteTo(packetWriter); if (packet.TopicFilters?.Count > 0) { foreach (var topicFilter in packet.TopicFilters) { packetWriter.WriteWithLengthPrefix(topicFilter.Topic); var options = (byte)topicFilter.QualityOfServiceLevel; if (topicFilter.NoLocal) { options |= 1 << 2; } if (topicFilter.RetainAsPublished) { options |= 1 << 3; } if (topicFilter.RetainHandling != MqttRetainHandling.SendAtSubscribe) { options |= (byte)((byte)topicFilter.RetainHandling << 4); } packetWriter.Write(options); } } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02)); }
static byte EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter) { ValidatePublishPacket(packet); packetWriter.WriteWithLengthPrefix(packet.Topic); if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { if (!packet.PacketIdentifier.HasValue) { throw new MqttProtocolViolationException("Publish packet has no packet identifier."); } packetWriter.Write(packet.PacketIdentifier.Value); } else { if (packet.PacketIdentifier > 0) { throw new MqttProtocolViolationException("Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5]."); } } if (packet.Payload?.Length > 0) { packetWriter.Write(packet.Payload, 0, packet.Payload.Length); } byte fixedHeader = 0; if (packet.Retain) { fixedHeader |= 0x01; } fixedHeader |= (byte)((byte)packet.QualityOfServiceLevel << 1); if (packet.Dup) { fixedHeader |= 0x08; } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader)); }
static byte EncodeSubscribePacket(MqttSubscribePacket packet, IMqttPacketWriter packetWriter) { if (!packet.TopicFilters.Any()) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3]."); } if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("Subscribe packet has no packet identifier."); } packetWriter.Write(packet.PacketIdentifier); if (packet.TopicFilters?.Count > 0) { foreach (var topicFilter in packet.TopicFilters) { if (topicFilter.NoLocal) { throw new MqttProtocolViolationException("NoLocal is not supported in 3.1.1."); } if (topicFilter.RetainAsPublished) { throw new MqttProtocolViolationException("RetainAsPublished is not supported in 3.1.1."); } if (topicFilter.RetainHandling != MqttRetainHandling.SendAtSubscribe) { throw new MqttProtocolViolationException("RetainHandling is not supported in 3.1.1."); } packetWriter.WriteWithLengthPrefix(topicFilter.Topic); packetWriter.Write((byte)topicFilter.QualityOfServiceLevel); } } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02)); }
static byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet, IMqttPacketWriter packetWriter) { if (!packet.TopicFilters.Any()) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2]."); } if (!packet.PacketIdentifier.HasValue) { throw new MqttProtocolViolationException("Unsubscribe packet has no packet identifier."); } packetWriter.Write(packet.PacketIdentifier.Value); if (packet.TopicFilters?.Any() == true) { foreach (var topicFilter in packet.TopicFilters) { packetWriter.WriteWithLengthPrefix(topicFilter); } } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02)); }
protected virtual byte EncodeConnectPacket(MqttConnectPacket packet, IMqttPacketWriter packetWriter) { ValidateConnectPacket(packet); packetWriter.WriteWithLengthPrefix("MQIsdp"); packetWriter.Write(3); // Protocol Level 3 byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillMessage != null) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillMessage.QualityOfServiceLevel << 3); if (packet.WillMessage.Retain) { connectFlags |= 0x20; } } if (packet.Password != null && packet.Username == null) { throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22]."); } if (packet.Password != null) { connectFlags |= 0x40; } if (packet.Username != null) { connectFlags |= 0x80; } packetWriter.Write(connectFlags); packetWriter.Write(packet.KeepAlivePeriod); packetWriter.WriteWithLengthPrefix(packet.ClientId); if (packet.WillMessage != null) { packetWriter.WriteWithLengthPrefix(packet.WillMessage.Topic); packetWriter.WriteWithLengthPrefix(packet.WillMessage.Payload); } if (packet.Username != null) { packetWriter.WriteWithLengthPrefix(packet.Username); } if (packet.Password != null) { packetWriter.WriteWithLengthPrefix(packet.Password); } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }
private static byte EncodeConnectPacket(MqttConnectPacket packet, IMqttPacketWriter packetWriter) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packetWriter == null) { throw new ArgumentNullException(nameof(packetWriter)); } if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession) { throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7]."); } packetWriter.WriteWithLengthPrefix("MQTT"); packetWriter.Write(5); // [3.1.2.2 Protocol Version] byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillMessage != null) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillMessage.QualityOfServiceLevel << 3); if (packet.WillMessage.Retain) { connectFlags |= 0x20; } } if (packet.Password != null && packet.Username == null) { throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22]."); } if (packet.Password != null) { connectFlags |= 0x40; } if (packet.Username != null) { connectFlags |= 0x80; } packetWriter.Write(connectFlags); packetWriter.Write(packet.KeepAlivePeriod); var propertiesWriter = new MqttV500PropertiesWriter(); if (packet.Properties != null) { propertiesWriter.WriteSessionExpiryInterval(packet.Properties.SessionExpiryInterval); propertiesWriter.WriteAuthenticationMethod(packet.Properties.AuthenticationMethod); propertiesWriter.WriteAuthenticationData(packet.Properties.AuthenticationData); propertiesWriter.WriteRequestProblemInformation(packet.Properties.RequestProblemInformation); propertiesWriter.WriteRequestResponseInformation(packet.Properties.RequestResponseInformation); propertiesWriter.WriteReceiveMaximum(packet.Properties.ReceiveMaximum); propertiesWriter.WriteTopicAliasMaximum(packet.Properties.TopicAliasMaximum); propertiesWriter.WriteMaximumPacketSize(packet.Properties.MaximumPacketSize); propertiesWriter.WriteUserProperties(packet.Properties.UserProperties); } propertiesWriter.WriteTo(packetWriter); packetWriter.WriteWithLengthPrefix(packet.ClientId); if (packet.WillMessage != null) { var willPropertiesWriter = new MqttV500PropertiesWriter(); willPropertiesWriter.WritePayloadFormatIndicator(packet.WillMessage.PayloadFormatIndicator); willPropertiesWriter.WriteMessageExpiryInterval(packet.WillMessage.MessageExpiryInterval); willPropertiesWriter.WriteTopicAlias(packet.WillMessage.TopicAlias); willPropertiesWriter.WriteResponseTopic(packet.WillMessage.ResponseTopic); willPropertiesWriter.WriteCorrelationData(packet.WillMessage.CorrelationData); willPropertiesWriter.WriteSubscriptionIdentifiers(packet.WillMessage.SubscriptionIdentifiers); willPropertiesWriter.WriteContentType(packet.WillMessage.ContentType); willPropertiesWriter.WriteUserProperties(packet.WillMessage.UserProperties); // This is a special case! willPropertiesWriter.WriteWillDelayInterval(packet.Properties?.WillDelayInterval); willPropertiesWriter.WriteTo(packetWriter); packetWriter.WriteWithLengthPrefix(packet.WillMessage.Topic); packetWriter.WriteWithLengthPrefix(packet.WillMessage.Payload); } if (packet.Username != null) { packetWriter.WriteWithLengthPrefix(packet.Username); } if (packet.Password != null) { packetWriter.WriteWithLengthPrefix(packet.Password); } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }
private static byte EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packetWriter == null) { throw new ArgumentNullException(nameof(packetWriter)); } if (packet.QualityOfServiceLevel == 0 && packet.Dup) { throw new MqttProtocolViolationException("Dup flag must be false for QoS 0 packets [MQTT-3.3.1-2]."); } packetWriter.WriteWithLengthPrefix(packet.Topic); if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { if (!packet.PacketIdentifier.HasValue) { throw new MqttProtocolViolationException("Publish packet has no packet identifier."); } packetWriter.Write(packet.PacketIdentifier.Value); } else { if (packet.PacketIdentifier > 0) { throw new MqttProtocolViolationException("Packet identifier must be 0 if QoS == 0 [MQTT-2.3.1-5]."); } } var propertiesWriter = new MqttV500PropertiesWriter(); if (packet.Properties != null) { propertiesWriter.WritePayloadFormatIndicator(packet.Properties.PayloadFormatIndicator); propertiesWriter.WriteMessageExpiryInterval(packet.Properties.MessageExpiryInterval); propertiesWriter.WriteTopicAlias(packet.Properties.TopicAlias); propertiesWriter.WriteResponseTopic(packet.Properties.ResponseTopic); propertiesWriter.WriteCorrelationData(packet.Properties.CorrelationData); propertiesWriter.WriteSubscriptionIdentifiers(packet.Properties.SubscriptionIdentifiers); propertiesWriter.WriteContentType(packet.Properties.ContentType); propertiesWriter.WriteUserProperties(packet.Properties.UserProperties); } propertiesWriter.WriteTo(packetWriter); if (packet.Payload?.Length > 0) { packetWriter.Write(packet.Payload, 0, packet.Payload.Length); } byte fixedHeader = 0; if (packet.Retain) { fixedHeader |= 0x01; } fixedHeader |= (byte)((byte)packet.QualityOfServiceLevel << 1); if (packet.Dup) { fixedHeader |= 0x08; } return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader)); }