Browse Source

Refactorings

release/3.x.x
Christian Kratky 7 years ago
parent
commit
b5fc6e3d12
7 changed files with 45 additions and 20 deletions
  1. +9
    -3
      Frameworks/MQTTnet.NetFramework/Implementations/MqttWebSocketChannel.cs
  2. +8
    -2
      Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs
  3. +9
    -3
      Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs
  4. +11
    -3
      MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs
  5. +2
    -2
      MQTTnet.Core/Internal/TaskExtensions.cs
  6. +6
    -6
      Tests/MQTTnet.Core.Tests/ExtensionTests.cs
  7. +0
    -1
      Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs

+ 9
- 3
Frameworks/MQTTnet.NetFramework/Implementations/MqttWebSocketChannel.cs View File

@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
public async Task ConnectAsync(MqttClientOptions options)
{
_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);
}

public Task DisconnectAsync()
public async Task DisconnectAsync()
{
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()


+ 8
- 2
Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs View File

@@ -23,10 +23,16 @@ namespace MQTTnet.Implementations
RawStream = new WebSocketStream(_webSocket);
}

public Task DisconnectAsync()
public async Task DisconnectAsync()
{
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()


+ 9
- 3
Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs View File

@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
public async Task ConnectAsync(MqttClientOptions options)
{
_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);
}

public Task DisconnectAsync()
public async Task DisconnectAsync()
{
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()


+ 11
- 3
MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs View File

@@ -30,7 +30,7 @@ namespace MQTTnet.Core.Adapter
{
try
{
await _channel.ConnectAsync(options).TimeoutAfter(timeout);
await _channel.ConnectAsync(options).TimeoutAfter(timeout).ConfigureAwait(false);
}
catch (MqttCommunicationTimedOutException)
{
@@ -50,7 +50,7 @@ namespace MQTTnet.Core.Adapter
{
try
{
await _channel.DisconnectAsync();
await _channel.DisconnectAsync().ConfigureAwait(false);
}
catch (MqttCommunicationTimedOutException)
{
@@ -82,7 +82,15 @@ namespace MQTTnet.Core.Adapter
}

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)
{


+ 2
- 2
MQTTnet.Core/Internal/TaskExtensions.cs View File

@@ -25,9 +25,9 @@ namespace MQTTnet.Core.Internal

try
{
#pragma warning disable 4014
#pragma warning disable 4014
task.ContinueWith(t =>
#pragma warning restore 4014
#pragma warning restore 4014
{
if (t.IsFaulted)
{


+ 6
- 6
Tests/MQTTnet.Core.Tests/ExtensionTests.cs View File

@@ -9,25 +9,25 @@ namespace MQTTnet.Core.Tests
[TestClass]
public class ExtensionTests
{
[ExpectedException(typeof( MqttCommunicationTimedOutException ) )]
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
[TestMethod]
public async Task TestTimeoutAfter()
{
await Task.Delay(TimeSpan.FromMilliseconds(500)).TimeoutAfter(TimeSpan.FromMilliseconds(100));
}

[ExpectedException(typeof( MqttCommunicationTimedOutException))]
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
[TestMethod]
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]
public async Task TestTimeoutAfterCompleteInTime()
{
var result = await Task.Delay( TimeSpan.FromMilliseconds( 100 ) ).ContinueWith( t => 5 ).TimeoutAfter( TimeSpan.FromMilliseconds( 500 ) );
Assert.AreEqual( 5, result );
var result = await Task.Delay(TimeSpan.FromMilliseconds(100)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(500));
Assert.AreEqual(5, result);
}
}
}

+ 0
- 1
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs View File

@@ -36,7 +36,6 @@ namespace MQTTnet.TestApp.UniversalWindows
var options = new MqttClientOptions
{
Server = Server.Text,
Port = 8080,
UserName = User.Text,
Password = Password.Text,
ClientId = ClientId.Text,


Loading…
Cancel
Save