Fix server adapter memory leak and UnitTests

This commit is contained in:
Christian Kratky
2020-07-25 23:34:30 +02:00
parent 5056843611
commit 4e6aeb91a4
11 changed files with 125 additions and 92 deletions
+7 -7
View File
@@ -58,9 +58,9 @@ namespace MQTTnet.Adapter
public async Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
try
{
if (timeout == TimeSpan.Zero)
@@ -85,9 +85,9 @@ namespace MQTTnet.Adapter
public async Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
try
{
if (timeout == TimeSpan.Zero)
@@ -113,9 +113,9 @@ namespace MQTTnet.Adapter
public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken)
{
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
try
{
await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
@@ -169,8 +169,8 @@ namespace MQTTnet.Adapter
public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
try
{
@@ -1,7 +1,6 @@
#if !WINDOWS_UWP
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Internal;
using MQTTnet.Server;
using System;
using System.Collections.Generic;
@@ -13,7 +12,7 @@ using System.Threading.Tasks;
namespace MQTTnet.Implementations
{
public sealed class MqttTcpServerAdapter : Disposable, IMqttServerAdapter
public sealed class MqttTcpServerAdapter : IMqttServerAdapter
{
readonly List<MqttTcpServerListener> _listeners = new List<MqttTcpServerListener>();
readonly IMqttNetScopedLogger _logger;
@@ -67,14 +66,9 @@ namespace MQTTnet.Implementations
return PlatformAbstractionLayer.CompletedTask;
}
protected override void Dispose(bool disposing)
public void Dispose()
{
if (disposing)
{
Cleanup();
}
base.Dispose(disposing);
Cleanup();
}
void Cleanup()
+3 -2
View File
@@ -1,10 +1,11 @@
using MQTTnet.Server.Status;
using System;
using MQTTnet.Server.Status;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MQTTnet.Server
{
public interface IMqttServer : IApplicationMessageReceiver, IApplicationMessagePublisher
public interface IMqttServer : IApplicationMessageReceiver, IApplicationMessagePublisher, IDisposable
{
bool IsStarted { get; }
IMqttServerStartedHandler StartedHandler { get; set; }
+2 -2
View File
@@ -51,14 +51,14 @@ namespace MQTTnet.Server
var checkSubscriptionsResult = SubscriptionsManager.CheckSubscriptions(applicationMessage.Topic, applicationMessage.QualityOfServiceLevel);
if (!checkSubscriptionsResult.IsSubscribed)
{
return true;
return false;
}
_logger.Verbose("Queued application message with topic '{0}' (ClientId: {1}).", applicationMessage.Topic, ClientId);
ApplicationMessagesQueue.Enqueue(applicationMessage, senderClientId, checkSubscriptionsResult.QualityOfServiceLevel, isRetainedApplicationMessage);
return false;
return true;
}
public async Task SubscribeAsync(ICollection<MqttTopicFilter> topicFilters)
+30 -5
View File
@@ -11,10 +11,11 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Implementations;
using MQTTnet.Internal;
namespace MQTTnet.Server
{
public class MqttServer : IMqttServer
public class MqttServer : Disposable, IMqttServer
{
readonly MqttServerEventDispatcher _eventDispatcher;
readonly ICollection<IMqttServerAdapter> _adapters;
@@ -77,6 +78,7 @@ namespace MQTTnet.Server
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
{
ThrowIfDisposed();
ThrowIfNotStarted();
return _clientSessionsManager.GetClientStatusAsync();
@@ -84,6 +86,7 @@ namespace MQTTnet.Server
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
{
ThrowIfDisposed();
ThrowIfNotStarted();
return _clientSessionsManager.GetSessionStatusAsync();
@@ -91,6 +94,7 @@ namespace MQTTnet.Server
public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
{
ThrowIfDisposed();
ThrowIfNotStarted();
return _retainedMessagesManager.GetMessagesAsync();
@@ -98,6 +102,7 @@ namespace MQTTnet.Server
public Task ClearRetainedApplicationMessagesAsync()
{
ThrowIfDisposed();
ThrowIfNotStarted();
return _retainedMessagesManager?.ClearMessagesAsync() ?? PlatformAbstractionLayer.CompletedTask;
@@ -107,7 +112,8 @@ namespace MQTTnet.Server
{
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
ThrowIfDisposed();
ThrowIfNotStarted();
return _clientSessionsManager.SubscribeAsync(clientId, topicFilters);
@@ -118,6 +124,7 @@ namespace MQTTnet.Server
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
ThrowIfDisposed();
ThrowIfNotStarted();
return _clientSessionsManager.UnsubscribeAsync(clientId, topicFilters);
@@ -127,6 +134,8 @@ namespace MQTTnet.Server
{
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
ThrowIfDisposed();
MqttTopicValidator.ThrowIfInvalid(applicationMessage.Topic);
ThrowIfNotStarted();
@@ -138,6 +147,7 @@ namespace MQTTnet.Server
public async Task StartAsync(IMqttServerOptions options)
{
ThrowIfDisposed();
ThrowIfStarted();
Options = options ?? throw new ArgumentNullException(nameof(options));
@@ -190,13 +200,13 @@ namespace MQTTnet.Server
}
finally
{
_clientSessionsManager?.Dispose();
_clientSessionsManager = null;
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
_retainedMessagesManager = null;
_clientSessionsManager?.Dispose();
_clientSessionsManager = null;
}
var stoppedHandler = StoppedHandler;
@@ -206,6 +216,21 @@ namespace MQTTnet.Server
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
StopAsync().GetAwaiter().GetResult();
foreach (var adapter in _adapters)
{
adapter.Dispose();
}
}
base.Dispose(disposing);
}
Task OnHandleClient(IMqttChannelAdapter channelAdapter)
{
return _clientSessionsManager.HandleClientConnectionAsync(channelAdapter);
@@ -13,7 +13,7 @@ namespace MQTTnet.Server
public class MqttServerOptionsBuilder
{
readonly MqttServerOptions _options = new MqttServerOptions();
public MqttServerOptionsBuilder WithConnectionBacklog(int value)
{
_options.DefaultEndpointOptions.ConnectionBacklog = value;
@@ -193,6 +193,18 @@ namespace MQTTnet.Server
return this;
}
public MqttServerOptionsBuilder WithDefaultEndpointReuseAddress()
{
_options.DefaultEndpointOptions.ReuseAddress = true;
return this;
}
public MqttServerOptionsBuilder WithTlsEndpointReuseAddress()
{
_options.TlsEndpointOptions.ReuseAddress = true;
return this;
}
public MqttServerOptionsBuilder WithPersistentSessions()
{
_options.EnablePersistentSessions = true;