コード例 #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 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();

            this.BeginWrite(PacketData, PacketIdentifier);

            return(PacketIdentifier);
        }
コード例 #2
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.AtMostOne)
            {
                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);

            return(PacketIdentifier);
        }
コード例 #3
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);
            this.inputState = 0;
            this.BeginRead();
        }
コード例 #4
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 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();

            this.BeginWrite(PacketData, PacketIdentifier);

            return(PacketIdentifier);
        }
コード例 #5
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.AtMostOne)
			{
				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);

			return PacketIdentifier;
		}
コード例 #6
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);
			this.inputState = 0;
			this.BeginRead();
		}
コード例 #7
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 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();

			this.BeginWrite(PacketData, PacketIdentifier);

			return PacketIdentifier;
		}
コード例 #8
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 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();

			this.BeginWrite(PacketData, PacketIdentifier);

			return PacketIdentifier;
		}