Ejemplo n.º 1
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
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);
        }