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; } }
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; }
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); }
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 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()); }
public MqttClientSessionsManager(IMqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, MqttServer server, IMqttNetLogger logger) { _server = server ?? throw new ArgumentNullException(nameof(server)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _options = options ?? throw new ArgumentNullException(nameof(options)); _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager)); }
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); }
public async Task SubscribeAsync(ICollection <TopicFilter> topicFilters, MqttRetainedMessagesManager retainedMessagesManager) { await SubscriptionsManager.SubscribeAsync(topicFilters).ConfigureAwait(false); var matchingRetainedMessages = await retainedMessagesManager.GetSubscribedMessagesAsync(topicFilters).ConfigureAwait(false); foreach (var matchingRetainedMessage in matchingRetainedMessages) { EnqueueApplicationMessage(matchingRetainedMessage, null, true); } }
public MqttClientSubscriptionsManager( MqttSession session, MqttServerEventContainer eventContainer, MqttRetainedMessagesManager retainedMessagesManager, ISubscriptionChangedNotification subscriptionChangedNotification) { _session = session ?? throw new ArgumentNullException(nameof(session)); _eventContainer = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer)); _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager)); _subscriptionChangedNotification = subscriptionChangedNotification; }
public MqttClientSessionsManager(IMqttServerOptions options, MqttServer server, MqttRetainedMessagesManager retainedMessagesManager, CancellationToken cancellationToken, IMqttNetChildLogger logger) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } _logger = logger.CreateChildLogger(nameof(MqttClientSessionsManager)); _cancellationToken = cancellationToken; _options = options ?? throw new ArgumentNullException(nameof(options)); Server = server ?? throw new ArgumentNullException(nameof(server)); _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager)); }
public MqttClientSession( string clientId, IMqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, IMqttNetLogger logger) { _options = options ?? throw new ArgumentNullException(nameof(options)); _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); ClientId = clientId; SubscriptionsManager = new MqttClientSubscriptionsManager(_options, clientId); PendingMessagesQueue = new MqttClientPendingMessagesQueue(_options, this, _logger); }
public MqttClientSessionsManager( MqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, MqttServerEventContainer eventContainer, IMqttNetLogger logger) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } _logger = logger.WithSource(nameof(MqttClientSessionsManager)); _rootLogger = logger; _options = options ?? throw new ArgumentNullException(nameof(options)); _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager)); _eventContainer = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer)); }
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); }
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()); }