/// <summary> /// Parse bytes for a UNSUBSCRIBE message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>UNSUBSCRIBE message instance</returns> public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; int index = 0; byte[] topicUtf8; int topicUtf8Length; MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... //int received = channel.Receive(buffer); 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 // 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(); // 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> /// Unsubscribe for message topics /// </summary> /// <param name="topics">List of topics to unsubscribe</param> /// <returns>Message Id in UNSUBACK message from broker</returns> public ushort Unsubscribe(string[] topics) { int attempts = 0; bool acknowledged = false; MqttMsgUnsubscribe unsubscribe = new MqttMsgUnsubscribe(topics); MqttMsgUnsuback unsuback = null; do { try { // try unsubscribe unsuback = (MqttMsgUnsuback)this.SendReceive(unsubscribe.GetBytes()); acknowledged = true; } catch (MqttTimeoutException) { // no UNSUBACK message received in time, retry with duplicate flag attempts++; unsubscribe.DupFlag = true; // delay before retry if (attempts < MQTT_ATTEMPTS_RETRY) Thread.Sleep(MQTT_DELAY_RETRY); } } while ((attempts < MQTT_ATTEMPTS_RETRY) && !acknowledged); // return message id from SUBACK or zero (no message id) return acknowledged ? unsuback.MessageId : (ushort)0; }
/// <summary> /// Unsubscribe for message topics /// </summary> /// <param name="topics">List of topics to unsubscribe</param> /// <returns>Message Id in UNSUBACK message from broker</returns> public ushort Unsubscribe(string[] topics) { MqttMsgUnsubscribe unsubscribe = new MqttMsgUnsubscribe(topics); unsubscribe.MessageId = this.GetMessageId(); // enqueue unsubscribe request into the inflight queue this.EnqueueInflight(unsubscribe, MqttMsgFlow.ToPublish); return unsubscribe.MessageId; }
/// <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) 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; }
public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { int num1 = 0; MqttMsgUnsubscribe mqttMsgUnsubscribe1 = new MqttMsgUnsubscribe(); 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) { mqttMsgUnsubscribe1.qosLevel = checked ((byte)(((int)fixedHeaderFirstByte & 6) >> 1)); mqttMsgUnsubscribe1.dupFlag = ((int)fixedHeaderFirstByte & 8) >> 3 == 1; mqttMsgUnsubscribe1.retain = false; } MqttMsgUnsubscribe mqttMsgUnsubscribe2 = mqttMsgUnsubscribe1; byte[] numArray1 = buffer; int index1 = num1; int num2 = checked (index1 + 1); int num3 = (int)checked ((ushort)((int)numArray1[index1] << 8 & 65280)); mqttMsgUnsubscribe2.messageId = (ushort)num3; MqttMsgUnsubscribe mqttMsgUnsubscribe3 = mqttMsgUnsubscribe1; int messageId = (int)mqttMsgUnsubscribe3.messageId; byte[] numArray2 = buffer; int index2 = num2; int num4 = checked (index2 + 1); int num5 = (int)numArray2[index2]; mqttMsgUnsubscribe3.messageId = (ushort)(messageId | num5); IList <string> stringList = (IList <string>) new List <string>(); 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); num4 = checked (sourceIndex + length2); stringList.Add(new string(Encoding.UTF8.GetChars(bytes))); }while (num4 < length1); mqttMsgUnsubscribe1.topics = new string[stringList.Count]; int index5 = 0; while (index5 < stringList.Count) { mqttMsgUnsubscribe1.topics[index5] = stringList[index5]; checked { ++index5; } } return(mqttMsgUnsubscribe1); }