Esempio 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 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);
        }
Esempio n. 2
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 void PING()
        {
            BinaryOutput Packet = new BinaryOutput();

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

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, 0);

            EventHandler h = this.OnPing;

            if (!(h is null))
            {
                try
                {
                    h(this, new EventArgs());
                }
                catch (Exception ex)
                {
#if LineListener
                    LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
                }
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private void PUBCOMP(ushort PacketIdentifier)
        {
            BinaryOutput Packet = new BinaryOutput();

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

            byte[] PacketData = Packet.GetPacket();

            this.BeginWrite(PacketData, 0);
        }
Esempio n. 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);
        }
Esempio n. 6
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.stream.BeginWrite(PacketData, 0, PacketData.Length, this.EndDisconnect, Done);

            Done.WaitOne(1000);
        }
Esempio n. 7
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();
        }
Esempio n. 8
0
        private void Serialize(Player Player, BinaryOutput Output)
        {
            Output.WriteString(Player.PublicEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.PublicEndpoint.Port);

            Output.WriteString(Player.LocalEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.LocalEndpoint.Port);

            Output.WriteGuid(Player.PlayerId);
            Output.WriteUInt((uint)Player.Count);

            foreach (KeyValuePair <string, string> P in Player)
            {
                Output.WriteString(P.Key);
                Output.WriteString(P.Value);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Creates inter-player peer-to-peer connections between known players.
        /// </summary>
        public async Task ConnectPlayers()
        {
            if (this.state != MultiPlayerState.FindingPlayers)
            {
                throw new Exception("The multiplayer environment is not in the state of finding players.");
            }

            await this.SetState(MultiPlayerState.ConnectingPlayers);

            int          Index  = 0;
            BinaryOutput Output = new BinaryOutput();

            Output.WriteByte(1);
            Output.WriteString(this.applicationName);
            this.localPlayer.Index = Index++;
            this.Serialize(this.localPlayer, Output);

#if LineListener
            Console.Out.Write("Tx: INTERCONNECT(" + this.localPlayer.ToString());
#endif
            lock (this.remotePlayersByEndpoint)
            {
                Output.WriteUInt((uint)this.remotePlayersByEndpoint.Count);

                foreach (Player Player in this.remotePlayersByEndpoint.Values)
                {
                    Player.Index = Index++;
                    this.Serialize(Player, Output);

#if LineListener
                    Console.Out.Write("," + Player.ToString());
#endif
                }
            }

            this.mqttTerminatedPacketIdentifier = await this.mqttConnection.PUBLISH(this.mqttNegotiationTopic, MqttQualityOfService.AtLeastOnce, false, Output);

            this.mqttConnection.OnPublished += this.MqttConnection_OnPublished;

#if LineListener
            Console.Out.WriteLine(")");
#endif
            await this.StartConnecting();
        }
Esempio n. 10
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);
        }
Esempio n. 11
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;
		}
Esempio n. 12
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);
		}
Esempio n. 13
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;
		}
Esempio n. 14
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);
		}
Esempio n. 15
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 void PING()
		{
			BinaryOutput Packet = new BinaryOutput();
			Packet.WriteByte((byte)MqttControlPacketType.PINGREQ << 4);
			Packet.WriteUInt(0);

			byte[] PacketData = Packet.GetPacket();

			this.BeginWrite(PacketData, 0);

			EventHandler h = this.OnPing;
			if (h != null)
			{
				try
				{
					h(this, new EventArgs());
				}
				catch (Exception ex)
				{
#if LineListener
					LLOut(ex.Message, Color.Yellow, Color.DarkRed);
#endif
				}
			}
		}
Esempio n. 16
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();
		}
Esempio n. 17
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.stream.BeginWrite(PacketData, 0, PacketData.Length, this.EndDisconnect, Done);

			Done.WaitOne(1000);
		}
Esempio n. 18
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;
		}