mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-24 14:43:04 +08:00
Refactorings
This commit is contained in:
@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
|
|||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public async Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None).ConfigureAwait(false);
|
||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -23,10 +23,16 @@ namespace MQTTnet.Implementations
|
|||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
|
|||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public async Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None).ConfigureAwait(false);
|
||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace MQTTnet.Core.Adapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _channel.ConnectAsync(options).TimeoutAfter(timeout);
|
await _channel.ConnectAsync(options).TimeoutAfter(timeout).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
@@ -50,7 +50,7 @@ namespace MQTTnet.Core.Adapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _channel.DisconnectAsync();
|
await _channel.DisconnectAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
@@ -82,7 +82,15 @@ namespace MQTTnet.Core.Adapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _sendTask; // configure await false geneates stackoverflow
|
await _sendTask; // configure await false geneates stackoverflow
|
||||||
await _channel.SendStream.FlushAsync().TimeoutAfter(timeout).ConfigureAwait(false);
|
|
||||||
|
if (timeout > TimeSpan.Zero)
|
||||||
|
{
|
||||||
|
await _channel.SendStream.FlushAsync().TimeoutAfter(timeout).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await _channel.SendStream.FlushAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ namespace MQTTnet.Core.Internal
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#pragma warning disable 4014
|
#pragma warning disable 4014
|
||||||
task.ContinueWith(t =>
|
task.ContinueWith(t =>
|
||||||
#pragma warning restore 4014
|
#pragma warning restore 4014
|
||||||
{
|
{
|
||||||
if (t.IsFaulted)
|
if (t.IsFaulted)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,25 +9,25 @@ namespace MQTTnet.Core.Tests
|
|||||||
[TestClass]
|
[TestClass]
|
||||||
public class ExtensionTests
|
public class ExtensionTests
|
||||||
{
|
{
|
||||||
[ExpectedException(typeof( MqttCommunicationTimedOutException ) )]
|
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task TestTimeoutAfter()
|
public async Task TestTimeoutAfter()
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromMilliseconds(500)).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
await Task.Delay(TimeSpan.FromMilliseconds(500)).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
[ExpectedException(typeof( MqttCommunicationTimedOutException))]
|
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task TestTimeoutAfterWithResult()
|
public async Task TestTimeoutAfterWithResult()
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
await Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task TestTimeoutAfterCompleteInTime()
|
public async Task TestTimeoutAfterCompleteInTime()
|
||||||
{
|
{
|
||||||
var result = await Task.Delay( TimeSpan.FromMilliseconds( 100 ) ).ContinueWith( t => 5 ).TimeoutAfter( TimeSpan.FromMilliseconds( 500 ) );
|
var result = await Task.Delay(TimeSpan.FromMilliseconds(100)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(500));
|
||||||
Assert.AreEqual( 5, result );
|
Assert.AreEqual(5, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
var options = new MqttClientOptions
|
var options = new MqttClientOptions
|
||||||
{
|
{
|
||||||
Server = Server.Text,
|
Server = Server.Text,
|
||||||
Port = 8080,
|
|
||||||
UserName = User.Text,
|
UserName = User.Text,
|
||||||
Password = Password.Text,
|
Password = Password.Text,
|
||||||
ClientId = ClientId.Text,
|
ClientId = ClientId.Text,
|
||||||
|
|||||||
Reference in New Issue
Block a user