static byte EncodeSubAckPacket(MqttSubAckPacket packet, MqttBufferWriter bufferWriter) { if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("SubAck packet has no packet identifier."); } bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); if (packet.ReasonCodes.Any()) { foreach (var packetSubscribeReturnCode in packet.ReasonCodes) { if (packetSubscribeReturnCode == MqttSubscribeReasonCode.GrantedQoS0) { bufferWriter.WriteByte((byte)MqttSubscribeReturnCode.SuccessMaximumQoS0); } else if (packetSubscribeReturnCode == MqttSubscribeReasonCode.GrantedQoS1) { bufferWriter.WriteByte((byte)MqttSubscribeReturnCode.SuccessMaximumQoS1); } else if (packetSubscribeReturnCode == MqttSubscribeReasonCode.GrantedQoS2) { bufferWriter.WriteByte((byte)MqttSubscribeReturnCode.SuccessMaximumQoS2); } else { bufferWriter.WriteByte((byte)MqttSubscribeReturnCode.Failure); } } } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.SubAck)); }
byte EncodeConnAckPacket(MqttConnAckPacket packet) { byte connectAcknowledgeFlags = 0x0; if (packet.IsSessionPresent) { connectAcknowledgeFlags |= 0x1; } _bufferWriter.WriteByte(connectAcknowledgeFlags); _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteSessionExpiryInterval(packet.SessionExpiryInterval); _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod); _propertiesWriter.WriteAuthenticationData(packet.AuthenticationData); _propertiesWriter.WriteRetainAvailable(packet.RetainAvailable); _propertiesWriter.WriteReceiveMaximum(packet.ReceiveMaximum); _propertiesWriter.WriteMaximumQoS(packet.MaximumQoS); _propertiesWriter.WriteAssignedClientIdentifier(packet.AssignedClientIdentifier); _propertiesWriter.WriteTopicAliasMaximum(packet.TopicAliasMaximum); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteMaximumPacketSize(packet.MaximumPacketSize); _propertiesWriter.WriteWildcardSubscriptionAvailable(packet.WildcardSubscriptionAvailable); _propertiesWriter.WriteSubscriptionIdentifiersAvailable(packet.SubscriptionIdentifiersAvailable); _propertiesWriter.WriteSharedSubscriptionAvailable(packet.SharedSubscriptionAvailable); _propertiesWriter.WriteServerKeepAlive(packet.ServerKeepAlive); _propertiesWriter.WriteResponseInformation(packet.ResponseInformation); _propertiesWriter.WriteServerReference(packet.ServerReference); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.ConnAck)); }
static byte EncodeSubscribePacket(MqttSubscribePacket packet, MqttBufferWriter bufferWriter) { 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."); } bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); if (packet.TopicFilters?.Count > 0) { foreach (var topicFilter in packet.TopicFilters) { bufferWriter.WriteString(topicFilter.Topic); bufferWriter.WriteByte((byte)topicFilter.QualityOfServiceLevel); } } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02)); }
byte EncodeConnAckPacket(MqttConnAckPacket packet, MqttBufferWriter bufferWriter) { bufferWriter.WriteByte(0); // Reserved. bufferWriter.WriteByte((byte)packet.ReturnCode); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.ConnAck)); }
public static byte[] AddMqttHeader(this MqttBufferWriter writer, MqttControlPacketType header, byte[] body) { writer.WriteByte(MqttBufferWriter.BuildFixedHeader(header)); writer.WriteVariableByteInteger((uint)body.Length); writer.WriteBinary(body, 0, body.Length); return(writer.GetBuffer()); }
byte EncodePublishPacket(MqttPublishPacket packet) { if (packet.QualityOfServiceLevel == 0 && packet.Dup) { throw new MqttProtocolViolationException("Dup flag must be false for QoS 0 packets [MQTT-3.3.1-2]."); } _bufferWriter.WriteString(packet.Topic); if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("Publish packet has no packet identifier."); } _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); } else { if (packet.PacketIdentifier > 0) { throw new MqttProtocolViolationException("Packet identifier must be 0 if QoS == 0 [MQTT-2.3.1-5]."); } } _propertiesWriter.WriteContentType(packet.ContentType); _propertiesWriter.WriteCorrelationData(packet.CorrelationData); _propertiesWriter.WriteMessageExpiryInterval(packet.MessageExpiryInterval); _propertiesWriter.WritePayloadFormatIndicator(packet.PayloadFormatIndicator); _propertiesWriter.WriteResponseTopic(packet.ResponseTopic); _propertiesWriter.WriteSubscriptionIdentifiers(packet.SubscriptionIdentifiers); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTopicAlias(packet.TopicAlias); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); // The payload is the past part of the packet. But it is not added here in order to keep // memory allocation low. byte fixedHeader = 0; if (packet.Retain) { fixedHeader |= 0x01; } fixedHeader |= (byte)((byte)packet.QualityOfServiceLevel << 1); if (packet.Dup) { fixedHeader |= 0x08; } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader)); }
static byte EncodeUnsubAckPacket(MqttUnsubAckPacket packet, MqttBufferWriter bufferWriter) { if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("UnsubAck packet has no packet identifier."); } bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck)); }
byte EncodeConnAckPacketV311(MqttConnAckPacket packet, MqttBufferWriter bufferWriter) { byte connectAcknowledgeFlags = 0x0; if (packet.IsSessionPresent) { connectAcknowledgeFlags |= 0x1; } bufferWriter.WriteByte(connectAcknowledgeFlags); bufferWriter.WriteByte((byte)packet.ReturnCode); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.ConnAck)); }
byte EncodeDisconnectPacket(MqttDisconnectPacket packet) { _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteServerReference(packet.ServerReference); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteSessionExpiryInterval(packet.SessionExpiryInterval); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Disconnect)); }
byte EncodeAuthPacket(MqttAuthPacket packet) { _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod); _propertiesWriter.WriteAuthenticationData(packet.AuthenticationData); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Auth)); }
byte EncodeSubscribePacket(MqttSubscribePacket packet) { 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); _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); if (packet.SubscriptionIdentifier > 0) { _propertiesWriter.WriteSubscriptionIdentifier(packet.SubscriptionIdentifier); } _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); if (packet.TopicFilters?.Count > 0) { foreach (var topicFilter in packet.TopicFilters) { _bufferWriter.WriteString(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); } _bufferWriter.WriteByte(options); } } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02)); }
byte EncodePubRelPacket(MqttPubRelPacket packet) { ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); if (_propertiesWriter.Length > 0 || packet.ReasonCode != MqttPubRelReasonCode.Success) { _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02)); }
byte EncodeUnsubAckPacket(MqttUnsubAckPacket packet) { ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet); _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); foreach (var reasonCode in packet.ReasonCodes) { _bufferWriter.WriteByte((byte)reasonCode); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck)); }
static byte EncodePublishPacket(MqttPublishPacket packet, MqttBufferWriter bufferWriter) { ValidatePublishPacket(packet); bufferWriter.WriteString(packet.Topic); if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("Publish packet has no packet identifier."); } bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); } else { if (packet.PacketIdentifier > 0) { throw new MqttProtocolViolationException("Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5]."); } } // The payload is the past part of the packet. But it is not added here in order to keep // memory allocation low. byte fixedHeader = 0; if (packet.Retain) { fixedHeader |= 0x01; } fixedHeader |= (byte)((byte)packet.QualityOfServiceLevel << 1); if (packet.Dup) { fixedHeader |= 0x08; } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader)); }
byte EncodePubAckPacket(MqttPubAckPacket packet) { if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("PubAck packet has no packet identifier."); } _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); if (_bufferWriter.Length > 0 || packet.ReasonCode != MqttPubAckReasonCode.Success) { _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.PubAck)); }
byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet) { if (packet.TopicFilters?.Any() != true) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2]."); } ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet); _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); foreach (var topicFilter in packet.TopicFilters) { _bufferWriter.WriteString(topicFilter); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02)); }
byte EncodeSubAckPacket(MqttSubAckPacket packet) { if (packet.ReasonCodes?.Any() != true) { throw new MqttProtocolViolationException("At least one reason code must be set[MQTT - 3.8.3 - 3]."); } ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet); _bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); foreach (var reasonCode in packet.ReasonCodes) { _bufferWriter.WriteByte((byte)reasonCode); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.SubAck)); }
static byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet, MqttBufferWriter bufferWriter) { if (!packet.TopicFilters.Any()) { throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2]."); } if (packet.PacketIdentifier == 0) { throw new MqttProtocolViolationException("Unsubscribe packet has no packet identifier."); } bufferWriter.WriteTwoByteInteger(packet.PacketIdentifier); if (packet.TopicFilters?.Any() == true) { foreach (var topicFilter in packet.TopicFilters) { bufferWriter.WriteString(topicFilter); } } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02)); }
byte EncodeConnectPacket(MqttConnectPacket packet) { if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession) { throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7]."); } _bufferWriter.WriteString("MQTT"); _bufferWriter.WriteByte(5); // [3.1.2.2 Protocol Version] byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillFlag) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillQoS << 3); if (packet.WillRetain) { 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; } _bufferWriter.WriteByte(connectFlags); _bufferWriter.WriteTwoByteInteger(packet.KeepAlivePeriod); _propertiesWriter.WriteSessionExpiryInterval(packet.SessionExpiryInterval); _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod); _propertiesWriter.WriteAuthenticationData(packet.AuthenticationData); _propertiesWriter.WriteRequestProblemInformation(packet.RequestProblemInformation); _propertiesWriter.WriteRequestResponseInformation(packet.RequestResponseInformation); _propertiesWriter.WriteReceiveMaximum(packet.ReceiveMaximum); _propertiesWriter.WriteTopicAliasMaximum(packet.TopicAliasMaximum); _propertiesWriter.WriteMaximumPacketSize(packet.MaximumPacketSize); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); _bufferWriter.WriteString(packet.ClientId); if (packet.WillFlag) { _propertiesWriter.WritePayloadFormatIndicator(packet.WillPayloadFormatIndicator); _propertiesWriter.WriteMessageExpiryInterval(packet.WillMessageExpiryInterval); _propertiesWriter.WriteResponseTopic(packet.WillResponseTopic); _propertiesWriter.WriteCorrelationData(packet.WillCorrelationData); _propertiesWriter.WriteContentType(packet.WillContentType); _propertiesWriter.WriteUserProperties(packet.WillUserProperties); _propertiesWriter.WriteWillDelayInterval(packet.WillDelayInterval); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); _bufferWriter.WriteString(packet.WillTopic); _bufferWriter.WriteBinaryData(packet.WillMessage); } if (packet.Username != null) { _bufferWriter.WriteString(packet.Username); } if (packet.Password != null) { _bufferWriter.WriteBinaryData(packet.Password); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }
byte EncodeConnectPacketV311(MqttConnectPacket packet, MqttBufferWriter bufferWriter) { ValidateConnectPacket(packet); bufferWriter.WriteString("MQTT"); bufferWriter.WriteByte(4); // 3.1.2.2 Protocol Level 4 byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillFlag) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillQoS << 3); if (packet.WillRetain) { 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; } bufferWriter.WriteByte(connectFlags); bufferWriter.WriteTwoByteInteger(packet.KeepAlivePeriod); bufferWriter.WriteString(packet.ClientId); if (packet.WillFlag) { bufferWriter.WriteString(packet.WillTopic); bufferWriter.WriteBinaryData(packet.WillMessage); } if (packet.Username != null) { bufferWriter.WriteString(packet.Username); } if (packet.Password != null) { bufferWriter.WriteBinaryData(packet.Password); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }
static byte EncodePingRespPacket() { return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.PingResp)); }
static byte EncodeEmptyPacket(MqttControlPacketType type) { return(MqttBufferWriter.BuildFixedHeader(type)); }