Refactorings

This commit is contained in:
Christian Kratky
2017-09-16 17:06:47 +02:00
parent dc65bdb913
commit b5fc6e3d12
7 changed files with 45 additions and 20 deletions
@@ -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)
{ {
+2 -2
View File
@@ -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)
{ {
+6 -6
View File
@@ -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,