Exemple #1
0
        private async Task ContentReceived(MqttContent Content)
        {
            ContentReceivedEventHandler h = this.OnContentReceived;

            if (h != null)
            {
                try
                {
                    await h(this, Content);
                }
                catch (Exception ex)
                {
                    this.Exception(ex);
                    Log.Critical(ex);
                }
            }
        }
Exemple #2
0
        private async Task <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)
                    {
                        await this.ConnectionError(this, ex);

                        this.DisposeClient();
                        return(false);
                    }
                    break;

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

                    break;

                case MqttControlPacketType.PINGRESP:
                    EventHandlerAsync h = this.OnPingResponse;
                    if (h != null)
                    {
                        try
                        {
                            await h(this, new EventArgs());
                        }
                        catch (Exception ex)
                        {
                            this.Exception(ex);
                            Log.Critical(ex);
                        }
                    }
                    break;

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

                    if (Header.QualityOfService > MqttQualityOfService.AtMostOnce)
                    {
                        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.AtMostOnce:
                        await this.ContentReceived(Content);

                        break;

                    case MqttQualityOfService.AtLeastOnce:
                        await this.PUBACK(Header.PacketIdentifier);

                        await this.ContentReceived(Content);

                        break;

                    case MqttQualityOfService.ExactlyOnce:
                        lock (this.synchObj)
                        {
                            this.contentCache[Header.PacketIdentifier] = Content;
                        }
                        await this.PUBREC(Header.PacketIdentifier);

                        break;
                    }
                    break;

                case MqttControlPacketType.PUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    PacketAcknowledgedEventHandler h2 = this.OnPublished;
                    if (h2 != null)
                    {
                        try
                        {
                            await h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
                            this.Exception(ex);
                        }
                    }
                    break;

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

                    break;

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

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

                case MqttControlPacketType.PUBCOMP:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnPublished;
                    if (h2 != null)
                    {
                        try
                        {
                            await h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
                            this.Exception(ex);
                        }
                    }
                    break;

                case MqttControlPacketType.SUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnSubscribed;
                    if (h2 != null)
                    {
                        try
                        {
                            await h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
                            this.Exception(ex);
                        }
                    }
                    break;

                case MqttControlPacketType.UNSUBACK:
                    this.PacketDelivered(Header.PacketIdentifier);
                    h2 = this.OnUnsubscribed;
                    if (h2 != null)
                    {
                        try
                        {
                            await h2(this, Header.PacketIdentifier);
                        }
                        catch (Exception ex)
                        {
                            this.Exception(ex);
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                await this.Error(ex);
            }

            return(true);
        }