Exemple #1
0
        public async Task StopAsync()
        {
            try
            {
                if (_cancellationTokenSource == null)
                {
                    return;
                }

                _cancellationTokenSource.Cancel(false);

                _clientSessionsManager.Stop();

                foreach (var adapter in _adapters)
                {
                    adapter.ClientAccepted -= OnClientAccepted;
                    await adapter.StopAsync().ConfigureAwait(false);
                }

                _logger.Info("Stopped.");
                Stopped?.Invoke(this, EventArgs.Empty);
            }
            finally
            {
                _cancellationTokenSource?.Dispose();
                _cancellationTokenSource = null;

                _retainedMessagesManager = null;

                _clientSessionsManager?.Dispose();
                _clientSessionsManager = null;
            }
        }
Exemple #2
0
        public MqttClientConnection(MqttConnectPacket connectPacket,
                                    IMqttChannelAdapter channelAdapter,
                                    MqttClientSession session,
                                    MqttConnectionValidatorContext connectionValidatorContext,
                                    IMqttServerOptions serverOptions,
                                    MqttClientSessionsManager sessionsManager,
                                    IMqttRetainedMessagesManager retainedMessagesManager,
                                    IMqttNetLogger logger)
        {
            Session                  = session ?? throw new ArgumentNullException(nameof(session));
            _serverOptions           = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _sessionsManager         = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
            _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));

            _channelAdapter             = channelAdapter ?? throw new ArgumentNullException(nameof(channelAdapter));
            _connectionValidatorContext = connectionValidatorContext ?? throw new ArgumentNullException(nameof(connectionValidatorContext));
            _dataConverter = _channelAdapter.PacketFormatterAdapter.DataConverter;
            _endpoint      = _channelAdapter.Endpoint;
            ConnectPacket  = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.CreateScopedLogger(nameof(MqttClientConnection));

            _connectedTimestamp         = DateTime.UtcNow;
            LastPacketReceivedTimestamp = _connectedTimestamp;
            _lastNonKeepAlivePacketReceivedTimestamp = LastPacketReceivedTimestamp;
        }
Exemple #3
0
        public async Task StartAsync(IMqttServerOptions options)
        {
            Options = options ?? throw new ArgumentNullException(nameof(options));

            if (_cancellationTokenSource != null)
            {
                throw new InvalidOperationException("The server is already started.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _retainedMessagesManager = new MqttRetainedMessagesManager(Options, _logger);
            await _retainedMessagesManager.LoadMessagesAsync().ConfigureAwait(false);

            _clientSessionsManager = new MqttClientSessionsManager(Options, _retainedMessagesManager, _cancellationTokenSource.Token, _eventDispatcher, _logger);
            _clientSessionsManager.Start();

            foreach (var adapter in _adapters)
            {
                adapter.ClientAccepted += OnClientAccepted;
                await adapter.StartAsync(Options).ConfigureAwait(false);
            }

            _logger.Info("Started.");
            Started?.Invoke(this, EventArgs.Empty);
        }
Exemple #4
0
        public async Task StopAsync()
        {
            try
            {
                if (_cancellationTokenSource == null)
                {
                    return;
                }

                _cancellationTokenSource.Cancel(false);
                _cancellationTokenSource.Dispose();

                foreach (var adapter in _adapters)
                {
                    adapter.ClientAccepted -= OnClientAccepted;
                    await adapter.StopAsync();
                }

                await _clientSessionsManager.StopAsync();

                _logger.Info <MqttServer>("Stopped.");
            }
            finally
            {
                _clientSessionsManager?.Dispose();
                _retainedMessagesManager?.Dispose();

                _cancellationTokenSource = null;
                _retainedMessagesManager = null;
                _clientSessionsManager   = null;
            }
        }
        public MqttClientConnection(
            MqttConnectPacket connectPacket,
            IMqttChannelAdapter channelAdapter,
            MqttClientSession session,
            IMqttServerOptions serverOptions,
            MqttClientSessionsManager sessionsManager,
            MqttRetainedMessagesManager retainedMessagesManager,
            IMqttNetChildLogger logger)
        {
            Session                  = session ?? throw new ArgumentNullException(nameof(session));
            _serverOptions           = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _sessionsManager         = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
            _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));

            _channelAdapter = channelAdapter ?? throw new ArgumentNullException(nameof(channelAdapter));
            _dataConverter  = _channelAdapter.PacketFormatterAdapter.DataConverter;
            _endpoint       = _channelAdapter.Endpoint;
            _connectPacket  = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.CreateChildLogger(nameof(MqttClientConnection));

            _keepAliveMonitor = new MqttClientKeepAliveMonitor(_connectPacket.ClientId, StopAsync, _logger);

            _connectedTimestamp                      = DateTime.UtcNow;
            _lastPacketReceivedTimestamp             = _connectedTimestamp;
            _lastNonKeepAlivePacketReceivedTimestamp = _lastPacketReceivedTimestamp;
        }
