Ejemplo n.º 1
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()));
 }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
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();

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

                sb.Append("SUBSCRIBE(");

                foreach (KeyValuePair <string, MqttQualityOfService> P in Topics)
                {
                    if (First)
                    {
                        First = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }

                    sb.Append(P.Value.ToString());
                    sb.Append(':');
                    sb.Append(P.Key);
                }

                sb.Append(')');

                this.Information(sb.ToString());
            }

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

            return(PacketIdentifier);
        }