/// <summary>
 /// Constructor
 /// </summary>
 /// <param name="disconnect">DISCONNECT message received from client</param>
 public ConnectionClosedRequestEventArgs(MqttMsgDisconnect disconnect)
 {
     Message = disconnect;
 }
Пример #2
0
        /// <summary>
        /// Parse bytes for a DISCONNECT message
        /// </summary>
        /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
        /// <param name="protocolVersion">MQTT Protocol Version</param>
        /// <param name="channel">Channel connected to the broker</param>
        /// <returns>DISCONNECT message instance</returns>
        public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, MqttProtocolVersion protocolVersion, IMqttNetworkChannel channel)
        {
            byte[]            buffer;
            int               index = 0;
            MqttMsgDisconnect msg   = new MqttMsgDisconnect();

            if (((protocolVersion == MqttProtocolVersion.Version_3_1_1) || (protocolVersion == MqttProtocolVersion.Version_5)) &&
                ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_DISCONNECT_FLAG_BITS))
            {
                throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
            }

            // get remaining length and allocate buffer
            int remainingLength = DecodeVariableByte(channel);

            // NOTE : remainingLength must be 0
            if ((remainingLength != 0) && (protocolVersion == MqttProtocolVersion.Version_5))
            {
                // V5.0 specific
                buffer = new byte[remainingLength];

                // read bytes from socket...
                channel.Receive(buffer);
                msg.ResonCode = (MqttReasonCode)buffer[index++];
                int propSize = EncodeDecodeHelper.GetPropertySize(buffer, ref index);
                propSize += index;
                MqttProperty prop;

                while (propSize > index)
                {
                    prop = (MqttProperty)buffer[index++];
                    switch (prop)
                    {
                    case MqttProperty.SessionExpiryInterval:
                        // 4 bytes
                        msg.SessionExpiryInterval = EncodeDecodeHelper.DecodeUint(buffer, ref index);
                        break;

                    case MqttProperty.ReasonString:
                        // UTF8 encoded
                        msg.Reason = EncodeDecodeHelper.GetUTF8FromBuffer(buffer, ref index);
                        break;

                    case MqttProperty.UserProperty:
                        // UTF8 key value encoding, so 2 strings in a raw
                        string key   = EncodeDecodeHelper.GetUTF8FromBuffer(buffer, ref index);
                        string value = EncodeDecodeHelper.GetUTF8FromBuffer(buffer, ref index);
                        msg.UserProperties.Add(new UserProperty(key, value));
                        break;

                    case MqttProperty.ServerReference:
                        // UTF8 encoded
                        msg.ServerReference = EncodeDecodeHelper.GetUTF8FromBuffer(buffer, ref index);
                        break;

                    default:
                        // non supported property
                        index = propSize;
                        break;
                    }
                }
            }

            return(msg);
        }