public void Stop(MqttClientDisconnectType type)
        {
            try
            {
                var cts = _cancellationTokenSource;
                if (cts == null || cts.IsCancellationRequested)
                {
                    return;
                }

                _wasCleanDisconnect = type == MqttClientDisconnectType.Clean;

                _cancellationTokenSource?.Cancel(false);

                if (_willMessage != null && !_wasCleanDisconnect)
                {
                    _sessionsManager.EnqueueApplicationMessage(this, _willMessage.ToPublishPacket());
                }

                _willMessage = null;
            }
            finally
            {
                _logger.Info("Client '{0}': Session stopped.", ClientId);
            }
        }
        private void Stop(MqttClientDisconnectType type, bool isInsideSession)
        {
            try
            {
                var cts = _cancellationTokenSource;
                if (cts == null || cts.IsCancellationRequested)
                {
                    return;
                }

                _cancellationTokenSource?.Cancel(false);

                _wasCleanDisconnect = type == MqttClientDisconnectType.Clean;

                if (_willMessage != null && !_wasCleanDisconnect)
                {
                    _sessionsManager.EnqueueApplicationMessage(this, _willMessage.ToPublishPacket());
                }

                _willMessage = null;

                if (!isInsideSession)
                {
                    _workerTask?.GetAwaiter().GetResult();
                }
            }
            finally
            {
                _logger.Info("Client '{0}': Disconnected (clean={1}).", ClientId, _wasCleanDisconnect);
                _eventDispatcher.OnClientDisconnected(ClientId, _wasCleanDisconnect);
            }
        }
        public Task HandleClientDisconnectedAsync(string clientId, MqttClientDisconnectType disconnectType)
        {
            var handler = ClientDisconnectedHandler;

            if (handler == null)
            {
                return(Task.FromResult(0));
            }

            return(handler.HandleClientDisconnectedAsync(new MqttServerClientDisconnectedEventArgs(clientId, disconnectType)));
        }
        public async Task TryHandleClientDisconnectedAsync(string clientId, MqttClientDisconnectType disconnectType)
        {
            try
            {
                var handler = ClientDisconnectedHandler;
                if (handler == null)
                {
                    return;
                }

                await handler.HandleClientDisconnectedAsync(new MqttServerClientDisconnectedEventArgs(clientId, disconnectType)).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                _logger.Error(exception, "Error while handling 'ClientDisconnected' event.");
            }
        }
        public async Task SafeNotifyClientDisconnectedAsync(string clientId, MqttClientDisconnectType disconnectType, string endpoint)
        {
            try
            {
                var handler = ClientDisconnectedHandler;
                if (handler == null)
                {
                    return;
                }

                await handler.HandleClientDisconnectedAsync(new MqttServerClientDisconnectedEventArgs
                {
                    ClientId       = clientId,
                    DisconnectType = disconnectType,
                    Endpoint       = endpoint
                }).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                _logger.Error(exception, "Error while handling custom 'ClientDisconnected' event.");
            }
        }
 public void Stop(MqttClientDisconnectType type)
 {
     Stop(type, false);
 }
 public void Stop(MqttClientDisconnectType disconnectType)
 {
     StopCalledCount++;
 }
Exemple #8
0
        private async Task CleanUpClient(string clientId, IMqttChannelAdapter channelAdapter, MqttClientDisconnectType disconnectType)
        {
            if (clientId != null)
            {
                _connections.TryRemove(clientId, out _);

                if (!_options.EnablePersistentSessions)
                {
                    await DeleteSessionAsync(clientId).ConfigureAwait(false);
                }
            }

            await SafeCleanupChannelAsync(channelAdapter).ConfigureAwait(false);

            if (clientId != null)
            {
                await _eventDispatcher.SafeNotifyClientDisconnectedAsync(clientId, disconnectType).ConfigureAwait(false);
            }
        }
        public async Task CleanUpClient(string clientId, IMqttChannelAdapter channelAdapter, MqttClientDisconnectType disconnectType)
        {
            if (clientId != null)
            {
                // in case it is a takeover _connections already contains the new connection
                if (disconnectType != MqttClientDisconnectType.Takeover)
                {
                    lock (_connections)
                    {
                        _connections.Remove(clientId);
                    }

                    if (!_options.EnablePersistentSessions)
                    {
                        await DeleteSessionAsync(clientId).ConfigureAwait(false);
                    }
                }
            }

            var endpoint = channelAdapter.Endpoint;

            await SafeCleanupChannelAsync(channelAdapter).ConfigureAwait(false);

            if (clientId != null)
            {
                await _eventDispatcher.SafeNotifyClientDisconnectedAsync(clientId, disconnectType, endpoint).ConfigureAwait(false);
            }
        }
Exemple #10
0
 public MqttServerClientDisconnectedEventArgs(string clientId, MqttClientDisconnectType disconnectType)
 {
     ClientId       = clientId ?? throw new ArgumentNullException(nameof(clientId));
     DisconnectType = disconnectType;
 }