public bool TryDispatch(MqttPacket packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            ushort identifier = 0;

            if (packet is MqttPacketWithIdentifier packetWithIdentifier)
            {
                identifier = packetWithIdentifier.PacketIdentifier;
            }

            var packetType = packet.GetType();
            var awaitables = new List <IMqttPacketAwaitable>();

            lock (_awaitables)
            {
                for (var i = _awaitables.Count - 1; i >= 0; i--)
                {
                    var entry = _awaitables[i];

                    // Note: The PingRespPacket will also arrive here and has NO identifier but there
                    // is code which waits for it. So the code must be able to deal with filters which
                    // are referring to the type only (identifier is 0)!
                    if (entry.Filter.Type != packetType || entry.Filter.Identifier != identifier)
                    {
                        continue;
                    }

                    awaitables.Add(entry);
                    _awaitables.RemoveAt(i);
                }
            }

            foreach (var matchingEntry in awaitables)
            {
                matchingEntry.Complete(packet);
            }

            return(awaitables.Count > 0);
        }
Ejemplo n.º 2
0
 public void AssertIsConnAckPacket(MqttPacket packet)
 {
     Assert.AreEqual(packet.GetType(), typeof(MqttConnAckPacket));
 }
Ejemplo n.º 3
0
        static void ThrowIfPacketIdentifierIsInvalid(ushort packetIdentifier, MqttPacket packet)
        {
            // SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1].

            if (packetIdentifier == 0)
            {
                throw new MqttProtocolViolationException($"Packet identifier is not set for {packet.GetType().Name}.");
            }
        }