示例#1
0
        /// <summary>
        ///     Handles the publish complete, for messages that are undergoing Qos ExactlyOnce processing.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns>Boolean value indicating whether the message was successfull processed.</returns>
        private bool HandlePublishRelease(MqttMessage msg)
        {
            var pubRelMsg      = (MqttPublishReleaseMessage)msg;
            var publishSuccess = true;

            try {
                MqttPublishMessage pubMsg;
                receivedMessages.TryGetValue(pubRelMsg.VariableHeader.MessageIdentifier, out pubMsg);
                if (pubMsg != null)
                {
                    receivedMessages.Remove(pubRelMsg.VariableHeader.MessageIdentifier);

                    // send the message for processing to whoever is waiting.
                    var topic = new PublicationTopic(pubMsg.VariableHeader.TopicName);
                    OnMessageReceived(topic, pubMsg);

                    var compMsg = new MqttPublishCompleteMessage()
                                  .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                    connectionHandler.SendMessage(compMsg);
                }
            } catch (ArgumentException ex) {
                Log.Warn(m => m("Message recieved which contained topic ({0}) that was not valid according to MQTT Spec was suppressed.",
                                pubRelMsg.VariableHeader.TopicName), ex);
                publishSuccess = false;
            } catch (Exception ex) {
                Log.Error(m => m("An error occurred while processing a publish release message received from a broker ({0}).", msg), ex);
                publishSuccess = false;
            }

            return(publishSuccess);
        }
示例#2
0
        /// <summary>
        /// Handles the publish complete, for messages that are undergoing Qos ExactlyOnce processing.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns>Boolean value indicating whether the message was successfull processed.</returns>
        private bool HandlePublishRelease(MqttMessage msg)
        {
            MqttPublishReleaseMessage pubRelMsg = (MqttPublishReleaseMessage)msg;
            bool publishSuccess = false;

            MqttPublishMessage pubMsg;

            receivedMessages.TryGetValue(pubRelMsg.VariableHeader.MessageIdentifier, out pubMsg);
            if (pubMsg != null)
            {
                receivedMessages.Remove(pubRelMsg.VariableHeader.MessageIdentifier);

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

                MqttPublishCompleteMessage compMsg = new MqttPublishCompleteMessage()
                                                     .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                connectionHandler.SendMessage(compMsg);
            }
            else
            {
                // TODO: if we receive a publish release but haven't seen the original publish message, what do we do?!
            }

            return(publishSuccess);
        }
示例#3
0
        public void BasicSerialization()
        {
            var expected = new[]
            {
                (byte)0x70,
                (byte)0x02,
                (byte)0x0,
                (byte)0x4,
            };

            MqttPublishCompleteMessage msg = new MqttPublishCompleteMessage().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.
        }
示例#4
0
        /// <summary>
        ///     Handles the publish complete, for messages that are undergoing Qos ExactlyOnce processing.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns>Boolean value indicating whether the message was successfull processed.</returns>
        private bool HandlePublishRelease(MqttMessage msg) {
            var pubRelMsg = (MqttPublishReleaseMessage) msg;
            var publishSuccess = true;

            try {
                MqttPublishMessage pubMsg;
                receivedMessages.TryGetValue(pubRelMsg.VariableHeader.MessageIdentifier, out pubMsg);
                if (pubMsg != null) {
                    receivedMessages.Remove(pubRelMsg.VariableHeader.MessageIdentifier);

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

                    var compMsg = new MqttPublishCompleteMessage()
                        .WithMessageIdentifier(pubMsg.VariableHeader.MessageIdentifier);
                    connectionHandler.SendMessage(compMsg);
                }
            } catch (Exception ex) {
                Log.Error(m => m("An error occurred while processing a publish release message received from a broker ({0}).", msg), ex);
                publishSuccess = false;
            }

            return publishSuccess;
        }
示例#5
0
        /*
         *
         * The callbacks below are to deal with workflow when sending messages TO a broker.
         *
         */

        /// <summary>
        /// Handles a publish complete message received from a broker.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns>true if the message flow completed successfully, otherwise false.</returns>
        private bool HandlePublishComplete(MqttMessage msg)
        {
            MqttPublishCompleteMessage compMsg = (MqttPublishCompleteMessage)msg;

            return(publishedMessages.Remove(compMsg.VariableHeader.MessageIdentifier));
        }