Exemplo n.º 1
0
#pragma warning enable

		/// <summary>
		/// Parses a header.
		/// </summary>
		/// <param name="Input">Binary input.</param>
		/// <returns>Header.</returns>
		public static MqttHeader Parse(BinaryInput Input)
		{
			MqttHeader Result = new MqttHeader();

			// Fixed part:

			byte b = Input.ReadByte();

			Result.ControlPacketType = (MqttControlPacketType)(b >> 4);
			Result.Retain = (b & 1) != 0;
			Result.QualityOfService = (MqttQualityOfService)((b >> 1) & 3);
			Result.Duplicate = (b & 8) != 0;

			b = Input.ReadByte();

			int Offset = 0;

			Result.RemainingLength = b & 127;
			while ((b & 128) != 0)
			{
				b = Input.ReadByte();
				Offset += 7;
				Result.RemainingLength |= (b & 127) << Offset;
			}

			// Variable part:

			switch (Result.ControlPacketType)
			{
				case MqttControlPacketType.CONNECT:
				case MqttControlPacketType.CONNACK:
				case MqttControlPacketType.PINGREQ:
				case MqttControlPacketType.PINGRESP:
				case MqttControlPacketType.DISCONNECT:
				case MqttControlPacketType.PUBLISH:
				default:
					Result.PacketIdentifier = 0;
					break;

				case MqttControlPacketType.PUBACK:
				case MqttControlPacketType.PUBREC:
				case MqttControlPacketType.PUBREL:
				case MqttControlPacketType.PUBCOMP:
				case MqttControlPacketType.SUBSCRIBE:
				case MqttControlPacketType.SUBACK:
				case MqttControlPacketType.UNSUBSCRIBE:
				case MqttControlPacketType.UNSUBACK:
					Result.PacketIdentifier = Input.ReadByte();
					Result.PacketIdentifier <<= 8;
					Result.PacketIdentifier |= Input.ReadByte();

					Result.RemainingLength -= 2;
					break;
			}

			return Result;
		}
Exemplo n.º 2
0
#pragma warning enable

        /// <summary>
        /// Parses a header.
        /// </summary>
        /// <param name="Input">Binary input.</param>
        /// <returns>Header.</returns>
        public static MqttHeader Parse(BinaryInput Input)
        {
            MqttHeader Result = new MqttHeader();

            // Fixed part:

            byte b = Input.ReadByte();

            Result.ControlPacketType = (MqttControlPacketType)(b >> 4);
            Result.Retain            = (b & 1) != 0;
            Result.QualityOfService  = (MqttQualityOfService)((b >> 1) & 3);
            Result.Duplicate         = (b & 8) != 0;

            b = Input.ReadByte();

            int Offset = 0;

            Result.RemainingLength = b & 127;
            while ((b & 128) != 0)
            {
                b       = Input.ReadByte();
                Offset += 7;
                Result.RemainingLength |= (b & 127) << Offset;
            }

            // Variable part:

            switch (Result.ControlPacketType)
            {
            case MqttControlPacketType.CONNECT:
            case MqttControlPacketType.CONNACK:
            case MqttControlPacketType.PINGREQ:
            case MqttControlPacketType.PINGRESP:
            case MqttControlPacketType.DISCONNECT:
            case MqttControlPacketType.PUBLISH:
            default:
                Result.PacketIdentifier = 0;
                break;

            case MqttControlPacketType.PUBACK:
            case MqttControlPacketType.PUBREC:
            case MqttControlPacketType.PUBREL:
            case MqttControlPacketType.PUBCOMP:
            case MqttControlPacketType.SUBSCRIBE:
            case MqttControlPacketType.SUBACK:
            case MqttControlPacketType.UNSUBSCRIBE:
            case MqttControlPacketType.UNSUBACK:
                Result.PacketIdentifier   = Input.ReadByte();
                Result.PacketIdentifier <<= 8;
                Result.PacketIdentifier  |= Input.ReadByte();

                Result.RemainingLength -= 2;
                break;
            }

            return(Result);
        }
Exemplo n.º 3
0
 internal MqttContent(MqttHeader Header, string Topic, byte[] Data)
 {
     this.header = Header;
     this.topic  = Topic;
     this.data   = Data;
 }
