Merge branch 'develop' into master

This commit is contained in:
Vladimir Akopyan
2018-05-11 17:52:20 +02:00
committed by GitHub
75 changed files with 1884 additions and 1146 deletions
@@ -1,11 +1,11 @@
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Exceptions;
using MQTTnet.Internal;
using MQTTnet.Packets;
using MQTTnet.Protocol;
@@ -14,7 +14,7 @@ namespace MQTTnet.Server
public sealed class MqttClientPendingMessagesQueue : IDisposable
{
private readonly ConcurrentQueue<MqttBasePacket> _queue = new ConcurrentQueue<MqttBasePacket>();
private readonly SemaphoreSlim _queueWaitSemaphore = new SemaphoreSlim(0);
private readonly AsyncAutoResetEvent _queueAutoResetEvent = new AsyncAutoResetEvent();
private readonly IMqttServerOptions _options;
private readonly MqttClientSession _clientSession;
private readonly IMqttNetLogger _logger;
@@ -66,7 +66,7 @@ namespace MQTTnet.Server
if (packet == null) throw new ArgumentNullException(nameof(packet));
_queue.Enqueue(packet);
_queueWaitSemaphore.Release();
_queueAutoResetEvent.Set();
_logger.Verbose<MqttClientPendingMessagesQueue>("Enqueued packet (ClientId: {0}).", _clientSession.ClientId);
}
@@ -94,7 +94,7 @@ namespace MQTTnet.Server
MqttBasePacket packet = null;
try
{
await _queueWaitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
await _queueAutoResetEvent.WaitOneAsync(cancellationToken).ConfigureAwait(false);
if (!_queue.TryDequeue(out packet))
{
throw new InvalidOperationException(); // should not happen
@@ -105,7 +105,7 @@ namespace MQTTnet.Server
return;
}
await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new[] { packet }).ConfigureAwait(false);
await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, new[] { packet }, cancellationToken).ConfigureAwait(false);
_logger.Verbose<MqttClientPendingMessagesQueue>("Enqueued packet sent (ClientId: {0}).", _clientSession.ClientId);
}
@@ -132,21 +132,21 @@ namespace MQTTnet.Server
if (publishPacket.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
{
publishPacket.Dup = true;
_queue.Enqueue(packet);
_queueWaitSemaphore.Release();
Enqueue(publishPacket);
}
}
if (!cancellationToken.IsCancellationRequested)
{
await _clientSession.StopAsync().ConfigureAwait(false);
_clientSession.Stop(MqttClientDisconnectType.NotClean);
}
}
}
public void Dispose()
{
_queueWaitSemaphore?.Dispose();
_queueAutoResetEvent?.Dispose();
}
}
}