Преглед на файлове

event is triggered but something is blocking

release/3.x.x
JanEggers преди 6 години
родител
ревизия
b55f5ff298
променени са 6 файла, в които са добавени 45 реда и са изтрити 24 реда
  1. +5
    -2
      Source/MQTTnet/Client/MqttClient.cs
  2. +15
    -8
      Source/MQTTnet/Implementations/MqttTcpChannel.cs
  3. +20
    -1
      Source/MQTTnet/Server/MqttClientSession.cs
  4. +0
    -10
      Source/MQTTnet/Server/MqttClientSessionsManager.cs
  5. +2
    -2
      Source/MQTTnet/Server/MqttServer.cs
  6. +3
    -1
      Tests/MQTTnet.Core.Tests/MqttServerTests.cs

+ 5
- 2
Source/MQTTnet/Client/MqttClient.cs Целия файл

@@ -222,8 +222,11 @@ namespace MQTTnet.Client

try
{
await WaitForTaskAsync(_packetReceiverTask, sender).ConfigureAwait(false);
await WaitForTaskAsync(_keepAliveMessageSenderTask, sender).ConfigureAwait(false);
if (!(exception is MqttCommunicationClosedGracefullyException))
{
await WaitForTaskAsync(_packetReceiverTask, sender).ConfigureAwait(false);
await WaitForTaskAsync(_keepAliveMessageSenderTask, sender).ConfigureAwait(false);
}

if (_adapter != null)
{


+ 15
- 8
Source/MQTTnet/Implementations/MqttTcpChannel.cs Целия файл

@@ -87,8 +87,14 @@ namespace MQTTnet.Implementations

public void Dispose()
{
TryDispose(_stream, () => _stream = null);
TryDispose(_socket, () => _socket = null);
Cleanup(ref _stream, (s) => s.Dispose());
Cleanup(ref _socket, (s) => {
if (s.Connected)
{
s.Shutdown(SocketShutdown.Both);
}
s.Dispose();
});
}

private bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
@@ -157,11 +163,16 @@ namespace MQTTnet.Implementations
}
}

private static void TryDispose(IDisposable disposable, Action afterDispose)
private static void Cleanup<T>(ref T item, Action<T> handler) where T : class
{
var temp = item;
item = null;
try
{
disposable?.Dispose();
if (temp != null)
{
handler(temp);
}
}
catch (ObjectDisposedException)
{
@@ -169,10 +180,6 @@ namespace MQTTnet.Implementations
catch (NullReferenceException)
{
}
finally
{
afterDispose();
}
}
}
}


+ 20
- 1
Source/MQTTnet/Server/MqttClientSession.cs Целия файл

@@ -29,6 +29,7 @@ namespace MQTTnet.Server
private MqttApplicationMessage _willMessage;
private bool _wasCleanDisconnect;
private IMqttChannelAdapter _adapter;
private Task<bool> _run;

public MqttClientSession(
string clientId,
@@ -65,7 +66,13 @@ namespace MQTTnet.Server
status.LastNonKeepAlivePacketReceived = _keepAliveMonitor.LastNonKeepAlivePacketReceived;
}

public async Task<bool> RunAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter)
public Task<bool> RunAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter)
{
_run = RunInternalAsync(connectPacket, adapter);
return _run;
}

private async Task<bool> RunInternalAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter)
{
if (connectPacket == null) throw new ArgumentNullException(nameof(connectPacket));
if (adapter == null) throw new ArgumentNullException(nameof(adapter));
@@ -122,6 +129,16 @@ namespace MQTTnet.Server
{
_adapter.ReadingPacketStarted -= OnAdapterReadingPacketStarted;
_adapter.ReadingPacketCompleted -= OnAdapterReadingPacketCompleted;

try
{
await _adapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false);
_adapter.Dispose();
}
catch (Exception exception)
{
_logger.Error(exception, exception.Message);
}
}

_adapter = null;
@@ -153,6 +170,8 @@ namespace MQTTnet.Server
}

_willMessage = null;

_run?.GetAwaiter().GetResult();
}
finally
{


+ 0
- 10
Source/MQTTnet/Server/MqttClientSessionsManager.cs Целия файл

@@ -249,16 +249,6 @@ namespace MQTTnet.Server
}
finally
{
try
{
await clientAdapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false);
clientAdapter.Dispose();
}
catch (Exception exception)
{
_logger.Error(exception, exception.Message);
}

if (!_options.EnablePersistentSessions)
{
DeleteSession(clientId);


+ 2
- 2
Source/MQTTnet/Server/MqttServer.cs Целия файл

@@ -106,14 +106,14 @@ namespace MQTTnet.Server

_cancellationTokenSource.Cancel(false);
_clientSessionsManager.Stop();

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

_clientSessionsManager.Stop();

_logger.Info("Stopped.");
Stopped?.Invoke(this, EventArgs.Empty);
}


+ 3
- 1
Tests/MQTTnet.Core.Tests/MqttServerTests.cs Целия файл

@@ -253,9 +253,11 @@ namespace MQTTnet.Core.Tests

await c1.ConnectAsync(clientOptions);

await Task.Delay(500);

await s.StopAsync();

await Task.Delay(500);
await Task.Delay(1000);

Assert.IsTrue(disconnectCalled);
}


Зареждане…
Отказ
Запис