예제 #1
0
        /// <summary>
        /// Sends a PING message to the server. This is automatically done to keep the connection alive. Only call this method
        /// if you want to send additional PING messages, apart from the ones sent to keep the connection alive.
        /// </summary>
        public async Task PING()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PINGREQ << 4);
            Packet.WriteUInt(0);

            byte[] PacketData = Packet.GetPacket();

            await this.Write(PacketData, 0, null);

            EventHandlerAsync h = this.OnPing;

            if (h != null)
            {
                try
                {
                    await h(this, new EventArgs());
                }
                catch (Exception ex)
                {
                    this.Exception(ex);
                    Log.Critical(ex);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Unsubscribes from information earlier subscribed to. Topics can include wildcards.
        /// </summary>
        /// <param name="Topics">Topics</param>
        /// <returns>Packet identifier assigned to unsubscription.</returns>
        public async Task <ushort> UNSUBSCRIBE(params string[] Topics)
        {
            BinaryOutput Payload = new BinaryOutput();
            ushort       PacketIdentifier;

            PacketIdentifier = this.packetIdentifier++;
            if (PacketIdentifier == 0)
            {
                PacketIdentifier = this.packetIdentifier++;
            }

            Payload.WriteUInt16(PacketIdentifier);

            foreach (string Topic in Topics)
            {
                Payload.WriteString(Topic);
            }

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();
            byte         b      = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4);

            b |= 2;

            Packet.WriteByte(b);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            await this.Write(PacketData, PacketIdentifier, null);

            return(PacketIdentifier);
        }
예제 #3
0
        /// <summary>
        /// Unsubscribes from information earlier subscribed to. Topics can include wildcards.
        /// </summary>
        /// <param name="Topics">Topics</param>
        /// <returns>Packet identifier assigned to unsubscription.</returns>
        public async Task <ushort> UNSUBSCRIBE(params string[] Topics)
        {
            BinaryOutput Payload = new BinaryOutput();
            ushort       PacketIdentifier;

            PacketIdentifier = this.packetIdentifier++;
            if (PacketIdentifier == 0)
            {
                PacketIdentifier = this.packetIdentifier++;
            }

            Payload.WriteUInt16(PacketIdentifier);

            foreach (string Topic in Topics)
            {
                Payload.WriteString(Topic);
            }

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();
            byte         b      = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4);

            b |= 2;

            Packet.WriteByte(b);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            if (this.HasSniffers)
            {
                StringBuilder sb    = new StringBuilder();
                bool          First = true;

                sb.Append("UNSUBSCRIBE(");

                foreach (string Topic in Topics)
                {
                    if (First)
                    {
                        First = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }

                    sb.Append(Topic);
                }

                sb.Append(')');

                this.Information(sb.ToString());
            }
            await this.Write(PacketData, PacketIdentifier, null);

            return(PacketIdentifier);
        }
예제 #4
0
        private async Task <ushort> PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data)
        {
            BinaryOutput Payload = new BinaryOutput();
            ushort       PacketIdentifier;

            Payload.WriteString(Topic);

            if (QoS > MqttQualityOfService.AtMostOnce)
            {
                PacketIdentifier = this.packetIdentifier++;
                if (PacketIdentifier == 0)
                {
                    PacketIdentifier = this.packetIdentifier++;
                }

                Payload.WriteUInt16(PacketIdentifier);
            }
            else
            {
                PacketIdentifier = 0;
            }

            Payload.WriteBytes(Data);

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();
            byte         b      = (byte)((int)MqttControlPacketType.PUBLISH << 4);

            if (Duplicate)
            {
                b |= 8;
            }

            b |= (byte)((int)QoS << 1);

            if (Retain)
            {
                b |= 1;
            }

            Packet.WriteByte(b);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            if (this.HasSniffers)
            {
                this.Information("PUBLISH(" + QoS.ToString() + ":" + Topic + ")");
            }

            await this.Write(PacketData, PacketIdentifier, null);

            return(PacketIdentifier);
        }
예제 #5
0
        private Task PINGRESP()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4);
            Packet.WriteUInt(0);

            byte[] PacketData = Packet.GetPacket();

            return(this.Write(PacketData, 0, null));
        }
