/// <summary>
        ///     Synchronously connect to the specific Mqtt Connection.
        /// </summary>
        /// <param name="hostname">The hostname hostnameto connect to.</param>
        /// <param name="port">The port on the host to connect to.</param>
        /// <param name="connectMessage">The connection message that should be used to initiate the connection.</param>
        protected override ConnectionState InternalConnect(string hostname, int port, MqttConnectMessage connectMessage) {
            int connectionAttempts = 0;

            do {
                // Initiate the connection
                connectionState = ConnectionState.Connecting;

                connection = MqttConnection.Connect(hostname, port);
                this.RegisterForMessage(MqttMessageType.ConnectAck, ConnectAckProcessor);
                connection.DataAvailable += connection_MessageDataAvailable;

                // transmit the required connection message to the broker.
                SendMessage(connectMessage);

                // we're the sync connection handler so we need to wait for the brokers acknowlegement of the connections
                if (!connectionResetEvent.WaitOne(5000, false)) {
                    // if we don't get a response in 5 seconds, dispose the connection and rebuild it.
                    connectionState = ConnectionState.Disconnecting;
                    connection.Dispose();
                    connectionState = ConnectionState.Disconnected;
                }
            } while (connectionState != ConnectionState.Connected && ++connectionAttempts < MaxConnectionAttempts);

            // if we've failed to handshake with the broker, throw an exception.
            if (connectionState != ConnectionState.Connected) {
                throw new ConnectionException(
                    String.Format("The maximum allowed connection attempts ({0}) were exceeded. The broker " +
                                  "is not responding to the connection request message (Missing Connection Acknowledgement)",
                                  MaxConnectionAttempts),
                    connectionState);
            }

            return connectionState;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Gets a configured connect message.
        /// </summary>
        /// <returns>An MqttConnectMessage that can be used to connect to a message broker.</returns>
        private MqttConnectMessage GetConnectMessage(string username, string password)
        {
            var message = new MqttConnectMessage()
                          .WithClientIdentifier(clientIdentifier)
                          .KeepAliveFor(30)
                          .AuthenticateAs(username, password)
                          .StartClean();

            return(message);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Performs a synchronous connect to the message broker.
        /// </summary>
        public ConnectionState Connect()
        {
            MqttConnectMessage connectMessage = GetConnectMessage();

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            subscriptionsManager = new SubscriptionsManager(connectionHandler);
            messageLogger        = new MessageLogger(connectionHandler);
            publishingManager    = new PublishingManager(connectionHandler, HandlePublishMessage);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
Ejemplo n.º 4
0
        public void BasicSerialization()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                .WithClientIdentifier("mark")
                .KeepAliveFor(30)
                .StartClean();

            Console.WriteLine(msg);

            byte[] mb = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal<byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal<byte>(18, mb[1]);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Connect to the specific Mqtt Connection.
        /// </summary>
        /// <param name="server">The hostname to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="message">The connect message to use as part of the connection process.</param>
        public ConnectionState Connect(string server, int port, MqttConnectMessage message) {
            try {
                connectionState = InternalConnect(server, port, message);
            } catch (ConnectionException) {
                connectionState = ConnectionState.Faulted;
                throw;
            }

            // if we managed to connection, ensure we catch any unexpected disconnects
            if (connectionState == ConnectionState.Connected) {
                this.connection.ConnectionDropped +=
                    (sender, e) => { this.connectionState = ConnectionState.Disconnected; };
            }

            return connectionState;
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Connect to the specific Mqtt Connection.
        /// </summary>
        /// <param name="server">The hostname to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="message">The connect message to use as part of the connection process.</param>
        public ConnectionState Connect(string server, int port, MqttConnectMessage message)
        {
            try {
                connectionState = InternalConnect(server, port, message);
            } catch (ConnectionException) {
                connectionState = ConnectionState.Faulted;
                throw;
            }

            // if we managed to connection, ensure we catch any unexpected disconnects
            if (connectionState == ConnectionState.Connected)
            {
                this.connection.ConnectionDropped +=
                    (sender, e) => { this.connectionState = ConnectionState.Disconnected; };
            }

            return(connectionState);
        }
        /// <summary>
        /// Synchronously connect to the specific Mqtt Connection.
        /// </summary>
        /// <param name="connection"></param>
        protected override ConnectionState InternalConnect(string server, int port, MqttConnectMessage connectMessage)
        {
            int connectionAttempts = 0;

            do
            {
                // Initiate the connection
                connectionState = ConnectionState.Connecting;

                connection = MqttConnection.Connect(server, port);
                this.RegisterForMessage(MqttMessageType.ConnectAck, ConnectAckProcessor);
                connection.DataAvailable += connection_MessageDataAvailable;

                // transmit the required connection message to the broker.
                SendMessage(connectMessage);

                // we're the sync connection handler so we need to wait for the brokers acknowlegement of the connections
                if (!connectionResetEvent.WaitOne(5000, false))
                {
                    // if we don't get a response in 5 seconds, dispose the connection and rebuild it.
                    connectionState = ConnectionState.Disconnecting;
                    connection.Dispose();
                    connectionState = ConnectionState.Disconnected;
                }
            } while (connectionState != ConnectionState.Connected && ++connectionAttempts < MaxConnectionAttempts);

            // if we've failed to handshake with the broker, throw an exception.
            if (connectionState != ConnectionState.Connected)
            {
                throw new ConnectionException(
                          String.Format("The maximum allowed connection attempts ({0}) were exceeded. The broker " +
                                        "is not responding to the connection request message (Missing Connection Acknowledgement)",
                                        MaxConnectionAttempts),
                          connectionState);
            }

            return(connectionState);
        }
Ejemplo n.º 8
0
        public void WithWillSet()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                .WithProtocolName("MQIsdp")
                .WithProtocolVersion(3)
                .WithClientIdentifier("mark")
                .KeepAliveFor(30)
                .StartClean()
                .Will()
                .WithWillQos(MqttQos.AtLeastOnce)
                .WithWillRetain()
                .WithWillTopic("willTopic")
                .WithWillMessage("willMessage");

            Console.WriteLine(msg);

            byte[] mb = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal<byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal<byte>(42, mb[1]);

        }
Ejemplo n.º 9
0
 /// <summary>
 /// Connect to the specific Mqtt Connection.
 /// </summary>
 /// <param name="server">The server to connect to.</param>
 /// <param name="port">The port to connect to.</param>
 /// <param name="message">The connect message to use as part of the connection process.</param>
 protected abstract ConnectionState InternalConnect(string server, int port, MqttConnectMessage message);
Ejemplo n.º 10
0
 /// <summary>
 ///     Connect to the specific Mqtt Connection.
 /// </summary>
 /// <param name="hostname">The hostname to connect to.</param>
 /// <param name="port">The port to connect to.</param>
 /// <param name="message">The connect message to use as part of the connection process.</param>
 protected abstract ConnectionState InternalConnect(string hostname, int port, MqttConnectMessage message);
Ejemplo n.º 11
0
 /// <summary>
 /// Connect to the specific Mqtt Connection.
 /// </summary>
 /// <param name="server">The server to connect to.</param>
 /// <param name="port">The port to connect to.</param>
 /// <param name="message">The connect message to use as part of the connection process.</param>
 protected abstract ConnectionState InternalConnect(string server, int port, MqttConnectMessage message);
Ejemplo n.º 12
0
 /// <summary>
 ///     Connect to the specific Mqtt Connection.
 /// </summary>
 /// <param name="hostname">The hostname to connect to.</param>
 /// <param name="port">The port to connect to.</param>
 /// <param name="message">The connect message to use as part of the connection process.</param>
 protected abstract ConnectionState InternalConnect(string hostname, int port, MqttConnectMessage message);