コード例 #1
0
 /// <summary>
 ///     Handles the MessageSent event of the connectionHandler control.
 /// </summary>
 private bool MessageSent(MqttMessage msg) {
     if (!this.disposed) {
         pingTimer.Change(keepAlivePeriod, keepAlivePeriod);
         return true;
     } else {
         return false;
     }
 }
コード例 #2
0
        /// <summary>
        /// Invokes the serialization of a message to get an array of bytes that represent the message.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        internal static byte[] GetMessageBytes(MqttMessage msg)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                msg.WriteTo(ms);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] msgBytes = new byte[ms.Length];
                ms.Read(msgBytes, 0, (int)ms.Length);

                return msgBytes;
            }
        }
コード例 #3
0
        /// <summary>
        /// Sends a message to the broker through the current connection.
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(MqttMessage message)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                message.WriteTo(stream);
                stream.Seek(0, SeekOrigin.Begin);
                connection.Send(stream);
            }

            // let any registered people know we're doing a message.
            foreach (Func<MqttMessage, bool> callback in sentMessageCallbacks)
            {
                callback(message);
            }
        }
コード例 #4
0
        /// <summary>
        ///     Processes the connect acknowledgement message.
        /// </summary>
        /// <param name="msg">The connect acknowledgement message.</param>
        private bool ConnectAckProcessor(MqttMessage msg) {
            try {
                var 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 ||
                    ackMsg.VariableHeader.ReturnCode == MqttConnectReturnCode.NotAuthorized ||
                    ackMsg.VariableHeader.ReturnCode == MqttConnectReturnCode.BadUsernameOrPassword)
                {
                    // 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();
            }

            return true;
        }
コード例 #5
0
ファイル: MockBroker.cs プロジェクト: fm0597/nmqtt
 /// <summary>
 /// Sends the message to the client connected to the broker.
 /// </summary>
 /// <param name="msg">The Mqtt Message.</param>
 public void SendMessage(MqttMessage msg)
 {
     msg.WriteTo(networkStream);
     networkStream.Flush();
 }
コード例 #6
0
        /// <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;
        }
コード例 #7
0
 /// <summary>
 /// Processed ping response messages received from a message broker.
 /// </summary>
 /// <param name="pingMsg"></param>
 /// <returns></returns>
 private bool PingResponseReceived(MqttMessage pingMsg)
 {
     return true;
 }
コード例 #8
0
        /// <summary>
        /// Signal to the keepalive that a ping request has been received from the message broker.
        /// </summary>
        /// <remarks>
        /// The effect of calling this method on the keepalive handler is the transmission of a ping response
        /// message to the message broker on the current connection.
        /// </remarks>
        private bool PingRequestReceived(MqttMessage pingMsg)
        {
            Monitor.Enter(shutdownPadlock);
            try
            {
                if (!disposed)
                {
                    MqttPingResponseMessage pingRespMessage = new MqttPingResponseMessage();
                    connectionHandler.SendMessage(pingRespMessage);

                    pingTimer.Change(keepAlivePeriod, keepAlivePeriod);
                }
            }
            finally
            {
                Monitor.Exit(shutdownPadlock);
            }

            return true;
        }
コード例 #9
0
 /// <summary>
 ///     Processed ping response messages received from a message broker.
 /// </summary>
 /// <param name="pingMsg"></param>
 /// <returns></returns>
 private bool PingResponseReceived(MqttMessage pingMsg)
 {
     return(true);
 }