示例#1
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);
        }
示例#2
0
        public static void Main(string[] args)
        {
            Initialize();

            Console.Out.Write("Host Name (default iot.eclipse.org): ");
            string Host = Console.In.ReadLine();

            if (string.IsNullOrEmpty(Host))
            {
                Console.Out.WriteLine("Using iot.eclipse.org.");
                Host = "iot.eclipse.org";
            }

            Console.Out.WriteLine();
            Console.Out.Write("Port Number (default 1883): ");
            string s = Console.In.ReadLine();
            int    Port;

            if (string.IsNullOrEmpty(s))
            {
                Console.Out.WriteLine("Using port 1883.");
                Port = 1883;
            }
            else
            {
                Port = int.Parse(s);
            }

            Console.Out.WriteLine();

            BinaryOutput Payload;
            int          PacketsLeft = NrTestsPerQoS;

            using (MqttConnection MqttConnection = ConnectToMqttServer("iot.eclipse.org", Port, string.Empty, string.Empty))
            {
                WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen);

                MqttConnection.TrustServer = true;

                MqttConnection.OnConnectionError += (sender, ex) =>
                {
                    WriteLine("Unable to connect:", C64Colors.Red);
                };

                MqttConnection.OnError += (sender, ex) =>
                {
                    WriteLine(ex.Message, C64Colors.Red);
                };

                MqttConnection.OnContentReceived += (sender, Content) =>
                {
                    string ClientId = Content.DataInput.ReadString();
                    if (ClientId == sender.ClientId)
                    {
                        DateTime             TP  = Content.DataInput.ReadDateTime();
                        MqttQualityOfService QoS = (MqttQualityOfService)Content.DataInput.ReadByte();
                        Console.Out.WriteLine("Latency: " + (DateTime.Now - TP).TotalMilliseconds + " ms (" + QoS.ToString() + ")");

                        bool Resend;

                        if (--PacketsLeft > 0)
                        {
                            Resend = true;
                        }
                        else if (QoS < MqttQualityOfService.ExactlyOne)
                        {
                            QoS         = (MqttQualityOfService)((int)QoS + 1);
                            PacketsLeft = NrTestsPerQoS;
                            Resend      = true;
                        }
                        else
                        {
                            Resend = false;
                        }

                        if (Resend)
                        {
                            Payload = new BinaryOutput();
                            Payload.WriteString(MqttConnection.ClientId);
                            Payload.WriteDateTime(DateTime.Now);
                            Payload.WriteByte((byte)QoS);

                            MqttConnection.PUBLISH("RetroSharp/Examples/Networking/Latency", QoS, false, Payload);
                        }
                        else
                        {
                            Console.Out.WriteLine("Press ENTER to continue.");
                        }
                    }
                };

                MqttConnection.OnStateChanged += (sender, state) =>
                {
                    WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen);

                    if (state == MqttState.Connected)
                    {
                        MqttConnection.SUBSCRIBE("RetroSharp/Examples/Networking/Latency");

                        Payload = new BinaryOutput();
                        Payload.WriteString(MqttConnection.ClientId);
                        Payload.WriteDateTime(DateTime.Now);
                        Payload.WriteByte((byte)MqttQualityOfService.AtMostOne);

                        MqttConnection.PUBLISH("RetroSharp/Examples/Networking/Latency", MqttQualityOfService.AtMostOne, false, Payload);
                    }
                };

                Console.In.ReadLine();
            }

            Terminate();
        }