예제 #6
0
        private void PINGRESP()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4);
            Packet.WriteUInt(0);

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, 0, null);
        }
예제 #7
0
        private void PUBREL(ushort PacketIdentifier)
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)(((int)MqttControlPacketType.PUBREL << 4) | 2));
            Packet.WriteUInt(2);
            Packet.WriteUInt16(PacketIdentifier);

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, PacketIdentifier, null);
        }
예제 #8
0
        private void PUBREC(ushort PacketIdentifier)
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PUBREC << 4);
            Packet.WriteUInt(2);
            Packet.WriteUInt16(PacketIdentifier);

            byte[] PacketData = Packet.GetPacket();

            this.Write(PacketData, 0, null);
        }
예제 #9
0
        private Task PUBCOMP(ushort PacketIdentifier)
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PUBCOMP << 4);
            Packet.WriteUInt(2);
            Packet.WriteUInt16(PacketIdentifier);

            byte[] PacketData = Packet.GetPacket();

            return(this.Write(PacketData, 0, null));
        }
예제 #10
0
        private ushort PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data)
        {
            BinaryOutput Payload = new BinaryOutput();
            ushort       PacketIdentifier;

            Payload.WriteString(Topic);

            if (QoS > MqttQualityOfService.AtMostOnce)
            {
                PacketIdentifier = this.packetIdentifier++;
                if (PacketIdentifier == 0)
                {
                    PacketIdentifier = this.packetIdentifier++;
                }

                Payload.WriteUInt16(PacketIdentifier);
            }
            else
            {
                PacketIdentifier = 0;
            }

            Payload.WriteBytes(Data);

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();
            byte         b      = (byte)((int)MqttControlPacketType.PUBLISH << 4);

            if (Duplicate)
            {
                b |= 8;
            }

            b |= (byte)((int)QoS << 1);

            if (Retain)
            {
                b |= 1;
            }

            Packet.WriteByte(b);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, PacketIdentifier, null);

            return(PacketIdentifier);
        }
예제 #11
0
        private Task PINGRESP()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4);
            Packet.WriteUInt(0);

            byte[] PacketData = Packet.GetPacket();

            if (this.HasSniffers)
            {
                this.Information("PINGRESP");
            }

            return(this.Write(PacketData, 0, null));
        }
예제 #12
0
        private void CONNECT(int KeepAliveSeconds)
        {
            this.State            = MqttState.Authenticating;
            this.keepAliveSeconds = KeepAliveSeconds;
            this.nextPing         = DateTime.Now.AddMilliseconds(KeepAliveSeconds * 500);
            this.secondTimer      = new Timer(this.SecondTimer_Elapsed, null, 1000, 1000);

            BinaryOutput Payload = new BinaryOutput();

            Payload.WriteString("MQTT");
            Payload.WriteByte(4);       // v3.1.1

            byte b = 2;                 // Clean session.

            Payload.WriteByte(b);

            Payload.WriteByte((byte)(KeepAliveSeconds >> 8));
            Payload.WriteByte((byte)KeepAliveSeconds);

            Payload.WriteString(this.clientId);
            if (!string.IsNullOrEmpty(this.userName))
            {
                b |= 128;
                Payload.WriteString(this.userName);

                if (!string.IsNullOrEmpty(this.password))
                {
                    b |= 64;
                    Payload.WriteString(this.password);
                }
            }

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.CONNECT << 4);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, 0, null);
            this.inputState = 0;

            Task T = this.ReadLoop();
        }
예제 #13
0
        private Task PUBCOMP(ushort PacketIdentifier)
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.PUBCOMP << 4);
            Packet.WriteUInt(2);
            Packet.WriteUInt16(PacketIdentifier);

            byte[] PacketData = Packet.GetPacket();

            if (this.HasSniffers)
            {
                this.Information("PUBCOMP");
            }

            return(this.Write(PacketData, 0, null));
        }
예제 #14
0
        private async Task DISCONNECT()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.DISCONNECT << 4);
            Packet.WriteUInt(2);

            byte[] PacketData = Packet.GetPacket();

            TaskCompletionSource <bool> Done = new TaskCompletionSource <bool>();

            await this.Write(PacketData, 0, (sender, e) =>
            {
                this.State = MqttState.Offline;
                Done.TrySetResult(true);
            });

            await Task.WhenAny(Done.Task, Task.Delay(1000));
        }
