Пример #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="connact">CONNECT message received from client</param>
 public ConnectionOpenedEventArgs(MqttMsgConnack connact)
 {
     Message = connact;
 }
Пример #2
0
        /// <summary>
        /// Parse bytes for a CONNACK 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>CONNACK message instance</returns>
        public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, MqttProtocolVersion protocolVersion, IMqttNetworkChannel channel)
        {
            byte[]         buffer;
            int            index = 0;
            MqttMsgConnack msg   = new MqttMsgConnack();

            if ((protocolVersion == MqttProtocolVersion.Version_3_1_1) || (protocolVersion == MqttProtocolVersion.Version_5))
            {
                // [v3.1.1] check flag bits
                if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS)
                {
                    throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
                }
            }

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

            buffer = new byte[remainingLength];

            // read bytes from socket...
            channel.Receive(buffer);
            if ((protocolVersion == MqttProtocolVersion.Version_3_1_1) || (protocolVersion == MqttProtocolVersion.Version_5))
            {
                // [v3.1.1] ... set session present flag ...
                msg.SessionPresent = (buffer[index++] & SESSION_PRESENT_FLAG_MASK) != 0x00;
            }
            // ...and set return code from broker
            msg.ReturnCode = (MqttReasonCode)buffer[index++];

            // v5.0 properties
            if (protocolVersion == MqttProtocolVersion.Version_5)
            {
                // size of the properties
                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.ReceiveMaximum:
                        // 2 bytes
                        msg.ReceiveMaximum  = (ushort)(buffer[index++] << 8);
                        msg.ReceiveMaximum |= buffer[index++];
                        break;

                    case MqttProperty.MaximumQoS:
                        msg.MaximumQoS = buffer[index++] != 0;
                        break;

                    case MqttProperty.RetainAvailable:
                        msg.RetainAvailable = buffer[index++] != 0;
                        break;

                    case MqttProperty.MaximumPacketSize:
                        // 4 bytes
                        msg.MaximumPacketSize = EncodeDecodeHelper.DecodeUint(buffer, ref index);
                        break;

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

                    case MqttProperty.TopicAliasMaximum:
                        // 2 bytes
                        msg.TopicAliasMaximum  = (ushort)(buffer[index++] << 8);
                        msg.TopicAliasMaximum |= buffer[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.WildcardSubscriptionAvailable:
                        msg.WildcardSubscriptionAvailable = buffer[index++] != 0;
                        break;

                    case MqttProperty.SubscriptionIdentifierAvailable:
                        msg.SubscriptionIdentifiersAvailable = buffer[index++] != 0;
                        break;

                    case MqttProperty.SharedSubscriptionAvailable:
                        msg.SharedSubscriptionAvailable = buffer[index++] != 0;
                        break;

                    case MqttProperty.ServerKeepAlive:
                        // 2 bytes
                        msg.ServerKeepAlive  = (ushort)(buffer[index++] << 8);
                        msg.ServerKeepAlive |= buffer[index++];
                        break;

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

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

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

                    case MqttProperty.AuthenticationData:
                        // byte[]
                        int length = ((buffer[index++] << 8) & 0xFF00);
                        length |= buffer[index++];
                        msg.AuthenticationData = new byte[length];
                        Array.Copy(buffer, index, msg.AuthenticationData, 0, length);
                        index += length;
                        break;

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

            return(msg);
        }