public MqttUnexpectedDisconnectReceivedException(MqttDisconnectPacket disconnectPacket)
     : base($"Unexpected DISCONNECT (Reason code={disconnectPacket.ReasonCode}) received.")
 {
     ReasonCode            = disconnectPacket.ReasonCode;
     SessionExpiryInterval = disconnectPacket.Properties?.SessionExpiryInterval;
     ReasonString          = disconnectPacket.Properties?.ReasonString;
     ServerReference       = disconnectPacket.Properties?.ServerReference;
     UserProperties        = disconnectPacket.Properties?.UserProperties;
 }
Exemplo n.º 2
0
        Task ProcessReceivedDisconnectPacket(MqttDisconnectPacket disconnectPacket)
        {
            _disconnectReason = (MqttClientDisconnectReason)(disconnectPacket.ReasonCode ?? MqttDisconnectReasonCode.NormalDisconnection);

            // Also dispatch disconnect to waiting threads to generate a proper exception.
            _packetDispatcher.FailAll(new MqttUnexpectedDisconnectReceivedException(disconnectPacket));

            return(DisconnectInternalAsync(_packetReceiverTask, null, null));
        }
Exemplo n.º 3
0
        Task ProcessReceivedDisconnectPacket(MqttDisconnectPacket disconnectPacket)
        {
            _disconnectReason = (MqttClientDisconnectReason)(disconnectPacket.ReasonCode ?? MqttDisconnectReasonCode.NormalDisconnection);

            // Also dispatch disconnect to waiting threads to generate a proper exception.
            _packetDispatcher.Dispatch(disconnectPacket);

            if (!DisconnectIsPending())
            {
                return(DisconnectInternalAsync(_packetReceiverTask, null, null));
            }

            return(PlatformAbstractionLayer.CompletedTask);
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions options)
        {
            var packet = new MqttDisconnectPacket();

            if (options == null)
            {
                packet.ReasonCode = MqttDisconnectReasonCode.NormalDisconnection;
            }
            else
            {
                packet.ReasonCode = (MqttDisconnectReasonCode)options.ReasonCode;
            }

            return(packet);
        }
Exemplo n.º 6
0
        private static MqttBasePacket DecodeDisconnectPacket(IMqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            var packet = new MqttDisconnectPacket
            {
                ReasonCode = (MqttDisconnectReasonCode)body.ReadByte()
            };

            var propertiesReader = new MqttV500PropertiesReader(body);

            while (propertiesReader.MoveNext())
            {
                if (packet.Properties == null)
                {
                    packet.Properties = new MqttDisconnectPacketProperties();
                }

                if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval)
                {
                    packet.Properties.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString)
                {
                    packet.Properties.ReasonString = propertiesReader.ReadReasonString();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ServerReference)
                {
                    packet.Properties.ServerReference = propertiesReader.ReadServerReference();
                }
                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(MqttDisconnectPacket));
                }
            }

            return(packet);
        }
Exemplo n.º 7
0
        public void Serialize_Full_MqttDisconnectPacket_V500()
        {
            var disconnectPacket = new MqttDisconnectPacket
            {
                ReasonCode            = MqttDisconnectReasonCode.QuotaExceeded,
                ReasonString          = "ReasonString",
                ServerReference       = "ServerReference",
                SessionExpiryInterval = 234,
                UserProperties        = new List <MqttUserProperty>
                {
                    new MqttUserProperty("Foo", "Bar")
                }
            };

            var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(disconnectPacket, MqttProtocolVersion.V500);

            Assert.AreEqual(disconnectPacket.ReasonCode, deserialized.ReasonCode);
            Assert.AreEqual(disconnectPacket.ReasonString, deserialized.ReasonString);
            Assert.AreEqual(disconnectPacket.ServerReference, deserialized.ServerReference);
            Assert.AreEqual(disconnectPacket.SessionExpiryInterval, deserialized.SessionExpiryInterval);
            CollectionAssert.AreEqual(disconnectPacket.UserProperties, deserialized.UserProperties);
        }
Exemplo n.º 8
0
        public void Serialize_Full_MqttDisconnectPacket_V311()
        {
            var disconnectPacket = new MqttDisconnectPacket
            {
                ReasonCode            = MqttDisconnectReasonCode.NormalDisconnection, // MQTTv3 has no other values than this.
                ReasonString          = "ReasonString",
                ServerReference       = "ServerReference",
                SessionExpiryInterval = 234,
                UserProperties        = new List <MqttUserProperty>
                {
                    new MqttUserProperty("Foo", "Bar")
                }
            };

            var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(disconnectPacket, MqttProtocolVersion.V311);

            Assert.AreEqual(disconnectPacket.ReasonCode, deserialized.ReasonCode);
            Assert.AreEqual(null, deserialized.ReasonString);        // Not supported in v3.1.1
            Assert.AreEqual(null, deserialized.ServerReference);     // Not supported in v3.1.1
            Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
            CollectionAssert.AreEqual(null, deserialized.UserProperties);
        }
Exemplo n.º 9
0
        private static byte EncodeDisconnectPacket(MqttDisconnectPacket packet, IMqttPacketWriter packetWriter)
        {
            if (!packet.ReasonCode.HasValue)
            {
                ThrowReasonCodeNotSetException();
            }

            packetWriter.Write((byte)packet.ReasonCode.Value);

            var propertiesWriter = new MqttV500PropertiesWriter();

            if (packet.Properties != null)
            {
                propertiesWriter.WriteServerReference(packet.Properties.ServerReference);
                propertiesWriter.WriteReasonString(packet.Properties.ReasonString);
                propertiesWriter.WriteSessionExpiryInterval(packet.Properties.SessionExpiryInterval);
                propertiesWriter.WriteUserProperties(packet.Properties.UserProperties);
            }

            propertiesWriter.WriteTo(packetWriter);

            return(MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Disconnect));
        }
Exemplo n.º 10
0
        MqttPacket DecodeDisconnectPacket(ArraySegment <byte> body)
        {
            ThrowIfBodyIsEmpty(body);

            _bufferReader.SetBuffer(body.Array, body.Offset, body.Count);

            var packet = new MqttDisconnectPacket
            {
                ReasonCode = (MqttDisconnectReasonCode)_bufferReader.ReadByte()
            };

            var propertiesReader = new MqttV5PropertiesReader(_bufferReader);

            while (propertiesReader.MoveNext())
            {
                if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval)
                {
                    packet.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString)
                {
                    packet.ReasonString = propertiesReader.ReadReasonString();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ServerReference)
                {
                    packet.ServerReference = propertiesReader.ReadServerReference();
                }
                else
                {
                    propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttDisconnectPacket));
                }
            }

            packet.UserProperties = propertiesReader.CollectedUserProperties;

            return(packet);
        }
Exemplo n.º 11
0
 private static byte Serialize(MqttDisconnectPacket packet, MqttPacketWriter writer)
 {
     return(SerializeEmptyPacketAsync(MqttControlPacketType.Disconnect, writer));
 }
 private Task SerializeAsync(MqttDisconnectPacket packet, IMqttCommunicationChannel destination)
 {
     return(SerializeEmptyPacketAsync(MqttControlPacketType.Disconnect, destination));
 }