예제 #15
0
        private void DISCONNECT()
        {
            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.DISCONNECT << 4);
            Packet.WriteUInt(2);

            byte[] PacketData = Packet.GetPacket();

            ManualResetEvent Done = new ManualResetEvent(false);

            this.BeginWrite(PacketData, 0, (sender, e) =>
            {
                this.State = MqttState.Offline;
                Done.Set();
            });

            Done.WaitOne(1000);
        }
예제 #16
0
        /// <summary>
        /// Subscribes to information from a set of topics. Topics can include wildcards.
        /// </summary>
        /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param>
        /// <returns>Packet identifier assigned to subscription.</returns>
        public async Task <ushort> SUBSCRIBE(params KeyValuePair <string, MqttQualityOfService>[] Topics)
        {
            BinaryOutput Payload = new BinaryOutput();
            ushort       PacketIdentifier;

            PacketIdentifier = this.packetIdentifier++;
            if (PacketIdentifier == 0)
            {
                PacketIdentifier = this.packetIdentifier++;
            }

            Payload.WriteUInt16(PacketIdentifier);

            foreach (KeyValuePair <string, MqttQualityOfService> Pair in Topics)
            {
                Payload.WriteString(Pair.Key);
                Payload.WriteByte((byte)Pair.Value);
            }

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();
            byte         b      = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4);

            b |= 2;

            Packet.WriteByte(b);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            await this.Write(PacketData, PacketIdentifier, null);

            return(PacketIdentifier);
        }
예제 #17
0
        private async Task CONNECT(int KeepAliveSeconds)
        {
            this.State            = MqttState.Authenticating;
            this.keepAliveSeconds = KeepAliveSeconds;
            this.nextPing         = DateTime.Now.AddMilliseconds(KeepAliveSeconds * 500);
            this.secondTimer      = new Timer(this.SecondTimer_Elapsed, null, 1000, 1000);

            BinaryOutput Payload = new BinaryOutput();

            Payload.WriteString("MQTT");
            Payload.WriteByte(4);       // v3.1.1

            byte b = 2;                 // Clean session.

            if (this.will)
            {
                b |= 4;
                b |= (byte)(((int)this.willQoS) << 3);

                if (this.willRetain)
                {
                    b |= 32;
                }
            }

            if (!string.IsNullOrEmpty(this.userName))
            {
                b |= 128;

                if (!string.IsNullOrEmpty(this.password))
                {
                    b |= 64;
                }
            }

            Payload.WriteByte(b);

            Payload.WriteByte((byte)(KeepAliveSeconds >> 8));
            Payload.WriteByte((byte)KeepAliveSeconds);

            Payload.WriteString(this.clientId);

            if (this.will)
            {
                Payload.WriteString(this.willTopic);

                int l = this.willData.Length;

                Payload.WriteByte((byte)(l >> 8));
                Payload.WriteByte((byte)l);
                Payload.WriteBytes(this.willData);
            }

            if (!string.IsNullOrEmpty(this.userName))
            {
                Payload.WriteString(this.userName);

                if (!string.IsNullOrEmpty(this.password))
                {
                    Payload.WriteString(this.password);
                }
            }

            byte[] PayloadData = Payload.GetPacket();

            BinaryOutput Packet = new BinaryOutput();

            Packet.WriteByte((byte)MqttControlPacketType.CONNECT << 4);
            Packet.WriteUInt((uint)PayloadData.Length);
            Packet.WriteBytes(PayloadData);

            byte[] PacketData = Packet.GetPacket();

            await this.Write(PacketData, 0, null);

            this.inputState = 0;
        }
예제 #18
0
 /// <summary>
 /// Publishes information on a topic.
 /// </summary>
 /// <param name="Topic">Topic name</param>
 /// <param name="QoS">Quality of service</param>
 /// <param name="Retain">If topic should retain information.</param>
 /// <param name="Data">Binary data to send.</param>
 /// <returns>Packet identifier assigned to data.</returns>
 public Task <ushort> PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, BinaryOutput Data)
 {
     return(this.PUBLISH(Topic, QoS, Retain, false, Data.GetPacket()));
 }