public void PubackBasicDecodeTestv5() { // Arrange byte[] encodedCorrect = new byte[] { 4, 0, 42, 0, 0 }; MokChannel mokChannel = new(encodedCorrect); // Act MqttMsgPuback puback = MqttMsgPuback.Parse(64, MqttProtocolVersion.Version_5, mokChannel); // Assert Assert.Equal((ushort)42, puback.MessageId); }
public void PubackAdvanceDecodeTestv5() { // Arrange byte[] encodedCorrect = new byte[] { 53, 0, 42, 151, 49, 31, 0, 26, 89, 111, 117, 32, 104, 97, 118, 101, 32, 101, 120, 99, 101, 101, 100, 32, 121, 111, 117, 114, 32, 113, 117, 111, 116, 97, 38, 0, 4, 80, 114, 111, 112, 0, 1, 49, 38, 0, 4, 80, 114, 111, 112, 0, 1, 50 }; MokChannel mokChannel = new(encodedCorrect); // Act MqttMsgPuback puback = MqttMsgPuback.Parse(64, MqttProtocolVersion.Version_5, mokChannel); // Assert Assert.Equal((ushort)42, puback.MessageId); Assert.Equal((byte)puback.ReasonCode, (byte)MqttReasonCode.QuotaExceeded); Assert.Equal(puback.Reason, "You have exceed your quota"); Assert.Equal(puback.UserProperties.Count, 2); var prop = new UserProperty("Prop", "1"); Assert.Equal(((UserProperty)puback.UserProperties[0]).Name, prop.Name); Assert.Equal(((UserProperty)puback.UserProperties[0]).Value, prop.Value); prop = new UserProperty("Prop", "2"); Assert.Equal(((UserProperty)puback.UserProperties[1]).Name, prop.Name); Assert.Equal(((UserProperty)puback.UserProperties[1]).Value, prop.Value); }
public static MqttMsgBase DecodeControlPacket(byte[] data) { byte fixedHeaderFirstByte = (byte)(data[0] >> MqttMsgBase.MSG_TYPE_OFFSET); switch (fixedHeaderFirstByte) { case MqttMsgBase.MQTT_MSG_CONNECT_TYPE: { return(MqttMsgConnect.Parse(data)); } case MqttMsgBase.MQTT_MSG_CONNACK_TYPE: { return(MqttMsgConnack.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE: { return(MqttMsgPublish.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBACK_TYPE: { return(MqttMsgPuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREC_TYPE: { return(MqttMsgPubrec.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREL_TYPE: { return(MqttMsgPubrel.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE: { return(MqttMsgPubcomp.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE: { return(MqttMsgSubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBACK_TYPE: { return(MqttMsgSuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE: { return(MqttMsgUnsubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE: { return(MqttMsgUnsuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE: { return(MqttMsgPingReq.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE: { return(MqttMsgPingResp.Parse(data)); } case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE: { CrestronLogger.WriteToLog("PACKETDECODER - Riconosciuto DISCONNNECT: ", 1); return(MqttMsgDisconnect.Parse(data)); } default: { throw new FormatException(); } } }
/// <summary> /// Thread for receiving messages from broker /// </summary> private void ReceiveThread() { int readBytes; byte[] fixedHeaderFirstByte = new byte[1]; byte msgType; while (this.isRunning) { try { // read first byte (fixed header) readBytes = this.socket.Receive(fixedHeaderFirstByte); if (readBytes > 0) { // extract message type from received byte msgType = (byte)((fixedHeaderFirstByte[0] & MqttMsgBase.MSG_TYPE_MASK) >> MqttMsgBase.MSG_TYPE_OFFSET); switch (msgType) { // impossible, broker can't send CONNECT message case MqttMsgBase.MQTT_MSG_CONNECT_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); // CONNACK message received from broker case MqttMsgBase.MQTT_MSG_CONNACK_TYPE: this.msgReceived = MqttMsgConnack.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); break; // impossible, broker can't send PINGREQ message case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); // CONNACK message received from broker case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE: this.msgReceived = MqttMsgPingResp.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); break; // impossible, broker can't send SUBSCRIBE message case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); // SUBACK message received from broker case MqttMsgBase.MQTT_MSG_SUBACK_TYPE: this.msgReceived = MqttMsgSuback.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); // raise subscribed topic event (SUBACK message received) this.OnMqttMsgSubscribed((MqttMsgSuback)this.msgReceived); break; // PUBLISH message received from broker case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE: MqttMsgPublish msgReceived = MqttMsgPublish.Parse(fixedHeaderFirstByte[0], this.socket); // for QoS Level 1 and 2, client sends PUBACK message to broker if ((this.msgReceived.QosLevel == MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE) || (this.msgReceived.QosLevel == MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE)) { MqttMsgPuback puback = new MqttMsgPuback(); puback.MessageId = (msgReceived).MessageId; this.Send(puback.GetBytes()); } // raise PUBLISH message received event this.OnMqttMsgPublishReceived(msgReceived); break; // PUBACK message received from broker case MqttMsgBase.MQTT_MSG_PUBACK_TYPE: this.msgReceived = MqttMsgPuback.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); // raise published message event // (PUBACK received for QoS Level 1) this.OnMqttMsgPublished(((MqttMsgPuback)this.msgReceived).MessageId); break; // PUBREC message received from broker case MqttMsgBase.MQTT_MSG_PUBREC_TYPE: this.msgReceived = MqttMsgPubrec.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); break; // impossible, broker can't send PUBREL message case MqttMsgBase.MQTT_MSG_PUBREL_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); // PUBCOMP message received from broker case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE: this.msgReceived = MqttMsgPubcomp.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); // raise published message event // (PUBCOMP received for QoS Level 2) this.OnMqttMsgPublished(((MqttMsgPuback)this.msgReceived).MessageId); break; // impossible, broker can't send UNSUBSCRIBE message case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); // UNSUBACK message received from broker case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE: this.msgReceived = MqttMsgUnsuback.Parse(fixedHeaderFirstByte[0], this.socket); this.endReceiving.Set(); // raise unsubscribed topic event this.OnMqttMsgUnsubscribed(((MqttMsgUnsuback)this.msgReceived).MessageId); break; default: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); } this.exReceiving = null; } } catch (Exception) { this.exReceiving = new MqttCommunicationException(); } } }
public MqttMsgBase DecodeControlPacket(byte[] data) { byte fixedHeaderFirstByte = (byte)(data[0] >> MSG_TYPE_OFFSET); switch (fixedHeaderFirstByte) { case MqttMsgBase.MQTT_MSG_CONNECT_TYPE: { return(MqttMsgConnect.Parse(data)); } case MqttMsgBase.MQTT_MSG_CONNACK_TYPE: { return(MqttMsgConnack.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE: { return(MqttMsgPublish.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBACK_TYPE: { return(MqttMsgPuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREC_TYPE: { return(MqttMsgPubrec.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREL_TYPE: { return(MqttMsgPubrel.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE: { return(MqttMsgPubcomp.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE: { return(MqttMsgSubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBACK_TYPE: { return(MqttMsgSuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE: { return(MqttMsgUnsubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE: { return(MqttMsgUnsuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE: { return(MqttMsgPingReq.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE: { return(MqttMsgPingResp.Parse(data)); } case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE: { return(MqttMsgDisconnect.Parse(data)); } default: { throw new FormatException(" First byte shifted : " + fixedHeaderFirstByte); } } }
public void ProcessReceivedMessage(MqttRawMessage rawMessage) { if (!rawMessage.Connection.IsRunning) { return; } // update last message received ticks rawMessage.Connection.LastCommunicationTime = Environment.TickCount; // extract message type from received byte var msgType = (byte)((rawMessage.MessageType & MqttMsgBase.MSG_TYPE_MASK) >> MqttMsgBase.MSG_TYPE_OFFSET); var protocolVersion = (byte)rawMessage.Connection.ProtocolVersion; switch (msgType) { case MqttMsgBase.MQTT_MSG_CONNECT_TYPE: var connect = MqttMsgConnect.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer); rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(connect)); break; case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE: var pingReqest = MqttMsgPingReq.Parse(rawMessage.MessageType, protocolVersion); outgoingMessageHandler.PingResp(rawMessage.Connection); break; case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE: var subscribe = MqttMsgSubscribe.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer, rawMessage.PayloadLength); rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(subscribe)); break; case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE: var publish = MqttMsgPublish.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer, rawMessage.PayloadLength); EnqueueInflight(rawMessage.Connection, publish, MqttMsgFlow.ToAcknowledge); break; case MqttMsgBase.MQTT_MSG_PUBACK_TYPE: // enqueue PUBACK message received (for QoS Level 1) into the internal queue var puback = MqttMsgPuback.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer); EnqueueInternal(rawMessage.Connection, puback); break; case MqttMsgBase.MQTT_MSG_PUBREC_TYPE: // enqueue PUBREC message received (for QoS Level 2) into the internal queue var pubrec = MqttMsgPubrec.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer); EnqueueInternal(rawMessage.Connection, pubrec); break; case MqttMsgBase.MQTT_MSG_PUBREL_TYPE: // enqueue PUBREL message received (for QoS Level 2) into the internal queue var pubrel = MqttMsgPubrel.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer); EnqueueInternal(rawMessage.Connection, pubrel); break; case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE: // enqueue PUBCOMP message received (for QoS Level 2) into the internal queue var pubcomp = MqttMsgPubcomp.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer); EnqueueInternal(rawMessage.Connection, pubcomp); break; case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE: var unsubscribe = MqttMsgUnsubscribe.Parse( rawMessage.MessageType, protocolVersion, rawMessage.PayloadBuffer, rawMessage.PayloadLength); rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(unsubscribe)); break; case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE: var disconnect = MqttMsgDisconnect.Parse(rawMessage.MessageType, protocolVersion); rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(disconnect)); break; case MqttMsgBase.MQTT_MSG_CONNACK_TYPE: case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE: case MqttMsgBase.MQTT_MSG_SUBACK_TYPE: case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); default: throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage); } }
public MqttMsgBase DecodeControlPacket(byte[] data) { #if PACKET_DEBUG CrestronLogger.WriteToLog("MQTTCLIENT - RECEIVE - " + BitConverter.ToString(data), 2); #endif byte fixedHeaderFirstByte = (byte)(data[0] >> MSG_TYPE_OFFSET); switch (fixedHeaderFirstByte) { case MqttMsgBase.MQTT_MSG_CONNECT_TYPE: { return(MqttMsgConnect.Parse(data)); } case MqttMsgBase.MQTT_MSG_CONNACK_TYPE: { return(MqttMsgConnack.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE: { return(MqttMsgPublish.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBACK_TYPE: { return(MqttMsgPuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREC_TYPE: { return(MqttMsgPubrec.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBREL_TYPE: { return(MqttMsgPubrel.Parse(data)); } case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE: { return(MqttMsgPubcomp.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE: { return(MqttMsgSubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_SUBACK_TYPE: { return(MqttMsgSuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE: { return(MqttMsgUnsubscribe.Parse(data)); } case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE: { return(MqttMsgUnsuback.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE: { return(MqttMsgPingReq.Parse(data)); } case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE: { return(MqttMsgPingResp.Parse(data)); } case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE: { return(MqttMsgDisconnect.Parse(data)); } default: { throw new FormatException(" First byte shifted : " + fixedHeaderFirstByte); } } }