mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Optimized exception handling and awaits.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Exceptions;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.WebSockets;
|
||||
@@ -11,7 +10,7 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
public sealed class MqttWebSocketChannel : IMqttCommunicationChannel, IDisposable
|
||||
{
|
||||
private ClientWebSocket _webSocket = new ClientWebSocket();
|
||||
private ClientWebSocket _webSocket;
|
||||
|
||||
public Stream RawStream { get; private set; }
|
||||
public Stream SendStream => RawStream;
|
||||
@@ -19,25 +18,15 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
_webSocket = null;
|
||||
|
||||
try
|
||||
{
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
catch (WebSocketException exception)
|
||||
{
|
||||
throw new MqttCommunicationException(exception);
|
||||
}
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
RawStream = null;
|
||||
return _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Exceptions;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.WebSockets;
|
||||
@@ -11,7 +10,7 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
public sealed class MqttWebSocketChannel : IMqttCommunicationChannel, IDisposable
|
||||
{
|
||||
private ClientWebSocket _webSocket = new ClientWebSocket();
|
||||
private ClientWebSocket _webSocket;
|
||||
|
||||
public Stream SendStream => RawStream;
|
||||
public Stream ReceiveStream => RawStream;
|
||||
@@ -19,25 +18,15 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
_webSocket = null;
|
||||
|
||||
try
|
||||
{
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
catch (WebSocketException exception)
|
||||
{
|
||||
throw new MqttCommunicationException(exception);
|
||||
}
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
RawStream = null;
|
||||
return _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -9,12 +9,26 @@ namespace MQTTnet.Implementations
|
||||
public class WebSocketStream : Stream
|
||||
{
|
||||
private readonly ClientWebSocket _webSocket;
|
||||
|
||||
|
||||
public WebSocketStream(ClientWebSocket webSocket)
|
||||
{
|
||||
_webSocket = webSocket;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public override long Length => throw new NotSupportedException();
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => throw new NotSupportedException();
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
@@ -52,21 +66,6 @@ namespace MQTTnet.Implementations
|
||||
WriteAsync(buffer, offset, count).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Exceptions;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.WebSockets;
|
||||
@@ -19,19 +18,9 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
_webSocket = null;
|
||||
|
||||
try
|
||||
{
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
catch (WebSocketException exception)
|
||||
{
|
||||
throw new MqttCommunicationException(exception);
|
||||
}
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -36,13 +36,14 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
Server = Server.Text,
|
||||
Port = 8080,
|
||||
UserName = User.Text,
|
||||
Password = Password.Text,
|
||||
ClientId = ClientId.Text
|
||||
ClientId = ClientId.Text,
|
||||
TlsOptions = { UseTls = UseTls.IsChecked == true },
|
||||
ConnectionType = MqttConnectionType.Ws
|
||||
};
|
||||
|
||||
options.TlsOptions.UseTls = UseTls.IsChecked == true;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
if (_mqttClient != null)
|
||||
|
||||
Reference in New Issue
Block a user