Refactor serializer locking and thread instances.

This commit is contained in:
Christian Kratky
2018-06-19 20:52:31 +02:00
parent 66048931a2
commit bc20850fba
17 changed files with 123 additions and 113 deletions
@@ -18,6 +18,8 @@ namespace MQTTnet.Adapter
private const uint ErrorOperationAborted = 0x800703E3;
private const int ReadBufferSize = 4096; // TODO: Move buffer size to config
private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
private readonly IMqttNetChildLogger _logger;
private readonly IMqttChannel _channel;
@@ -88,6 +90,7 @@ namespace MQTTnet.Adapter
public async Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken)
{
await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
_logger.Verbose("TX >>> {0}", packet);
@@ -107,6 +110,10 @@ namespace MQTTnet.Adapter
WrapException(exception);
}
finally
{
_writerSemaphore.Release();
}
}
public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)