/// <summary> /// Handles the receipt of publish acknowledgement messages. /// </summary> /// <param name="msg">The publish acknowledgement</param> /// <returns>True; always.</returns> /// <remarks> /// This callback simply removes it from the list of published messages. /// </remarks> private bool HandlePublishAcknowledgement(MqttMessage msg) { MqttPublishAckMessage ackMsg = (MqttPublishAckMessage)msg; // if we're expecting an ack for the message, remove it from the list of pubs awaiting ack. if (publishedMessages.Keys.Contains(ackMsg.VariableHeader.MessageIdentifier)) { publishedMessages.Remove(ackMsg.VariableHeader.MessageIdentifier); } return(true); }
/// <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); }
/// <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); }
public void ValidPayload() { // publish ack msg with message identifier 4 var expected = new[] { (byte)0x40, (byte)0x02, (byte)0x0, (byte)0x4, }; MqttPublishAckMessage msg = new MqttPublishAckMessage().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. }