示例#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
        private void SubscribeReceived(IMqttSession session, MqttSubscribeMessage subscribeMessage)
        {
            MqttSubscribeAckMessage subAck = new MqttSubscribeAckMessage().WithMessageIdentifier(subscribeMessage.VariableHeader.MessageIdentifier);

            AddSubscriptions(session, subscribeMessage.Payload.Subscriptions);
            session.Write(subAck);
            PublishMessages(session, storageProvider.GetRetained(subscribeMessage.Payload.Subscriptions), true);
        }
示例#3
0
 private void PublishMessages(IMqttSession session, IEnumerable <Tuple <string, MqttQos, byte[]> > messages, bool retained)
 {
     foreach (var message in messages)
     {
         string messageId = null;
         short? packetId  = null;
         //QOS 1 or 2, store in storage, and in session.
         if (message.Item2 != MqttQos.BestEffort)
         {
             messageId = storageProvider.StoreMessage(new InFlightMessage(message.Item1, message.Item3));
             session.Publish(messageId, message.Item2);
         }
         else
         {
             //QoS 0 just publish, that way the session can keep a straight up queue and not block QoS 0 messages from
             //intervening.
             MqttPublishMessage publishMessage = new MqttPublishMessage().WithQos(message.Item2).ToTopic(message.Item1).PublishData(message.Item3);
             publishMessage.Header.Retain    = retained;
             publishMessage.Header.Duplicate = false;
             session.Write(publishMessage);
         }
     }
 }