Ejemplo n.º 1
0
        void TryMaintainConnection(MqttClientConnection connection, DateTime now)
        {
            try
            {
                if (connection.Status != MqttClientConnectionStatus.Running)
                {
                    // The connection is already dead or just created so there is no need to check it.
                    return;
                }

                if (connection.ConnectPacket.KeepAlivePeriod == 0)
                {
                    // The keep alive feature is not used by the current connection.
                    return;
                }

                if (connection.IsReadingPacket)
                {
                    // The connection is currently reading a (large) packet. So it is obviously
                    // doing something and thus "connected".
                    return;
                }

                // Values described here: [MQTT-3.1.2-24].
                // If the client sends 5 sec. the server will allow up to 7.5 seconds.
                // If the client sends 1 sec. the server will allow up to 1.5 seconds.
                var maxDurationWithoutPacket = connection.ConnectPacket.KeepAlivePeriod * 1.5D;

                var secondsWithoutPackage = (now - connection.LastPacketReceivedTimestamp).TotalSeconds;
                if (secondsWithoutPackage < maxDurationWithoutPacket)
                {
                    // A packet was received before the timeout is affected.
                    return;
                }

                _logger.Warning(null, "Client '{0}': Did not receive any packet or keep alive signal.", connection.ClientId);

                // Execute the disconnection in background so that the keep alive monitor can continue
                // with checking other connections.
                // We do not need to wait for the task so no await is needed.
                // Also the internal state of the connection must be swapped to "Finalizing" because the
                // next iteration of the keep alive timer happens.
                var _ = connection.StopAsync(MqttDisconnectReasonCode.KeepAliveTimeout);
            }
            catch (Exception exception)
            {
                _logger.Error(exception, "Client {0}: Unhandled exception while checking keep alive timeouts.", connection.ClientId);
            }
        }
Ejemplo n.º 2
0
        async Task <MqttClientConnection> CreateClientConnectionAsync(MqttConnectPacket connectPacket, MqttConnectionValidatorContext connectionValidatorContext, IMqttChannelAdapter channelAdapter)
        {
            MqttClientConnection connection;
            MqttClientConnection existingConnection = null;

            using (await _createConnectionGate.WaitAsync(_cancellationToken).ConfigureAwait(false))
            {
                var session = _sessions.AddOrUpdate(
                    connectPacket.ClientId,
                    key =>
                {
                    _logger.Verbose("Created a new session for client '{0}'.", key);
                    return(new MqttClientSession(key, connectionValidatorContext.SessionItems, _eventDispatcher, _options, _retainedMessagesManager, _rootLogger));
                },
                    (key, existingSession) =>
                {
                    if (connectPacket.CleanSession)
                    {
                        _logger.Verbose("Deleting existing session of client '{0}'.", connectPacket.ClientId);
                        return(new MqttClientSession(key, connectionValidatorContext.SessionItems, _eventDispatcher, _options, _retainedMessagesManager, _rootLogger));
                    }

                    _logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
                    return(existingSession);
                });

                connection = new MqttClientConnection(connectPacket, channelAdapter, session, connectionValidatorContext, _options, this, _retainedMessagesManager, _rootLogger);

                _connections.AddOrUpdate(
                    connectPacket.ClientId,
                    key => connection,
                    (key, tempExistingConnection) =>
                {
                    existingConnection = tempExistingConnection;
                    return(connection);
                });
            }

            // Disconnect the client outside of the lock so that new clients can still connect while a single
            // one is being disconnected.
            if (existingConnection != null)
            {
                await existingConnection.StopAsync(true).ConfigureAwait(false);
            }

            return(connection);
        }
        private async Task <MqttClientConnection> CreateConnectionAsync(IMqttChannelAdapter channelAdapter, MqttConnectPacket connectPacket)
        {
            await _createConnectionGate.WaitAsync(_cancellationToken).ConfigureAwait(false);

            try
            {
                var isSessionPresent = _sessions.TryGetValue(connectPacket.ClientId, out var session);

                var isConnectionPresent = _connections.TryGetValue(connectPacket.ClientId, out var existingConnection);
                if (isConnectionPresent)
                {
                    await existingConnection.StopAsync().ConfigureAwait(false);
                }

                if (isSessionPresent)
                {
                    if (connectPacket.CleanSession)
                    {
                        session = null;

                        _logger.Verbose("Deleting existing session of client '{0}'.", connectPacket.ClientId);
                    }
                    else
                    {
                        _logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
                    }
                }

                if (session == null)
                {
                    session = new MqttClientSession(connectPacket.ClientId, _eventDispatcher, _options, _logger);
                    _logger.Verbose("Created a new session for client '{0}'.", connectPacket.ClientId);
                }

                var connection = new MqttClientConnection(connectPacket, channelAdapter, session, _options, this, _retainedMessagesManager, _logger);

                _connections[connection.ClientId] = connection;
                _sessions[session.ClientId]       = session;

                return(connection);
            }
            finally
            {
                _createConnectionGate.Release();
            }
        }
