public void ProcessReceivedMessage(MqttMessage mess) { try { // Check if the message is an acknowledgement if (mess is MqttLib.Core.Messages.MqttAcknowledgeMessage) { MqttAcknowledgeMessage ackMess = (MqttAcknowledgeMessage)mess; removeMessage(ackMess.AckID); } // Check if the message is a PUBREC else if (mess is MqttLib.Core.Messages.MqttPubrecMessage) { MqttPubrecMessage pubrec = (MqttPubrecMessage)mess; // Remove the initial publish message removeMessage(pubrec.AckID); // Send a pubrel message _strManager.SendMessage(new MqttPubrelMessage(pubrec.AckID)); } // Check if the message is a PUBCOMP else if (mess is MqttLib.Core.Messages.MqttPubcompMessage) { MqttPubcompMessage pubcomp = (MqttPubcompMessage)mess; // Remove the PUBREL message removeMessage(pubcomp.AckID); } else if (mess is MqttLib.Core.Messages.MqttPubrelMessage) { MqttPubrelMessage pubrel = (MqttPubrelMessage)mess; _strManager.SendMessage(new MqttPubcompMessage(pubrel.AckID)); } else if (mess is MqttLib.Core.Messages.MqttPublishMessage) { if (mess.QualityOfService == QoS.AtLeastOnce) { // Queue an acknowlegement _responses.Add(mess.MessageID, new MqttPubackMessage(mess.MessageID)); } else if (mess.QualityOfService == QoS.OnceAndOnceOnly) { _responses.Add(mess.MessageID, new MqttPubrelMessage(mess.MessageID)); } } // Raise a MessageReceivedEvent OnMessageReceived(mess); } catch (Exception e) { // we may have just lost the connection Log.Write(LogLevel.ERROR, e.ToString()); // important messages (>QoS0) will be resent by broker // we should just ignore the message with no further processing. } }
public void SendMessage(MqttMessage mess) { if (_connected) { qosManager.ProcessSentMessage(mess); mess.Serialise(_channel); } else { throw new NotConnectedException("Not connected to any stream, call Connect() first"); } }
private void OnMessageReceived(MqttMessage mess) { if (MessageReceived != null) { MessageReceived(this, new MqttMessageReceivedEventArgs(mess)); } }
public void ProcessSentMessage(MqttMessage mess) { // QoS > 0 if (mess.QualityOfService > QoS.BestEfforts && !mess.Duplicate) { lock (_messages) { _messages.Add(mess.MessageID, mess); } } }
public MqttMessageReceivedEventArgs(MqttMessage message) { _message = message; }
public void ProcessSentMessage(MqttMessage mess) { // QoS > 0 if (mess.QualityOfService > QoS.BestEfforts && !mess.Duplicate) { lock (_messages) { if (mess is MqttLib.Core.Messages.MqttAcknowledgeMessage) { MqttAcknowledgeMessage ackMess = (MqttAcknowledgeMessage)mess; _messages.Add(ackMess.AckID, mess); } else if (mess is MqttLib.Core.Messages.MqttPubrecMessage) { MqttPubrecMessage pubrec = (MqttPubrecMessage)mess; _messages.Add(pubrec.AckID, mess); } // Check if the message is a PUBCOMP else if (mess is MqttLib.Core.Messages.MqttPubcompMessage) { MqttPubcompMessage pubcomp = (MqttPubcompMessage)mess; _messages.Add(pubcomp.AckID, mess); } else if (mess is MqttLib.Core.Messages.MqttPubrelMessage) { MqttPubrelMessage pubrel = (MqttPubrelMessage)mess; _messages.Add(pubrel.AckID, mess); } else { if (!_messages.Contains(mess.MessageID)) { _messages.Add(mess.MessageID, mess); } } } } }