/// <summary> /// Parse bytes for a UNSUBACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>UNSUBACK message instance</returns> public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; MqttMsgUnsuback msg = new MqttMsgUnsuback(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); return(msg); }
/// <summary> /// Parse bytes for a PUBREC message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="socket">Socket connected to the broker</param> /// <returns>PUBREC message instance</returns> public static MqttMsgPubrec Parse(byte fixedHeaderFirstByte, Socket socket) { byte[] buffer; int index = 0; MqttMsgPubrec msg = new MqttMsgPubrec(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(socket); buffer = new byte[remainingLength]; // read bytes from socket... socket.Receive(buffer); // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); return(msg); }
public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { int num1 = 0; MqttMsgSuback mqttMsgSuback1 = new MqttMsgSuback(); if (protocolVersion == (byte)4 && ((uint)fixedHeaderFirstByte & 15U) > 0U) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } int length = MqttMsgBase.decodeRemainingLength(channel); byte[] buffer = new byte[length]; channel.Receive(buffer); MqttMsgSuback mqttMsgSuback2 = mqttMsgSuback1; byte[] numArray1 = buffer; int index1 = num1; int num2 = checked (index1 + 1); int num3 = (int)checked ((ushort)((int)numArray1[index1] << 8 & 65280)); mqttMsgSuback2.messageId = (ushort)num3; MqttMsgSuback mqttMsgSuback3 = mqttMsgSuback1; int messageId = (int)mqttMsgSuback3.messageId; byte[] numArray2 = buffer; int index2 = num2; int num4 = checked (index2 + 1); int num5 = (int)numArray2[index2]; mqttMsgSuback3.messageId = (ushort)(messageId | num5); mqttMsgSuback1.grantedQosLevels = new byte[checked (length - 2)]; int num6 = 0; do { mqttMsgSuback1.grantedQosLevels[checked (num6++)] = buffer[checked (num4++)]; }while (num4 < length); return(mqttMsgSuback1); }
/// <summary> /// Parse bytes for a PUBREL message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>PUBREL message instance</returns> public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; MqttMsgPubrel msg = new MqttMsgPubrel(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBREL_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1) { // only 3.1.0 // read QoS level from fixed header (would be QoS Level 1) msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET); // read DUP flag from fixed header msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01); } // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); return(msg); }
/// <summary> /// Parse bytes for a SUBACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>SUBACK message instance</returns> public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; MqttMsgSuback msg = new MqttMsgSuback(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_SUBACK_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); // payload contains QoS levels granted msg.grantedQosLevels = new byte[remainingLength - MESSAGE_ID_SIZE]; int qosIdx = 0; do { msg.grantedQosLevels[qosIdx++] = buffer[index++]; } while (index < remainingLength); return(msg); }
/// <summary> /// Parse bytes for a DISCONNECT message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>DISCONNECT message instance</returns> public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { MqttMsgDisconnect msg = new MqttMsgDisconnect(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_DISCONNECT_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); // NOTE : remainingLength must be 0 if (remainingLength > 0) { // error } return(msg); }
/// <summary> /// Parse bytes for a PUBLISH message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>PUBLISH message instance</returns> public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; byte[] topicUtf8; int topicUtf8Length; MqttMsgPublish msg = new MqttMsgPublish(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... int received = channel.Receive(buffer); // topic name topicUtf8Length = ((buffer[index++] << 8) & 0xFF00); topicUtf8Length |= buffer[index++]; topicUtf8 = new byte[topicUtf8Length]; Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length); index += topicUtf8Length; msg.topic = new String(Encoding.UTF8.GetChars(topicUtf8)); // read QoS level from fixed header msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET); // read DUP flag from fixed header msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01); // read retain flag from fixed header msg.retain = (((fixedHeaderFirstByte & RETAIN_FLAG_MASK) >> RETAIN_FLAG_OFFSET) == 0x01); // message id is valid only with QOS level 1 or QOS level 2 if ((msg.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) || (msg.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) { // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); } // get payload with message data int messageSize = remainingLength - index; int remaining = messageSize; int messageOffset = 0; msg.message = new byte[messageSize]; // BUG FIX 26/07/2013 : receiving large payload // copy first part of payload data received Array.Copy(buffer, index, msg.message, messageOffset, received - index); remaining -= (received - index); messageOffset += (received - index); // if payload isn't finished while (remaining > 0) { // receive other payload data received = channel.Receive(buffer); Array.Copy(buffer, 0, msg.message, messageOffset, received); remaining -= received; messageOffset += received; } return(msg); }
public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { int num1 = 0; MqttMsgSubscribe mqttMsgSubscribe1 = new MqttMsgSubscribe(); if (protocolVersion == (byte)4 && ((int)fixedHeaderFirstByte & 15) != 2) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } int length1 = MqttMsgBase.decodeRemainingLength(channel); byte[] buffer = new byte[length1]; channel.Receive(buffer); if (protocolVersion == (byte)3) { mqttMsgSubscribe1.qosLevel = checked ((byte)(((int)fixedHeaderFirstByte & 6) >> 1)); mqttMsgSubscribe1.dupFlag = ((int)fixedHeaderFirstByte & 8) >> 3 == 1; mqttMsgSubscribe1.retain = false; } MqttMsgSubscribe mqttMsgSubscribe2 = mqttMsgSubscribe1; byte[] numArray1 = buffer; int index1 = num1; int num2 = checked (index1 + 1); int num3 = (int)checked ((ushort)((int)numArray1[index1] << 8 & 65280)); mqttMsgSubscribe2.messageId = (ushort)num3; MqttMsgSubscribe mqttMsgSubscribe3 = mqttMsgSubscribe1; int messageId = (int)mqttMsgSubscribe3.messageId; byte[] numArray2 = buffer; int index2 = num2; int num4 = checked (index2 + 1); int num5 = (int)numArray2[index2]; mqttMsgSubscribe3.messageId = (ushort)(messageId | num5); IList <string> stringList = (IList <string>) new List <string>(); IList <byte> byteList1 = (IList <byte>) new List <byte>(); do { byte[] numArray3 = buffer; int index3 = num4; int num6 = checked (index3 + 1); int num7 = (int)numArray3[index3] << 8 & 65280; byte[] numArray4 = buffer; int index4 = num6; int sourceIndex = checked (index4 + 1); int num8 = (int)numArray4[index4]; int length2 = num7 | num8; byte[] bytes = new byte[length2]; Array.Copy((Array)buffer, sourceIndex, (Array)bytes, 0, length2); int num9 = checked (sourceIndex + length2); stringList.Add(new string(Encoding.UTF8.GetChars(bytes))); IList <byte> byteList2 = byteList1; byte[] numArray5 = buffer; int index5 = num9; num4 = checked (index5 + 1); int num10 = (int)numArray5[index5]; byteList2.Add((byte)num10); }while (num4 < length1); mqttMsgSubscribe1.topics = new string[stringList.Count]; mqttMsgSubscribe1.qosLevels = new byte[byteList1.Count]; int index6 = 0; while (index6 < stringList.Count) { mqttMsgSubscribe1.topics[index6] = stringList[index6]; mqttMsgSubscribe1.qosLevels[index6] = byteList1[index6]; checked { ++index6; } } return(mqttMsgSubscribe1); }
/// <summary> /// Parse bytes for a UNSUBSCRIBE message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>UNSUBSCRIBE message instance</returns> public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; byte[] topicUtf8; int topicUtf8Length; MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBSCRIBE_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... int received = channel.Receive(buffer); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1) { // only 3.1.0 // read QoS level from fixed header msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET); // read DUP flag from fixed header msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01); // retain flag not used msg.retain = false; } // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); // payload contains topics // NOTE : before, I don't know how many topics will be in the payload (so use List) // if .Net Micro Framework #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_V1_0) IList tmpTopics = new ArrayList(); // else other frameworks (.Net, .Net Compact, Mono, Windows Phone) #else IList <String> tmpTopics = new List <String>(); #endif do { // topic name topicUtf8Length = ((buffer[index++] << 8) & 0xFF00); topicUtf8Length |= buffer[index++]; topicUtf8 = new byte[topicUtf8Length]; Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length); index += topicUtf8Length; tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8))); } while (index < remainingLength); // copy from list to array msg.topics = new string[tmpTopics.Count]; for (int i = 0; i < tmpTopics.Count; i++) { msg.topics[i] = (string)tmpTopics[i]; } return(msg); }
/// <summary> /// Parse bytes for a SUBSCRIBE message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>SUBSCRIBE message instance</returns> public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; byte[] topicUtf8; int topicUtf8Length; MqttMsgSubscribe msg = new MqttMsgSubscribe(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... int received = channel.Receive(buffer); // read QoS level from fixed header msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET); // read DUP flag from fixed header msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01); // retain flag not used msg.retain = false; // message id msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId |= (buffer[index++]); // payload contains topics and QoS levels // NOTE : before, I don't know how many topics will be in the payload (so use List) // if .Net Micro Framework #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3) IList tmpTopics = new ArrayList(); IList tmpQosLevels = new ArrayList(); // else other frameworks (.Net, .Net Compact, Mono, Windows Phone) #else IList <String> tmpTopics = new List <String>(); IList <byte> tmpQosLevels = new List <byte>(); #endif do { // topic name topicUtf8Length = ((buffer[index++] << 8) & 0xFF00); topicUtf8Length |= buffer[index++]; topicUtf8 = new byte[topicUtf8Length]; Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length); index += topicUtf8Length; tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8))); // QoS level tmpQosLevels.Add(buffer[index++]); } while (index < remainingLength); // copy from list to array msg.topics = new string[tmpTopics.Count]; msg.qosLevels = new byte[tmpQosLevels.Count]; for (int i = 0; i < tmpTopics.Count; i++) { msg.topics[i] = (string)tmpTopics[i]; msg.qosLevels[i] = (byte)tmpQosLevels[i]; } return(msg); }
/// <summary> /// Parse bytes for a CONNECT message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>CONNECT message instance</returns> public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; int protNameUtf8Length; byte[] protNameUtf8; bool isUsernameFlag; bool isPasswordFlag; int clientIdUtf8Length; byte[] clientIdUtf8; int willTopicUtf8Length; byte[] willTopicUtf8; int willMessageUtf8Length; byte[] willMessageUtf8; int usernameUtf8Length; byte[] usernameUtf8; int passwordUtf8Length; byte[] passwordUtf8; MqttMsgConnect msg = new MqttMsgConnect(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); // protocol name protNameUtf8Length = ((buffer[index++] << 8) & 0xFF00); protNameUtf8Length |= buffer[index++]; protNameUtf8 = new byte[protNameUtf8Length]; Array.Copy(buffer, index, protNameUtf8, 0, protNameUtf8Length); index += protNameUtf8Length; msg.protocolName = new String(Encoding.UTF8.GetChars(protNameUtf8)); // protocol version msg.protocolVersion = buffer[index]; index += PROTOCOL_VERSION_NUMBER_SIZE; // connect flags isUsernameFlag = (buffer[index] & USERNAME_FLAG_MASK) != 0x00; isPasswordFlag = (buffer[index] & PASSWORD_FLAG_MASK) != 0x00; msg.willRetain = (buffer[index] & WILL_RETAIN_FLAG_MASK) != 0x00; msg.willQosLevel = (byte)((buffer[index] & WILL_QOS_FLAG_MASK) >> WILL_QOS_FLAG_OFFSET); msg.willFlag = (buffer[index] & WILL_FLAG_MASK) != 0x00; msg.cleanSession = (buffer[index] & CLEAN_SESSION_FLAG_MASK) != 0x00; index += CONNECT_FLAGS_SIZE; // keep alive timer msg.keepAlivePeriod = (ushort)((buffer[index++] << 8) & 0xFF00); msg.keepAlivePeriod |= buffer[index++]; // client identifier clientIdUtf8Length = ((buffer[index++] << 8) & 0xFF00); clientIdUtf8Length |= buffer[index++]; clientIdUtf8 = new byte[clientIdUtf8Length]; Array.Copy(buffer, index, clientIdUtf8, 0, clientIdUtf8Length); index += clientIdUtf8Length; msg.clientId = new String(Encoding.UTF8.GetChars(clientIdUtf8)); // will topic and will message if (msg.willFlag) { willTopicUtf8Length = ((buffer[index++] << 8) & 0xFF00); willTopicUtf8Length |= buffer[index++]; willTopicUtf8 = new byte[willTopicUtf8Length]; Array.Copy(buffer, index, willTopicUtf8, 0, willTopicUtf8Length); index += willTopicUtf8Length; msg.willTopic = new String(Encoding.UTF8.GetChars(willTopicUtf8)); willMessageUtf8Length = ((buffer[index++] << 8) & 0xFF00); willMessageUtf8Length |= buffer[index++]; willMessageUtf8 = new byte[willMessageUtf8Length]; Array.Copy(buffer, index, willMessageUtf8, 0, willMessageUtf8Length); index += willMessageUtf8Length; msg.willMessage = new String(Encoding.UTF8.GetChars(willMessageUtf8)); } // username if (isUsernameFlag) { usernameUtf8Length = ((buffer[index++] << 8) & 0xFF00); usernameUtf8Length |= buffer[index++]; usernameUtf8 = new byte[usernameUtf8Length]; Array.Copy(buffer, index, usernameUtf8, 0, usernameUtf8Length); index += usernameUtf8Length; msg.username = new String(Encoding.UTF8.GetChars(usernameUtf8)); } // password if (isPasswordFlag) { passwordUtf8Length = ((buffer[index++] << 8) & 0xFF00); passwordUtf8Length |= buffer[index++]; passwordUtf8 = new byte[passwordUtf8Length]; Array.Copy(buffer, index, passwordUtf8, 0, passwordUtf8Length); index += passwordUtf8Length; msg.password = new String(Encoding.UTF8.GetChars(passwordUtf8)); } return(msg); }
/// <summary> /// Parse bytes for a CONNECT message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>CONNECT message instance</returns> public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; int protNameUtf8Length; byte[] protNameUtf8; bool isUsernameFlag; bool isPasswordFlag; int clientIdUtf8Length; byte[] clientIdUtf8; int willTopicUtf8Length; byte[] willTopicUtf8; int willMessageUtf8Length; byte[] willMessageUtf8; int usernameUtf8Length; byte[] usernameUtf8; int passwordUtf8Length; byte[] passwordUtf8; MqttMsgConnect msg = new MqttMsgConnect(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); // protocol name protNameUtf8Length = ((buffer[index++] << 8) & 0xFF00); protNameUtf8Length |= buffer[index++]; protNameUtf8 = new byte[protNameUtf8Length]; Array.Copy(buffer, index, protNameUtf8, 0, protNameUtf8Length); index += protNameUtf8Length; msg.protocolName = new String(Encoding.UTF8.GetChars(protNameUtf8)); // [v3.1.1] wrong protocol name if (!msg.protocolName.Equals(PROTOCOL_NAME_V3_1) && !msg.protocolName.Equals(PROTOCOL_NAME_V3_1_1)) { throw new MqttClientException(MqttClientErrorCode.InvalidProtocolName); } // protocol version msg.protocolVersion = buffer[index]; index += PROTOCOL_VERSION_SIZE; // connect flags // [v3.1.1] check lsb (reserved) must be 0 if ((msg.protocolVersion == PROTOCOL_VERSION_V3_1_1) && ((buffer[index] & RESERVED_FLAG_MASK) != 0x00)) { throw new MqttClientException(MqttClientErrorCode.InvalidConnectFlags); } isUsernameFlag = (buffer[index] & USERNAME_FLAG_MASK) != 0x00; isPasswordFlag = (buffer[index] & PASSWORD_FLAG_MASK) != 0x00; msg.willRetain = (buffer[index] & WILL_RETAIN_FLAG_MASK) != 0x00; msg.willQosLevel = (byte)((buffer[index] & WILL_QOS_FLAG_MASK) >> WILL_QOS_FLAG_OFFSET); msg.willFlag = (buffer[index] & WILL_FLAG_MASK) != 0x00; msg.cleanSession = (buffer[index] & CLEAN_SESSION_FLAG_MASK) != 0x00; index += CONNECT_FLAGS_SIZE; // keep alive timer msg.keepAlivePeriod = (ushort)((buffer[index++] << 8) & 0xFF00); msg.keepAlivePeriod |= buffer[index++]; // client identifier [v3.1.1] it may be zero bytes long (empty string) clientIdUtf8Length = ((buffer[index++] << 8) & 0xFF00); clientIdUtf8Length |= buffer[index++]; clientIdUtf8 = new byte[clientIdUtf8Length]; Array.Copy(buffer, index, clientIdUtf8, 0, clientIdUtf8Length); index += clientIdUtf8Length; msg.clientId = new String(Encoding.UTF8.GetChars(clientIdUtf8)); // [v3.1.1] if client identifier is zero bytes long, clean session must be true if ((msg.protocolVersion == PROTOCOL_VERSION_V3_1_1) && (clientIdUtf8Length == 0) && (!msg.cleanSession)) { throw new MqttClientException(MqttClientErrorCode.InvalidClientId); } // will topic and will message if (msg.willFlag) { willTopicUtf8Length = ((buffer[index++] << 8) & 0xFF00); willTopicUtf8Length |= buffer[index++]; willTopicUtf8 = new byte[willTopicUtf8Length]; Array.Copy(buffer, index, willTopicUtf8, 0, willTopicUtf8Length); index += willTopicUtf8Length; msg.willTopic = new String(Encoding.UTF8.GetChars(willTopicUtf8)); willMessageUtf8Length = ((buffer[index++] << 8) & 0xFF00); willMessageUtf8Length |= buffer[index++]; willMessageUtf8 = new byte[willMessageUtf8Length]; Array.Copy(buffer, index, willMessageUtf8, 0, willMessageUtf8Length); index += willMessageUtf8Length; msg.willMessage = new String(Encoding.UTF8.GetChars(willMessageUtf8)); } // username if (isUsernameFlag) { usernameUtf8Length = ((buffer[index++] << 8) & 0xFF00); usernameUtf8Length |= buffer[index++]; usernameUtf8 = new byte[usernameUtf8Length]; Array.Copy(buffer, index, usernameUtf8, 0, usernameUtf8Length); index += usernameUtf8Length; msg.username = new String(Encoding.UTF8.GetChars(usernameUtf8)); } // password if (isPasswordFlag) { passwordUtf8Length = ((buffer[index++] << 8) & 0xFF00); passwordUtf8Length |= buffer[index++]; passwordUtf8 = new byte[passwordUtf8Length]; Array.Copy(buffer, index, passwordUtf8, 0, passwordUtf8Length); index += passwordUtf8Length; msg.password = new String(Encoding.UTF8.GetChars(passwordUtf8)); } return(msg); }
public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { int num1 = 0; MqttMsgConnect mqttMsgConnect1 = new MqttMsgConnect(); byte[] buffer = new byte[MqttMsgBase.decodeRemainingLength(channel)]; channel.Receive(buffer); byte[] numArray1 = buffer; int index1 = num1; int num2 = checked (index1 + 1); int num3 = (int)numArray1[index1] << 8 & 65280; byte[] numArray2 = buffer; int index2 = num2; int sourceIndex1 = checked (index2 + 1); int num4 = (int)numArray2[index2]; int length1 = num3 | num4; byte[] bytes1 = new byte[length1]; Array.Copy((Array)buffer, sourceIndex1, (Array)bytes1, 0, length1); int index3 = checked (sourceIndex1 + length1); mqttMsgConnect1.protocolName = new string(Encoding.UTF8.GetChars(bytes1)); if (!mqttMsgConnect1.protocolName.Equals("MQIsdp") && !mqttMsgConnect1.protocolName.Equals("MQTT")) { throw new MqttClientException(MqttClientErrorCode.InvalidProtocolName); } mqttMsgConnect1.protocolVersion = buffer[index3]; int index4 = checked (index3 + 1); if (mqttMsgConnect1.protocolVersion == (byte)4 && ((uint)buffer[index4] & 1U) > 0U) { throw new MqttClientException(MqttClientErrorCode.InvalidConnectFlags); } bool flag1 = ((uint)buffer[index4] & 128U) > 0U; bool flag2 = ((uint)buffer[index4] & 64U) > 0U; mqttMsgConnect1.willRetain = ((uint)buffer[index4] & 32U) > 0U; mqttMsgConnect1.willQosLevel = checked ((byte)(((int)buffer[index4] & 24) >> 3)); mqttMsgConnect1.willFlag = ((uint)buffer[index4] & 4U) > 0U; mqttMsgConnect1.cleanSession = ((uint)buffer[index4] & 2U) > 0U; int num5 = checked (index4 + 1); MqttMsgConnect mqttMsgConnect2 = mqttMsgConnect1; byte[] numArray3 = buffer; int index5 = num5; int num6 = checked (index5 + 1); int num7 = (int)checked ((ushort)((int)numArray3[index5] << 8 & 65280)); mqttMsgConnect2.keepAlivePeriod = (ushort)num7; MqttMsgConnect mqttMsgConnect3 = mqttMsgConnect1; int keepAlivePeriod = (int)mqttMsgConnect3.keepAlivePeriod; byte[] numArray4 = buffer; int index6 = num6; int num8 = checked (index6 + 1); int num9 = (int)numArray4[index6]; mqttMsgConnect3.keepAlivePeriod = (ushort)(keepAlivePeriod | num9); byte[] numArray5 = buffer; int index7 = num8; int num10 = checked (index7 + 1); int num11 = (int)numArray5[index7] << 8 & 65280; byte[] numArray6 = buffer; int index8 = num10; int sourceIndex2 = checked (index8 + 1); int num12 = (int)numArray6[index8]; int length2 = num11 | num12; byte[] bytes2 = new byte[length2]; Array.Copy((Array)buffer, sourceIndex2, (Array)bytes2, 0, length2); int num13 = checked (sourceIndex2 + length2); mqttMsgConnect1.clientId = new string(Encoding.UTF8.GetChars(bytes2)); if (mqttMsgConnect1.protocolVersion == (byte)4 && length2 == 0 && !mqttMsgConnect1.cleanSession) { throw new MqttClientException(MqttClientErrorCode.InvalidClientId); } if (mqttMsgConnect1.willFlag) { byte[] numArray7 = buffer; int index9 = num13; int num14 = checked (index9 + 1); int num15 = (int)numArray7[index9] << 8 & 65280; byte[] numArray8 = buffer; int index10 = num14; int sourceIndex3 = checked (index10 + 1); int num16 = (int)numArray8[index10]; int length3 = num15 | num16; byte[] bytes3 = new byte[length3]; Array.Copy((Array)buffer, sourceIndex3, (Array)bytes3, 0, length3); int num17 = checked (sourceIndex3 + length3); mqttMsgConnect1.willTopic = new string(Encoding.UTF8.GetChars(bytes3)); byte[] numArray9 = buffer; int index11 = num17; int num18 = checked (index11 + 1); int num19 = (int)numArray9[index11] << 8 & 65280; byte[] numArray10 = buffer; int index12 = num18; int sourceIndex4 = checked (index12 + 1); int num20 = (int)numArray10[index12]; int length4 = num19 | num20; byte[] bytes4 = new byte[length4]; Array.Copy((Array)buffer, sourceIndex4, (Array)bytes4, 0, length4); num13 = checked (sourceIndex4 + length4); mqttMsgConnect1.willMessage = new string(Encoding.UTF8.GetChars(bytes4)); } if (flag1) { byte[] numArray7 = buffer; int index9 = num13; int num14 = checked (index9 + 1); int num15 = (int)numArray7[index9] << 8 & 65280; byte[] numArray8 = buffer; int index10 = num14; int sourceIndex3 = checked (index10 + 1); int num16 = (int)numArray8[index10]; int length3 = num15 | num16; byte[] bytes3 = new byte[length3]; Array.Copy((Array)buffer, sourceIndex3, (Array)bytes3, 0, length3); num13 = checked (sourceIndex3 + length3); mqttMsgConnect1.username = new string(Encoding.UTF8.GetChars(bytes3)); } if (flag2) { byte[] numArray7 = buffer; int index9 = num13; int num14 = checked (index9 + 1); int num15 = (int)numArray7[index9] << 8 & 65280; byte[] numArray8 = buffer; int index10 = num14; int sourceIndex3 = checked (index10 + 1); int num16 = (int)numArray8[index10]; int length3 = num15 | num16; byte[] bytes3 = new byte[length3]; Array.Copy((Array)buffer, sourceIndex3, (Array)bytes3, 0, length3); int num17 = checked (sourceIndex3 + length3); mqttMsgConnect1.password = new string(Encoding.UTF8.GetChars(bytes3)); } return(mqttMsgConnect1); }
public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { int num1 = 0; MqttMsgPublish mqttMsgPublish1 = new MqttMsgPublish(); int length1 = MqttMsgBase.decodeRemainingLength(channel); byte[] buffer = new byte[length1]; int num2 = channel.Receive(buffer); byte[] numArray1 = buffer; int index1 = num1; int num3 = checked (index1 + 1); int num4 = (int)numArray1[index1] << 8 & 65280; byte[] numArray2 = buffer; int index2 = num3; int sourceIndex1 = checked (index2 + 1); int num5 = (int)numArray2[index2]; int length2 = num4 | num5; byte[] bytes = new byte[length2]; Array.Copy((Array)buffer, sourceIndex1, (Array)bytes, 0, length2); int sourceIndex2 = checked (sourceIndex1 + length2); mqttMsgPublish1.topic = new string(Encoding.UTF8.GetChars(bytes)); mqttMsgPublish1.qosLevel = checked ((byte)(((int)fixedHeaderFirstByte & 6) >> 1)); if (mqttMsgPublish1.qosLevel > (byte)2) { throw new MqttClientException(MqttClientErrorCode.QosNotAllowed); } mqttMsgPublish1.dupFlag = ((int)fixedHeaderFirstByte & 8) >> 3 == 1; mqttMsgPublish1.retain = ((int)fixedHeaderFirstByte & 1) == 1; if (mqttMsgPublish1.qosLevel == (byte)1 || mqttMsgPublish1.qosLevel == (byte)2) { MqttMsgPublish mqttMsgPublish2 = mqttMsgPublish1; byte[] numArray3 = buffer; int index3 = sourceIndex2; int num6 = checked (index3 + 1); int num7 = (int)checked ((ushort)((int)numArray3[index3] << 8 & 65280)); mqttMsgPublish2.messageId = (ushort)num7; MqttMsgPublish mqttMsgPublish3 = mqttMsgPublish1; int messageId = (int)mqttMsgPublish3.messageId; byte[] numArray4 = buffer; int index4 = num6; sourceIndex2 = checked (index4 + 1); int num8 = (int)numArray4[index4]; mqttMsgPublish3.messageId = (ushort)(messageId | num8); } int length3 = checked (length1 - sourceIndex2); int num9 = length3; int destinationIndex1 = 0; mqttMsgPublish1.message = new byte[length3]; Array.Copy((Array)buffer, sourceIndex2, (Array)mqttMsgPublish1.message, destinationIndex1, checked (num2 - sourceIndex2)); int num10 = checked (num9 - num2 - sourceIndex2); int destinationIndex2 = checked (destinationIndex1 + num2 - sourceIndex2); while (num10 > 0) { int length4 = channel.Receive(buffer); Array.Copy((Array)buffer, 0, (Array)mqttMsgPublish1.message, destinationIndex2, length4); checked { num10 -= length4; } checked { destinationIndex2 += length4; } } return(mqttMsgPublish1); }