internal override MqttMessage Decode(byte[] message) { MqttMessage publishMessage = new PublishMessage(); int index = 0; byte fixedHeader = message[index]; base.DecodeFixedHeader(fixedHeader); int remainingLength = base.DecodeRemainingLength(message); int temp = remainingLength; //increase the fixed header size do { index++; temp = temp / 128; } while (temp > 0); index++; byte[] buffer = new byte[remainingLength]; Buffer.BlockCopy(message, index, buffer, 0, buffer.Length); //base.VariableHeader = new PublishVariableHeader(); index = 0; int length = 0; this.Topic = ByteContainer.DecodeString(buffer, index, out length); index += length; if ((int)QualityOfServiceLevel > 0) { ushort messageId = (ushort)((buffer[index++] << 8) & 0xFF00); messageId |= buffer[index++]; this.MessageId = messageId; length += 2; } //length = ((buffer[index++] << 8) & 0xFF00); //length |= buffer[index++]; byte[] data = new byte[remainingLength - length]; Buffer.BlockCopy(buffer, index, data, 0, data.Length); this.Payload = data; //index = base.VariableHeader.Decode(buffer); //base.Payload = new PublishPayload(); //base.Payload.Decode(buffer, base.VariableHeader, index); return(publishMessage); }
public static MqttMessage DecodeMessage(byte[] message) { byte fixedHeader = message[0]; byte msgType = (byte)(fixedHeader >> 0x04); MqttMessageType messageType = (MqttMessageType)msgType; MqttMessage mqttMessage = null; switch (messageType) { case MqttMessageType.CONNECT: mqttMessage = new ConnectMessage(); break; case MqttMessageType.CONNACK: mqttMessage = new ConnectAckMessage(); break; case MqttMessageType.PUBLISH: mqttMessage = new PublishMessage(); break; case MqttMessageType.PUBACK: mqttMessage = new PublishAckMessage(); break; case MqttMessageType.PUBREC: mqttMessage = new PublishAckMessage(); break; case MqttMessageType.PUBREL: mqttMessage = new PublishAckMessage(); break; case MqttMessageType.PUBCOMP: mqttMessage = new PublishAckMessage(); break; case MqttMessageType.SUBSCRIBE: mqttMessage = new SubscribeMessage(); break; case MqttMessageType.SUBACK: mqttMessage = new SubscriptionAckMessage(); break; case MqttMessageType.UNSUBSCRIBE: mqttMessage = new UnsubscribeMessage(); break; case MqttMessageType.UNSUBACK: mqttMessage = new UnsubscribeAckMessage(); break; case MqttMessageType.PINGREQ: mqttMessage = new PingRequestMessage(); break; case MqttMessageType.PINGRESP: mqttMessage = new PingResponseMessage(); break; case MqttMessageType.DISCONNECT: mqttMessage = new DisconnectMessage(); break; default: throw new InvalidOperationException("Unknown message type."); } mqttMessage.Decode(message); return(mqttMessage); }