예제 #1
0
        internal void PublishReceived(IMqttSession session, MqttPublishMessage publishMessage)
        {
            if (publishMessage.Header.Qos == MqttQos.BestEffort && session.HasQoS2(publishMessage.VariableHeader.MessageIdentifier))
            {
                var pubRec = new MqttPublishReceivedMessage()
                             .WithMessageIdentifier(publishMessage.VariableHeader.MessageIdentifier);
                session.Write(pubRec);
            }
            if (publishMessage.Header.Retain)
            {
                this.storageProvider.PutRetained(publishMessage.VariableHeader.TopicName, publishMessage.Payload.Message.ToArray());
            }
            PublishMessage(GetSubscriptions(publishMessage.VariableHeader.TopicName), publishMessage.VariableHeader.TopicName, publishMessage.Payload.Message.ToArray());

            switch (publishMessage.Header.Qos)
            {
            case MqttQos.AtLeastOnce:
                var puback = new MqttPublishAckMessage().WithMessageIdentifier(publishMessage.VariableHeader.MessageIdentifier);
                session.Write(puback);
                break;

            case MqttQos.AtMostOnce:
                session.StoreQoS2(publishMessage.VariableHeader.MessageIdentifier);
                var pubRec = new MqttPublishReleaseMessage().WithMessageIdentifier(publishMessage.VariableHeader.MessageIdentifier);
                session.Write(pubRec);
                break;

            case MqttQos.BestEffort:
            default:
                break;
            }
        }
예제 #2
0
        internal async Task PublishReceived(IMqttSession session, Stream stream, PublishMessage publishMessage)
        {
            await publishMessage.ReadPayloadAsync();

            //If we've already received the QoS 2 message and forwarded it, then just send the PubRec
            if (publishMessage.QoS == QoS.AtMostOnce && await session.HasQoS2(publishMessage.PacketId.Value))
            {
                await new PubRecMessage(publishMessage.PacketId.Value).Write(stream);
                return;
            }
            if (publishMessage.Retain)
            {
                await storageProvider.PutRetained(publishMessage.Topic, publishMessage.Payload);
            }
            await PublishMessage(await sessionProvider.GetSubscriptions(publishMessage.Topic), publishMessage.Topic, publishMessage.Payload);

            switch (publishMessage.QoS)
            {
            case QoS.AtLeastOnce:
                await new PubAckMessage(publishMessage.PacketId.Value).Write(stream);
                break;

            case QoS.AtMostOnce:
                await session.StoreQoS2(publishMessage.PacketId.Value);

                await new PubRecMessage(publishMessage.PacketId.Value).Write(stream);
                break;

            case QoS.BestEffort:
            default:
                break;
            }
        }