Esempio n. 1
0
        /// <summary>
        ///     Handles the receipt of publish messages from a message broker.
        /// </summary>
        /// <param name="msg">The message that was published.</param>
        /// <returns></returns>
        private bool HandlePublish(MqttMessage msg)
        {
            var pubMsg         = (MqttPublishMessage)msg;
            var publishSuccess = true;

            try {
                var topic = new PublicationTopic(pubMsg.VariableHeader.TopicName);
                if (pubMsg.Header.Qos == MqttQos.AtMostOnce)
                {
                    // QOS AtMostOnce 0 require no response.

                    // send the message for processing to whoever is waiting.
                    OnMessageReceived(topic, pubMsg);
                }
                else if (pubMsg.Header.Qos == MqttQos.AtLeastOnce)
                {
                    // QOS AtLeastOnce 1 require an acknowledgement

                    // send the message for processing to whoever is waiting.
                    OnMessageReceived(topic, pubMsg);

                    var ackMsg = new MqttPublishAckMessage()
                                 .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                    connectionHandler.SendMessage(ackMsg);
                }
                else if (pubMsg.Header.Qos == MqttQos.ExactlyOnce)
                {
                    // QOS ExactlyOnce means we can't give it away yet, we gotta do a handshake
                    // to make sure the broker knows we got it, and we know he knows we got it.

                    // if we've already got it thats ok, it just means its being republished because
                    // of a handshake breakdown, overwrite our existing one for the sake of it
                    if (!receivedMessages.ContainsKey(pubMsg.VariableHeader.MessageIdentifier))
                    {
                        receivedMessages[pubMsg.VariableHeader.MessageIdentifier] = pubMsg;
                    }

                    var pubRecv = new MqttPublishReceivedMessage()
                                  .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                    connectionHandler.SendMessage(pubRecv);
                }
            } catch (ArgumentException ex) {
                Log.Warn(m => m("Message recieved which contained topic ({0}) that was not valid according to MQTT Spec was suppressed.",
                                pubMsg.VariableHeader.TopicName), ex);
                publishSuccess = false;
            } catch (Exception ex) {
                Log.Error(m => m("An error occurred while processing a message received from a broker ({0}).", msg), ex);
                publishSuccess = false;
            }
            return(publishSuccess);
        }
Esempio n. 2
0
        /// <summary>
        /// Handles publish received messages during processing of QOS level 2 (Exactly once) messages.
        /// </summary>
        /// <param name="msg">The publish received message</param>
        /// <returns>true or false, depending on the success of message processing.</returns>
        private bool HandlePublishReceived(MqttMessage msg)
        {
            MqttPublishReceivedMessage recvMsg = (MqttPublishReceivedMessage)msg;

            // if we've got a matching message, respond with a "ok release it for processing"
            if (publishedMessages.ContainsKey(recvMsg.VariableHeader.MessageIdentifier))
            {
                MqttPublishReleaseMessage relMsg = new MqttPublishReleaseMessage()
                                                   .WithMessageIdentifier(recvMsg.VariableHeader.MessageIdentifier);
                connectionHandler.SendMessage(relMsg);
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Handles the receipt of publish messages from a message broker.
        /// </summary>
        /// <param name="msg">The message that was published.</param>
        /// <returns></returns>
        private bool HandlePublish(MqttMessage msg)
        {
            MqttPublishMessage pubMsg = (MqttPublishMessage)msg;
            bool publishSuccess       = false;

            if (pubMsg.Header.Qos == MqttQos.AtMostOnce)
            {
                // QOS AtMostOnce 0 require no response.

                // send the message for processing to whoever is waiting.
                publishSuccess = publishMessageCallback(pubMsg);
            }
            else if (pubMsg.Header.Qos == MqttQos.AtLeastOnce)
            {
                // QOS AtLeastOnce 1 require an acknowledgement

                // send the message for processing to whoever is waiting.
                publishSuccess = publishMessageCallback(pubMsg);

                MqttPublishAckMessage ackMsg = new MqttPublishAckMessage()
                                               .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                connectionHandler.SendMessage(ackMsg);
            }
            else if (pubMsg.Header.Qos == MqttQos.ExactlyOnce)
            {
                // QOS ExactlyOnce means we can't give it away yet, we gotta do a handshake
                // to make sure the broker knows we got it, and we know he knows we got it.

                // if we've already got it thats ok, it just means its being republished because
                // of a handshake breakdown, overwrite our existing one for the sake of it
                if (!receivedMessages.ContainsKey(pubMsg.VariableHeader.MessageIdentifier))
                {
                    receivedMessages[pubMsg.VariableHeader.MessageIdentifier] = pubMsg;
                }

                MqttPublishReceivedMessage pubRecv = new MqttPublishReceivedMessage()
                                                     .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                connectionHandler.SendMessage(pubRecv);

                publishSuccess = true;
            }

            return(publishSuccess);
        }
Esempio n. 4
0
        public void BasicSerialization()
        {
            var expected = new[]
            {
                (byte)0x50,
                (byte)0x02,
                (byte)0x0,
                (byte)0x4,
            };

            MqttPublishReceivedMessage msg = new MqttPublishReceivedMessage().WithMessageIdentifier(4);
            Console.WriteLine(msg.ToString());

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal<int>(expected.Length, actual.Length);
            Assert.Equal<byte>(expected[0], actual[0]); // msg type of header
            Assert.Equal<byte>(expected[1], actual[1]); // remaining length
            Assert.Equal<byte>(expected[2], actual[2]); // connect ack - compression? always empty
            Assert.Equal<byte>(expected[3], actual[3]); // return code.
        }