Ejemplo n.º 4
0
        async Task <MqttClientConnection> CreateClientConnectionAsync(MqttConnectPacket connectPacket, MqttConnectionValidatorContext connectionValidatorContext, IMqttChannelAdapter channelAdapter, Func <Task> onStart, Func <MqttClientDisconnectType, Task> onStop)
        {
            using (await _createConnectionGate.WaitAsync(_cancellationToken).ConfigureAwait(false))
            {
                var isSessionPresent = _sessions.TryGetValue(connectPacket.ClientId, out var session);

                var isConnectionPresent = _connections.TryGetValue(connectPacket.ClientId, out var existingConnection);
                if (isConnectionPresent)
                {
                    await existingConnection.StopAsync(true).ConfigureAwait(false);
                }

                if (isSessionPresent)
                {
                    if (connectPacket.CleanSession)
                    {
                        session = null;

                        _logger.Verbose("Deleting existing session of client '{0}'.", connectPacket.ClientId);
                    }
                    else
                    {
                        _logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
                    }
                }

                if (session == null)
                {
                    session = new MqttClientSession(connectPacket.ClientId, connectionValidatorContext.SessionItems, _eventDispatcher, _options, _retainedMessagesManager, _logger);
                    _logger.Verbose("Created a new session for client '{0}'.", connectPacket.ClientId);
                }

                var connection = new MqttClientConnection(connectPacket, channelAdapter, session, _options, this, _retainedMessagesManager, onStart, onStop, _logger);

                _connections[connection.ClientId] = connection;
                _sessions[session.ClientId]       = session;

                return(connection);
            }
        }
Ejemplo n.º 5
0
        async Task <MqttApplicationMessageInterceptorContext> InterceptApplicationMessageAsync(MqttClientConnection senderConnection, MqttApplicationMessage applicationMessage)
        {
            var interceptor = _options.ApplicationMessageInterceptor;

            if (interceptor == null)
            {
                return(null);
            }

            string senderClientId;
            IDictionary <object, object> sessionItems;

            var messageIsFromServer = senderConnection == null;

            if (messageIsFromServer)
            {
                senderClientId = _options.ClientId;
                sessionItems   = _serverSessionItems;
            }
            else
            {
                senderClientId = senderConnection.ClientId;
                sessionItems   = senderConnection.Session.Items;
            }

            var interceptorContext = new MqttApplicationMessageInterceptorContext(senderClientId, sessionItems, applicationMessage);
            await interceptor.InterceptApplicationMessagePublishAsync(interceptorContext).ConfigureAwait(false);

            return(interceptorContext);
        }
Ejemplo n.º 6
0
        public void DispatchApplicationMessage(MqttApplicationMessage applicationMessage, MqttClientConnection sender)
        {
            if (applicationMessage == null)
            {
                throw new ArgumentNullException(nameof(applicationMessage));
            }

            _messageQueue.Enqueue(new MqttEnqueuedApplicationMessage(applicationMessage, sender));
        }
        async Task <MqttApplicationMessageInterceptorContext> InterceptApplicationMessageAsync(IMqttServerApplicationMessageInterceptor interceptor, MqttClientConnection clientConnection, MqttApplicationMessage applicationMessage)
        {
            string senderClientId;
            IDictionary <object, object> sessionItems;

            var messageIsFromServer = clientConnection == null;

            if (messageIsFromServer)
            {
                senderClientId = _options.ClientId;
                sessionItems   = _serverSessionItems;
            }
            else
            {
                senderClientId = clientConnection.ClientId;
                sessionItems   = clientConnection.Session.Items;
            }

            var interceptorContext = new MqttApplicationMessageInterceptorContext(senderClientId, sessionItems, _logger)
            {
                AcceptPublish      = true,
                ApplicationMessage = applicationMessage,
                CloseConnection    = false
            };

            await interceptor.InterceptApplicationMessagePublishAsync(interceptorContext).ConfigureAwait(false);

            return(interceptorContext);
        }
        private async Task <MqttApplicationMessageInterceptorContext> InterceptApplicationMessageAsync(MqttClientConnection sender, MqttApplicationMessage applicationMessage)
        {
            var interceptor = _options.ApplicationMessageInterceptor;

            if (interceptor == null)
            {
                return(null);
            }

            var senderClientId = sender?.ClientId;

            if (sender == null)
            {
                senderClientId = _options.ClientId;
            }

            var interceptorContext = new MqttApplicationMessageInterceptorContext(senderClientId, applicationMessage);
            await interceptor.InterceptApplicationMessagePublishAsync(interceptorContext).ConfigureAwait(false);

            return(interceptorContext);
        }
Ejemplo n.º 9
0
 public MqttPendingApplicationMessage(MqttApplicationMessage applicationMessage, MqttClientConnection sender)
 {
     Sender             = sender;
     ApplicationMessage = applicationMessage;
 }