mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 12:16:02 +08:00
Add proper state checks for server API.
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
* [RcpClient] Adjusted some namespaces (BREAKING CHANGE!)
|
* [RcpClient] Adjusted some namespaces (BREAKING CHANGE!)
|
||||||
* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!)
|
* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!)
|
||||||
* [Server] Adjusted some namespaces (BREAKING CHANGE!)
|
* [Server] Adjusted some namespaces (BREAKING CHANGE!)
|
||||||
|
* [Server] Added state checks (throw if not started etc.) for most server APIs.
|
||||||
</releaseNotes>
|
</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MQTTnet.Implementations;
|
||||||
|
|
||||||
namespace MQTTnet.Server
|
namespace MQTTnet.Server
|
||||||
{
|
{
|
||||||
@@ -76,24 +77,39 @@ namespace MQTTnet.Server
|
|||||||
|
|
||||||
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
|
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
|
||||||
{
|
{
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
return _clientSessionsManager.GetClientStatusAsync();
|
return _clientSessionsManager.GetClientStatusAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
|
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
|
||||||
{
|
{
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
return _clientSessionsManager.GetSessionStatusAsync();
|
return _clientSessionsManager.GetSessionStatusAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
|
public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
|
||||||
{
|
{
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
return _retainedMessagesManager.GetMessagesAsync();
|
return _retainedMessagesManager.GetMessagesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task ClearRetainedApplicationMessagesAsync()
|
||||||
|
{
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
|
return _retainedMessagesManager?.ClearMessagesAsync() ?? PlatformAbstractionLayer.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
|
public Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
|
||||||
{
|
{
|
||||||
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
||||||
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
||||||
|
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
return _clientSessionsManager.SubscribeAsync(clientId, topicFilters);
|
return _clientSessionsManager.SubscribeAsync(clientId, topicFilters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +118,8 @@ namespace MQTTnet.Server
|
|||||||
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
||||||
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
||||||
|
|
||||||
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
return _clientSessionsManager.UnsubscribeAsync(clientId, topicFilters);
|
return _clientSessionsManager.UnsubscribeAsync(clientId, topicFilters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +129,7 @@ namespace MQTTnet.Server
|
|||||||
|
|
||||||
MqttTopicValidator.ThrowIfInvalid(applicationMessage.Topic);
|
MqttTopicValidator.ThrowIfInvalid(applicationMessage.Topic);
|
||||||
|
|
||||||
if (_cancellationTokenSource == null) throw new InvalidOperationException("The server is not started.");
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
_clientSessionsManager.DispatchApplicationMessage(applicationMessage, null);
|
_clientSessionsManager.DispatchApplicationMessage(applicationMessage, null);
|
||||||
|
|
||||||
@@ -120,16 +138,15 @@ namespace MQTTnet.Server
|
|||||||
|
|
||||||
public async Task StartAsync(IMqttServerOptions options)
|
public async Task StartAsync(IMqttServerOptions options)
|
||||||
{
|
{
|
||||||
|
ThrowIfStarted();
|
||||||
|
|
||||||
Options = options ?? throw new ArgumentNullException(nameof(options));
|
Options = options ?? throw new ArgumentNullException(nameof(options));
|
||||||
|
|
||||||
if (Options.RetainedMessagesManager == null) throw new MqttConfigurationException("options.RetainedMessagesManager should not be null.");
|
|
||||||
|
|
||||||
if (_cancellationTokenSource != null) throw new InvalidOperationException("The server is already started.");
|
|
||||||
|
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
_retainedMessagesManager = Options.RetainedMessagesManager;
|
_retainedMessagesManager = Options.RetainedMessagesManager ?? throw new MqttConfigurationException("options.RetainedMessagesManager should not be null.");
|
||||||
await _retainedMessagesManager.Start(Options, _rootLogger);
|
|
||||||
|
await _retainedMessagesManager.Start(Options, _rootLogger).ConfigureAwait(false);
|
||||||
await _retainedMessagesManager.LoadMessagesAsync().ConfigureAwait(false);
|
await _retainedMessagesManager.LoadMessagesAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
_clientSessionsManager = new MqttClientSessionsManager(Options, _retainedMessagesManager, _cancellationTokenSource.Token, _eventDispatcher, _rootLogger);
|
_clientSessionsManager = new MqttClientSessionsManager(Options, _retainedMessagesManager, _cancellationTokenSource.Token, _eventDispatcher, _rootLogger);
|
||||||
@@ -189,14 +206,25 @@ namespace MQTTnet.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ClearRetainedApplicationMessagesAsync()
|
|
||||||
{
|
|
||||||
return _retainedMessagesManager?.ClearMessagesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
Task OnHandleClient(IMqttChannelAdapter channelAdapter)
|
Task OnHandleClient(IMqttChannelAdapter channelAdapter)
|
||||||
{
|
{
|
||||||
return _clientSessionsManager.HandleClientConnectionAsync(channelAdapter);
|
return _clientSessionsManager.HandleClientConnectionAsync(channelAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThrowIfStarted()
|
||||||
|
{
|
||||||
|
if (_cancellationTokenSource != null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("The MQTT server is already started.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThrowIfNotStarted()
|
||||||
|
{
|
||||||
|
if (_cancellationTokenSource == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("The MQTT server is not started.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user