Refactor web socket usage

This commit is contained in:
Christian Kratky
2017-09-18 18:20:36 +02:00
parent b5fc6e3d12
commit 32473af7c2
24 changed files with 116 additions and 86 deletions
@@ -9,14 +9,14 @@ namespace MQTTnet.Core.Adapter
{
public interface IMqttCommunicationAdapter
{
Task ConnectAsync(MqttClientOptions options, TimeSpan timeout);
IMqttPacketSerializer PacketSerializer { get; }
Task DisconnectAsync();
Task ConnectAsync(TimeSpan timeout, MqttClientOptions options);
Task DisconnectAsync(TimeSpan timeout);
Task SendPacketsAsync(TimeSpan timeout, IEnumerable<MqttBasePacket> packets);
Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout);
IMqttPacketSerializer PacketSerializer { get; }
}
}
@@ -26,11 +26,11 @@ namespace MQTTnet.Core.Adapter
public IMqttPacketSerializer PacketSerializer { get; }
public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
public async Task ConnectAsync(TimeSpan timeout, MqttClientOptions options)
{
try
{
await _channel.ConnectAsync(options).TimeoutAfter(timeout).ConfigureAwait(false);
await _channel.ConnectAsync(options).TimeoutAfter(timeout);
}
catch (MqttCommunicationTimedOutException)
{
@@ -46,11 +46,11 @@ namespace MQTTnet.Core.Adapter
}
}
public async Task DisconnectAsync()
public async Task DisconnectAsync(TimeSpan timeout)
{
try
{
await _channel.DisconnectAsync().ConfigureAwait(false);
await _channel.DisconnectAsync().TimeoutAfter(timeout).ConfigureAwait(false);
}
catch (MqttCommunicationTimedOutException)
{
@@ -113,7 +113,7 @@ namespace MQTTnet.Core.Adapter
ReceivedMqttPacket receivedMqttPacket;
if (timeout > TimeSpan.Zero)
{
receivedMqttPacket = await ReceiveAsync(_channel.RawStream).TimeoutAfter(timeout).ConfigureAwait(false);
receivedMqttPacket = await ReceiveAsync(_channel.RawReceiveStream).TimeoutAfter(timeout).ConfigureAwait(false);
}
else
{
-7
View File
@@ -1,7 +0,0 @@
namespace MQTTnet.Core.Channel
{
public static class BufferConstants
{
public const int Size = 4096 * 20;
}
}
@@ -6,14 +6,11 @@ namespace MQTTnet.Core.Channel
{
public interface IMqttCommunicationChannel
{
Task ConnectAsync(MqttClientOptions options);
Task DisconnectAsync();
Stream SendStream { get; }
Stream ReceiveStream { get; }
Stream RawReceiveStream { get; }
Stream RawStream { get; }
Task ConnectAsync(MqttClientOptions options);
Task DisconnectAsync();
}
}
+2 -2
View File
@@ -53,7 +53,7 @@ namespace MQTTnet.Core.Client
{
_disconnectedEventSuspended = false;
await _adapter.ConnectAsync(_options, _options.DefaultCommunicationTimeout).ConfigureAwait(false);
await _adapter.ConnectAsync(_options.DefaultCommunicationTimeout, _options).ConfigureAwait(false);
MqttTrace.Verbose(nameof(MqttClient), "Connection with server established.");
@@ -220,7 +220,7 @@ namespace MQTTnet.Core.Client
{
try
{
await _adapter.DisconnectAsync();
await _adapter.DisconnectAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false);
}
catch (Exception exception)
{
+1 -3
View File
@@ -3,8 +3,6 @@
public enum MqttConnectionType
{
Tcp,
Tls,
Ws,
Wss
Ws
}
}
@@ -56,7 +56,7 @@ namespace MQTTnet.Core.Server
{
if (_pendingPublishPackets.Count == 0)
{
await Task.Delay(TimeSpan.FromMilliseconds(5)).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellationToken).ConfigureAwait(false);
continue;
}
@@ -71,7 +71,7 @@ namespace MQTTnet.Core.Server
foreach (var publishPacket in packets)
{
publishPacket.Dup = true;
_pendingPublishPackets.Add(publishPacket);
_pendingPublishPackets.Add(publishPacket, cancellationToken);
}
}
catch (Exception exception)
@@ -80,7 +80,7 @@ namespace MQTTnet.Core.Server
foreach (var publishPacket in packets)
{
publishPacket.Dup = true;
_pendingPublishPackets.Add(publishPacket);
_pendingPublishPackets.Add(publishPacket, cancellationToken);
}
}
}
@@ -64,7 +64,7 @@ namespace MQTTnet.Core.Server
}
finally
{
await eventArgs.ClientAdapter.DisconnectAsync().ConfigureAwait(false);
await eventArgs.ClientAdapter.DisconnectAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false);
}
}