Exemplo n.º 4
0
        private bool ProcessInputPacket()
        {
            try
            {
                BinaryInput Packet = new BinaryInput(this.inputPacket);
                MqttHeader  Header = MqttHeader.Parse(Packet);

                switch (Header.ControlPacketType)
                {
                case MqttControlPacketType.CONNECT:
                default:
                    throw new Exception("Received command from server that is not handled: " + Header.ControlPacketType.ToString());

                case MqttControlPacketType.CONNACK:
                    bool SessionPresent = (Packet.ReadByte() & 1) != 0;
                    byte ReturnCode     = Packet.ReadByte();

                    try
                    {
                        switch (ReturnCode)
                        {
                        case 0:
                            this.State    = MqttState.Connected;
                            this.nextPing = DateTime.Now.AddMilliseconds(this.keepAliveSeconds * 500);
                            break;

                        case 1:
                            throw new IOException("Connection Refused, unacceptable protocol version.");

                        case 2:
                            throw new IOException("Connection Refused, identifier rejected.");

                        case 3:
                            throw new IOException("Connection Refused, Server unavailable.");

                        case 4:
                            throw new IOException("Connection Refused, bad user name or password.");

                        case 5:
                            throw new IOException("Connection Refused, not authorized.");

                        default:
                            throw new IOException("Unrecognized error code returned: " + ReturnCode.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        this.ConnectionError(ex);
                        this.stream.Close();
                        this.client.Close();
                        return(false);
                    }
                    break;

                case MqttControlPacketType.PINGREQ:
                    this.PINGRESP();
                    break;

                case MqttControlPacketType.PINGRESP:
                    EventHandler h = this.OnPingResponse;
                    if (!(h is null))
                    {
                        try
                        {
                            h(this, new EventArgs());
                        }
                        catch (Exception ex)
                        {
#if LineListener
                            LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                        }
                    }
                    break;

                case MqttControlPacketType.PUBLISH:
                    string Topic = Packet.ReadString();

                    if (Header.QualityOfService > MqttQualityOfService.AtMostOne)
                    {
                        Header.PacketIdentifier = Packet.ReadUInt16();
                    }
                    else
                    {
                        Header.PacketIdentifier = 0;
                    }

                    int         c       = Packet.BytesLeft;
                    byte[]      Data    = Packet.ReadBytes(c);
                    MqttContent Content = new MqttContent(Header, Topic, Data);

                    switch (Header.QualityOfService)
                    {
                    case MqttQualityOfService.AtMostOne:
                        this.ContentReceived(Content);
                        break;

                    case MqttQualityOfService.AtLeastOne:
                        this.PUBACK(Header.PacketIdentifier);
                        this.ContentReceived(Content);
                        break;

                    case MqttQualityOfService.ExactlyOne:
                        lock (this.contentCache)
                        {
                            this.contentCache[Header.PacketIdentifier] = Content;
                        }
                        this.PUBREC(Header.PacketIdentifier);
                        break;
                    }
                    break;

                case MqttControlPacketType.PUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    PacketAcknowledgedEventHandler h2 = this.OnPublished;
                    if (!(h2 is null))
                    {
                        try
                        {
                            h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
#if LineListener
                            LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                        }
                    }
                    break;

                case MqttControlPacketType.PUBREC:
                    this.PacketDelivered(Header.PacketIdentifier);
                    this.PUBREL(Header.PacketIdentifier);
                    break;

                case MqttControlPacketType.PUBREL:
                    lock (this.contentCache)
                    {
                        if (this.contentCache.TryGetValue(Header.PacketIdentifier, out Content))
                        {
                            this.contentCache.Remove(Header.PacketIdentifier);
                        }
                        else
                        {
                            Content = null;
                        }
                    }
                    this.PUBCOMP(Header.PacketIdentifier);

                    if (!(Content is null))
                    {
                        this.ContentReceived(Content);
                    }
                    break;

                case MqttControlPacketType.PUBCOMP:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnPublished;
                    if (!(h2 is null))
                    {
                        try
                        {
                            h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
#if LineListener
                            LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                        }
                    }
                    break;

                case MqttControlPacketType.SUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnSubscribed;
                    if (!(h2 is null))
                    {
                        try
                        {
                            h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
#if LineListener
                            LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                        }
                    }
                    break;

                case MqttControlPacketType.UNSUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnUnsubscribed;
                    if (!(h2 is null))
                    {
                        try
                        {
                            h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
#if LineListener
                            LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                this.Error(ex);
            }

            return(true);
        }
Exemplo n.º 5
0
		internal MqttContent(MqttHeader Header, string Topic, byte[] Data)
		{
			this.header = Header;
			this.topic = Topic;
			this.data = Data;
		}