Optimized exception handling and awaits.

This commit is contained in:
Christian Kratky
2017-09-15 15:58:48 +02:00
parent 76105de4c7
commit eda8bff10b
6 changed files with 70 additions and 71 deletions
@@ -26,11 +26,19 @@ namespace MQTTnet.Core.Adapter
public IMqttPacketSerializer PacketSerializer { get; }
public Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
{
try
{
return _channel.ConnectAsync(options).TimeoutAfter(timeout);
await _channel.ConnectAsync(options).TimeoutAfter(timeout);
}
catch (MqttCommunicationTimedOutException)
{
throw;
}
catch (MqttCommunicationException)
{
throw;
}
catch (Exception exception)
{
@@ -38,11 +46,19 @@ namespace MQTTnet.Core.Adapter
}
}
public Task DisconnectAsync()
public async Task DisconnectAsync()
{
try
{
return _channel.DisconnectAsync();
await _channel.DisconnectAsync();
}
catch (MqttCommunicationTimedOutException)
{
throw;
}
catch (MqttCommunicationException)
{
throw;
}
catch (Exception exception)
{
@@ -68,6 +84,14 @@ namespace MQTTnet.Core.Adapter
await _sendTask; // configure await false geneates stackoverflow
await _channel.SendStream.FlushAsync().TimeoutAfter(timeout).ConfigureAwait(false);
}
catch (MqttCommunicationTimedOutException)
{
throw;
}
catch (MqttCommunicationException)
{
throw;
}
catch (Exception exception)
{
throw new MqttCommunicationException(exception);
@@ -97,13 +121,21 @@ namespace MQTTnet.Core.Adapter
MqttTrace.Information(nameof(MqttChannelCommunicationAdapter), "RX <<< {0}", packet);
return packet;
}
catch (MqttCommunicationTimedOutException)
{
throw;
}
catch (MqttCommunicationException)
{
throw;
}
catch (Exception exception)
{
throw new MqttCommunicationException(exception);
}
}
private async Task<ReceivedMqttPacket> ReceiveAsync(Stream stream)
private static async Task<ReceivedMqttPacket> ReceiveAsync(Stream stream)
{
var header = MqttPacketReader.ReadHeaderFromSource(stream);