コード例 #1
0
        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));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        byte EncodeConnAckPacket(MqttConnAckPacket packet, MqttBufferWriter bufferWriter)
        {
            bufferWriter.WriteByte(0); // Reserved.
            bufferWriter.WriteByte((byte)packet.ReturnCode);

            return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.ConnAck));
        }
コード例 #5
0
 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());
 }
コード例 #6
0
        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));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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));
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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));
        }
コード例 #12
0
        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));
        }
コード例 #13
0
        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));
        }
コード例 #14
0
        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));
        }
コード例 #15
0
        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));
        }
コード例 #16
0
        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));
        }
コード例 #17
0
        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));
        }
コード例 #18
0
        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));
        }
コード例 #19
0
        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));
        }
コード例 #20
0
        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));
        }
コード例 #21
0
 static byte EncodePingRespPacket()
 {
     return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.PingResp));
 }
コード例 #22
0
 static byte EncodeEmptyPacket(MqttControlPacketType type)
 {
     return(MqttBufferWriter.BuildFixedHeader(type));
 }