Refactoring

This commit is contained in:
Christian Kratky
2018-07-26 20:02:11 +02:00
parent 2ced921008
commit 61b324043a
2 changed files with 14 additions and 5 deletions
+2
View File
@@ -13,9 +13,11 @@
<releaseNotes>* [Core] Performance optimizations. <releaseNotes>* [Core] Performance optimizations.
* [Client] Added support for proxies when using web socket connections (thanks to PitySOFT). * [Client] Added support for proxies when using web socket connections (thanks to PitySOFT).
* [Client] Refactored TLS parameter usage and added more parameters (thanks to PitySOFT). * [Client] Refactored TLS parameter usage and added more parameters (thanks to PitySOFT).
* [Client] Fixed a deadlock when clients are disconnecting (thanks to @JanEggers).
* [Server] Fixed an issue in client keep alive checks (thanks to @jenscski). * [Server] Fixed an issue in client keep alive checks (thanks to @jenscski).
* [Server] Changed the order of _ClientConnected_ and _ClientDisconnected_ events so that _ClientConnected_ is fired at first (thanks to @jenscski). * [Server] Changed the order of _ClientConnected_ and _ClientDisconnected_ events so that _ClientConnected_ is fired at first (thanks to @jenscski).
* [Server] Fixed an iussue which lets the server stop processing messages when using the application message interceptor (thanks to @alamsor). * [Server] Fixed an iussue which lets the server stop processing messages when using the application message interceptor (thanks to @alamsor).
* [Server] Fixed a deadlock when clients are disconnecting (thanks to @JanEggers).
</releaseNotes> </releaseNotes>
<copyright>Copyright Christian Kratky 2016-2018</copyright> <copyright>Copyright Christian Kratky 2016-2018</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>
+12 -5
View File
@@ -81,13 +81,16 @@ namespace MQTTnet.Server
try try
{ {
_adapter = adapter; _adapter = adapter;
adapter.ReadingPacketStarted += OnAdapterReadingPacketStarted; adapter.ReadingPacketStarted += OnAdapterReadingPacketStarted;
adapter.ReadingPacketCompleted += OnAdapterReadingPacketCompleted; adapter.ReadingPacketCompleted += OnAdapterReadingPacketCompleted;
_cancellationTokenSource = new CancellationTokenSource(); _cancellationTokenSource = new CancellationTokenSource();
//woraround for https://github.com/dotnet/corefx/issues/24430 //woraround for https://github.com/dotnet/corefx/issues/24430
_cleanupHandle = _cancellationTokenSource.Token.Register(() => Cleanup()); #pragma warning disable 4014
_cleanupHandle = _cancellationTokenSource.Token.Register(() => CleanupAsync());
#pragma warning restore 4014
//endworkaround //endworkaround
_wasCleanDisconnect = false; _wasCleanDisconnect = false;
@@ -131,7 +134,7 @@ namespace MQTTnet.Server
} }
finally finally
{ {
await Cleanup().ConfigureAwait(false); await CleanupAsync().ConfigureAwait(false);
_cleanupHandle?.Dispose(); _cleanupHandle?.Dispose();
_cleanupHandle = null; _cleanupHandle = null;
@@ -141,11 +144,11 @@ namespace MQTTnet.Server
} }
} }
private async Task Cleanup() private async Task CleanupAsync()
{ {
var adapter = _adapter;
try try
{ {
var adapter = _adapter;
if (adapter == null) if (adapter == null)
{ {
return; return;
@@ -155,13 +158,17 @@ namespace MQTTnet.Server
adapter.ReadingPacketStarted -= OnAdapterReadingPacketStarted; adapter.ReadingPacketStarted -= OnAdapterReadingPacketStarted;
adapter.ReadingPacketCompleted -= OnAdapterReadingPacketCompleted; adapter.ReadingPacketCompleted -= OnAdapterReadingPacketCompleted;
await adapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false); await adapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false);
adapter.Dispose();
} }
catch (Exception exception) catch (Exception exception)
{ {
_logger.Error(exception, exception.Message); _logger.Error(exception, exception.Message);
} }
finally
{
adapter?.Dispose();
}
} }
public void Stop(MqttClientDisconnectType type) public void Stop(MqttClientDisconnectType type)