Exemple #6
0
        public async Task StartAsync(IMqttServerOptions options)
        {
            Options = options ?? throw new ArgumentNullException(nameof(options));

            if (_cancellationTokenSource != null)
            {
                throw new InvalidOperationException("The server is already started.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _retainedMessagesManager = new MqttRetainedMessagesManager(Options, _logger);
            await _retainedMessagesManager.LoadMessagesAsync();

            _clientSessionsManager = new MqttClientSessionsManager(Options, _retainedMessagesManager, _logger)
            {
                ClientConnectedCallback            = OnClientConnected,
                ClientDisconnectedCallback         = OnClientDisconnected,
                ClientSubscribedTopicCallback      = OnClientSubscribedTopic,
                ClientUnsubscribedTopicCallback    = OnClientUnsubscribedTopic,
                ApplicationMessageReceivedCallback = OnApplicationMessageReceived
            };

            foreach (var adapter in _adapters)
            {
                adapter.ClientAccepted += OnClientAccepted;
                await adapter.StartAsync(Options);
            }

            _logger.Info <MqttServer>("Started.");
            Started?.Invoke(this, new MqttServerStartedEventArgs());
        }
Exemple #7
0
        public MqttClient(
            MqttConnectPacket connectPacket,
            IMqttChannelAdapter channelAdapter,
            MqttSession session,
            MqttServerOptions serverOptions,
            MqttServerEventContainer eventContainer,
            MqttClientSessionsManager sessionsManager,
            IMqttNetLogger logger)
        {
            _serverOptions   = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _eventContainer  = eventContainer;
            _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));

            ChannelAdapter = channelAdapter ?? throw new ArgumentNullException(nameof(channelAdapter));
            Endpoint       = channelAdapter.Endpoint;

            Session        = session ?? throw new ArgumentNullException(nameof(session));
            _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttClient));
        }
Exemple #8
0
        public MqttClientSession(
            string clientId,
            IMqttServerOptions options,
            MqttClientSessionsManager sessionsManager,
            MqttRetainedMessagesManager retainedMessagesManager,
            MqttServerEventDispatcher eventDispatcher,
            IMqttNetChildLogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _options                 = options ?? throw new ArgumentNullException(nameof(options));
            _sessionsManager         = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
            _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));
            _eventDispatcher         = eventDispatcher ?? throw new ArgumentNullException(nameof(eventDispatcher));

            ClientId = clientId;

            _logger = logger.CreateChildLogger(nameof(MqttClientSession));

            _keepAliveMonitor     = new MqttClientKeepAliveMonitor(this, _logger);
            _subscriptionsManager = new MqttClientSubscriptionsManager(clientId, _options, eventDispatcher);
            _pendingPacketsQueue  = new MqttClientPendingPacketsQueue(_options, this, _logger);
        }
Exemple #9
0
        public MqttServerKeepAliveMonitor(MqttServerOptions options, MqttClientSessionsManager sessionsManager, IMqttNetLogger logger)
        {
            _options         = options ?? throw new ArgumentNullException(nameof(options));
            _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttServerKeepAliveMonitor));
        }
        public MqttClientSession(
            string clientId,
            IMqttServerOptions options,
            MqttRetainedMessagesManager retainedMessagesManager,
            MqttClientSessionsManager sessionsManager,
            IMqttNetLogger logger)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));
            _sessionsManager         = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            ClientId = clientId;

            _subscriptionsManager = new MqttClientSubscriptionsManager(_options);
            _pendingMessagesQueue = new MqttClientPendingMessagesQueue(_options, this, _logger);
        }
Exemple #11
0
        public MqttServer(MqttServerOptions options, IEnumerable <IMqttServerAdapter> adapters, IMqttNetLogger logger)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            if (adapters == null)
            {
                throw new ArgumentNullException(nameof(adapters));
            }

            _adapters = adapters.ToList();

            _rootLogger = logger ?? throw new ArgumentNullException(nameof(logger));
            _logger     = logger.WithSource(nameof(MqttServer));

            _retainedMessagesManager = new MqttRetainedMessagesManager(_eventContainer, _rootLogger);
            _clientSessionsManager   = new MqttClientSessionsManager(options, _retainedMessagesManager, _eventContainer, _rootLogger);
            _keepAliveMonitor        = new MqttServerKeepAliveMonitor(options, _clientSessionsManager, _rootLogger);
        }
        public MqttSession(
            string clientId,
            bool isPersistent,
            IDictionary items,
            MqttServerOptions serverOptions,
            MqttServerEventContainer eventContainer,
            MqttRetainedMessagesManager retainedMessagesManager,
            MqttClientSessionsManager clientSessionsManager)
        {
            Id           = clientId ?? throw new ArgumentNullException(nameof(clientId));
            IsPersistent = isPersistent;
            Items        = items ?? throw new ArgumentNullException(nameof(items));

            _serverOptions         = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _clientSessionsManager = clientSessionsManager ?? throw new ArgumentNullException(nameof(clientSessionsManager));

            SubscriptionsManager = new MqttClientSubscriptionsManager(this, eventContainer, retainedMessagesManager, clientSessionsManager);
        }
Exemple #13
0
        public async Task StartAsync(IMqttServerOptions options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            if (_cancellationTokenSource != null)
            {
                throw new InvalidOperationException("The server is already started.");
            }

            _cancellationTokenSource = new CancellationTokenSource();
            _retainedMessagesManager = new MqttRetainedMessagesManager(_options, _logger);
            _clientSessionsManager   = new MqttClientSessionsManager(_options, _retainedMessagesManager, this, _logger);

            await _retainedMessagesManager.LoadMessagesAsync();

            foreach (var adapter in _adapters)
            {
                adapter.ClientAccepted += OnClientAccepted;
                await adapter.StartAsync(_options);
            }

            _logger.Info <MqttServer>("Started.");
            Started?.Invoke(this, new MqttServerStartedEventArgs());
        }
 public MqttClientSessionStatus(MqttClientSessionsManager sessionsManager, MqttClientSession session)
 {
     _sessionsManager = sessionsManager;
     _session         = session;
 }