Example #1
0
        public void UnacceptableProtocolVersion()
        {
            // our expected result
            var expected = new[]
            {
                (byte)0x20,
                (byte)0x02,
                (byte)0x0,
                (byte)0x1,
            };

            MqttConnectAckMessage msg = new MqttConnectAckMessage().WithReturnCode(MqttConnectReturnCode.UnacceptedProtocolVersion);
            Console.WriteLine(msg);

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

            Assert.Equal<int>(expected.Length, actual.Length);
            Assert.Equal<byte>(expected[0], actual[0]); // msg type of header
            Assert.Equal<byte>(expected[1], actual[1]); // remaining length
            Assert.Equal<byte>(expected[2], actual[2]); // connect ack - compression? always empty
            Assert.Equal<byte>(expected[3], actual[3]); // return code.
        }
        /// <summary>
        /// Processes the connect acknowledgement message.
        /// </summary>
        /// <param name="msg">The connect acknowledgement message.</param>
        private bool ConnectAckProcessor(MqttMessage msg)
        {
            try
            {
                MqttConnectAckMessage ackMsg = (MqttConnectAckMessage)msg;

                // drop the connection if our connect request has been rejected.
                if (ackMsg.VariableHeader.ReturnCode == MqttConnectReturnCode.BrokerUnavailable ||
                    ackMsg.VariableHeader.ReturnCode == MqttConnectReturnCode.IdentifierRejected ||
                    ackMsg.VariableHeader.ReturnCode == MqttConnectReturnCode.UnacceptedProtocolVersion)
                {
                    // TODO: Decide on a way to let the client know why we have been rejected.
                    PerformConnectionDisconnect();
                }
                else
                {
                    // initialize the keepalive to start the ping based keepalive process.
                    connectionState = ConnectionState.Connected;
                }

                connectionResetEvent.Set();
            }
            catch (InvalidMessageException)
            {
                PerformConnectionDisconnect();

                // not exactly, ready, but we've reached an end state so we should signal a bad connection attempt.
                connectionResetEvent.Set();
            }
            finally
            {
                // connected or disconnected now, don't need to process no more.
                // TODO: Implement one time only message registrations
                //this.UnRegisterForMessage(MqttMessageType.ConnectAck, ConnectAckProcessor);
            }

            return(true);
        }
Example #3
0
 internal void SendMessage(MqttConnectAckMessage ack)
 {
     throw new NotImplementedException();
 }
        public void SuccessfullResponseCausesConnectionStateConnected()
        {
            // register a method to process the Connect message and respond with a ConnectAck message
            broker.SetMessageHandler((messageArrived) =>
                {
                    MqttConnectMessage connect = (MqttConnectMessage)MqttMessage.CreateFrom(messageArrived);
                    MqttConnectAckMessage ack = new MqttConnectAckMessage().WithReturnCode(MqttConnectReturnCode.ConnectionAccepted);
                    broker.SendMessage(ack);
                });

            var ch = new SynchronousMqttConnectionHandler();
            Assert.Equal<ConnectionState>(ConnectionState.Connected, 
                ch.Connect(mockBrokerAddress, mockBrokerPort, new MqttConnectMessage().WithClientIdentifier(testClientId)));
        }