예제 #1
0
        /// <summary>
        /// Connect to a broker using a message builder instance.
        /// </summary>
        /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param>
        /// <param name="ipOrHost">An IP address or host name</param>
        /// <param name="port">The port number to use. Overrides default port in settings</param>
        /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns>
        public IAsyncOperation <MqttConnectAckMessage> ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port)
        {
            var tcs = new TaskCompletionSource <MqttConnectAckMessage>();

            _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, tcs);
            return(tcs.Task.AsAsyncOperation());
        }
        public void IsClosedCorrectlyReflectsStateOfObject()
        {
            var    ip            = "1.1.1.1";
            var    port          = 1883;
            var    eventData     = "Test data";
            string connectionKey = "123";
            var    moqSocket     = new MoqSocket();
            var    protocol      = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var    bldr          = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName        = "a/b/+",
                QualityOfService = QualityOfService.AtMostOnce
            };

            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            Assert.IsTrue(subClient.IsClosed);

            subClient.OnMessage(msg =>
            {
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.MessageType == MessageType.Publish);
            });

            Assert.IsFalse(subClient.IsClosed);
            subClient.Close();
            Assert.IsTrue(subClient.IsClosed);
        }
예제 #3
0
        public void ConnectAsyncCallsConnectCompleteEventWithEventData()
        {
            var are       = new AutoResetEvent(false);
            var ip        = "1.1.1.1";
            var port      = 1883;
            var eventData = "Test data";
            var moqSocket = new MoqSocket();
            var client    = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr      = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            MqttProtocolInformation.Settings.KeepAliveTime = 5 * 60;

            client.ConnectComplete += (sender, args) =>
            {
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect));
                are.Set();
            };

            client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            if (!are.WaitOne(5000))
            {
                Assert.Fail("ConnectComplete event not fired.");
            }
        }
예제 #4
0
        public void ConnectDisconnectConnectSequenceDoesNotThrow()
        {
            var areConnect    = new AutoResetEvent(false);
            var areDisconnect = new AutoResetEvent(false);
            var ip            = "1.1.1.1";
            var port          = 1883;
            var eventData     = "Test data";
            var moqSocket     = new MoqSocket();
            var client        = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr          = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            client.ConnectComplete += (sender, args) =>
            {
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect));
                areConnect.Set();
            };

            client.SendMessageComplete += (sender, args) =>
            {
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Disconnect));
                areDisconnect.Set();
            };

            client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);
            if (!areConnect.WaitOne(5000))
            {
                Assert.Fail("First ConnectComplete event did not fire.");
            }

            client.DisconnectAsync(eventData, null);
            if (!areDisconnect.WaitOne(5000))
            {
                Assert.Fail("First DisconnectComplete event did not fire.");
            }

            client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);
            if (!areConnect.WaitOne(5000))
            {
                Assert.Fail("Second ConnectComplete event did not fire.");
            }
        }
예제 #5
0
        /// <summary>
        /// Connect to a broker using a message builder instance.
        /// </summary>
        /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param>
        /// <param name="ipOrHost">An IP address or host name</param>
        /// <param name="port">The port number to use. Overrides default port in settings</param>
        /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns>
        public Task <MqttConnectAckMessage> ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port)
        {
            var tcs = new TaskCompletionSource <MqttConnectAckMessage>();

            if (_mqtt.IsConnected(_clientUid))
            {
                _mqtt.CloseConnection(_clientUid);
            }
            _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, tcs);
            return(tcs.Task);
        }
        public void OnMessageCallbackGetsCalledWithWildcardTopics1()
        {
            var    are           = new AutoResetEvent(false);
            var    ip            = "1.1.1.1";
            var    port          = 1883;
            var    eventData     = "Test data";
            string connectionKey = "123";
            var    moqSocket     = new MoqSocket();
            var    protocol      = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var    bldr          = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName        = "a/b/+",
                QualityOfService = QualityOfService.AtMostOnce
            };
            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            subClient.OnMessage(msg =>
            {
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.MessageType == MessageType.Publish);
                Assert.AreEqual(msg.Payload[0], 0x00);
                Assert.AreEqual(msg.Payload[1], 0x01);
                Assert.AreEqual(msg.Payload[2], 0x02);
                are.Set();
            });

            moqSocket.ReceiveMessage(new MqttPublishMessageBuilder
            {
                TopicName = "a/b/c",
                Payload   = new byte[] { 0x00, 0x01, 0x02 }
            });

            if (!are.WaitOne(5000))
            {
                Assert.Fail("OnMessage callback not called.");
            }
        }
        public void OnMessageCallbackDoesNotGetCalledAfterClose()
        {
            var    are           = new AutoResetEvent(false);
            var    ip            = "1.1.1.1";
            var    port          = 1883;
            var    eventData     = "Test data";
            string connectionKey = "123";
            var    moqSocket     = new MoqSocket();
            var    protocol      = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var    bldr          = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName        = "a/b/c",
                QualityOfService = QualityOfService.AtMostOnce
            };
            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            subClient.OnMessage(msg =>
            {
                Assert.Fail("OnMessage callback was called after Close call.");
                are.Set();
            });

            subClient.Close();

            moqSocket.ReceiveMessage(new MqttPublishMessageBuilder
            {
                TopicName = "a/b/c",
                Payload   = new byte[] { 0x00, 0x01, 0x02 }
            });

            are.WaitOne(3000);
        }
예제 #8
0
 /// <summary>
 /// Connect to a broker using a message builder instance.
 /// </summary>
 /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param>
 /// <param name="ipOrHost">An IP address or host name</param>
 /// <param name="port">The port number to use. Overrides default port in settings</param>
 /// <returns></returns>
 public void ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port)
 {
     _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, MessageType.Connect);
 }