Преглед изворни кода

Updated task methods.

Instead of awaiting most tasks, changed to simply returning the tasks.
release/3.x.x
Zazzmatazz пре 7 година
родитељ
комит
5c1eceb8a9
7 измењених фајлова са 52 додато и 55 уклоњено
  1. +4
    -4
      Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs
  2. +13
    -13
      Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs
  3. +8
    -8
      Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs
  4. +0
    -2
      Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs
  5. +6
    -7
      Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs
  6. +13
    -13
      Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs
  7. +8
    -8
      Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs

+ 4
- 4
Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs Прегледај датотеку

@@ -77,14 +77,14 @@ namespace MQTTnet
/// Asynchronously writes a sequence of bytes to the socket.
/// </summary>
/// <param name="buffer">The buffer to write data from.</param>
public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

try
{
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)
@@ -97,11 +97,11 @@ namespace MQTTnet
/// Asynchronously reads a sequence of bytes from the socket.
/// </summary>
/// <param name="buffer">The buffer to write the data into.</param>
public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)


+ 13
- 13
Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs Прегледај датотеку

@@ -30,27 +30,27 @@ namespace MQTTnet
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
_cert = cert ?? throw new ArgumentNullException(nameof(cert));

if (_socket.Connected)
{
NetworkStream ns = new NetworkStream(_socket, true);
_sslStream = new SslStream(ns);
}
if (!_socket.Connected)
return;
NetworkStream ns = new NetworkStream(_socket, true);
_sslStream = new SslStream(ns);
}

public async Task Authenticate()
public Task Authenticate()
{
await _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false);
return _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false);
}

/// <summary>
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
/// </summary>
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
public async Task ConnectAsync(MqttClientOptions options)
public Task ConnectAsync(MqttClientOptions options)
{
try
{
await _socket.ConnectAsync(options.Server, options.Port);
return _socket.ConnectAsync(options.Server, options.Port);
}
catch (SocketException exception)
{
@@ -78,14 +78,14 @@ namespace MQTTnet
/// Asynchronously writes a sequence of bytes to the socket.
/// </summary>
/// <param name="buffer">The buffer to write data from.</param>
public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

try
{
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)
@@ -98,11 +98,11 @@ namespace MQTTnet
/// Asynchronously reads a sequence of bytes from the socket.
/// </summary>
/// <param name="buffer">The buffer to write the data into.</param>
public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)


+ 8
- 8
Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs Прегледај датотеку

@@ -21,11 +21,11 @@ namespace MQTTnet
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
}

public async Task ConnectAsync(MqttClientOptions options)
public Task ConnectAsync(MqttClientOptions options)
{
try
{
await _socket.ConnectAsync(options.Server, options.Port);
return _socket.ConnectAsync(options.Server, options.Port);
}
catch (SocketException exception)
{
@@ -33,12 +33,12 @@ namespace MQTTnet
}
}

public async Task DisconnectAsync()
public Task DisconnectAsync()
{
try
{
_socket.Dispose();
await Task.FromResult(0);
return Task.FromResult(0);
}
catch (SocketException exception)
{
@@ -46,13 +46,13 @@ namespace MQTTnet
}
}

public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

try
{
await _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
return _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
}
catch (SocketException exception)
{
@@ -60,12 +60,12 @@ namespace MQTTnet
}
}

public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
var buffer2 = new ArraySegment<byte>(buffer);
await _socket.ReceiveAsync(buffer2, SocketFlags.None);
return _socket.ReceiveAsync(buffer2, SocketFlags.None);
}
catch (SocketException exception)
{


+ 0
- 2
Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs Прегледај датотеку

@@ -13,8 +13,6 @@ namespace MQTTnet
if (options == null) throw new ArgumentNullException(nameof(options));
return new MqttClient(options,
// The cast to IMqttCommunicationChannel is required... Roslyn is too stupid to
// figure out how to cast back to the base type
new MqttChannelCommunicationAdapter(options.UseSSL ? new MqttClientSslChannel() : (IMqttCommunicationChannel)new MqttTcpChannel(),
new DefaultMqttV311PacketSerializer()));
}


+ 6
- 7
Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs Прегледај датотеку

@@ -50,7 +50,6 @@ namespace MQTTnet
_sslStream = new SslStream(ns);

await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false);

}
catch (SocketException exception)
{
@@ -61,11 +60,11 @@ namespace MQTTnet
/// <summary>
/// Asynchronously disconnects the client from the server.
/// </summary>
public async Task DisconnectAsync()
public Task DisconnectAsync()
{
try
{
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
}
catch (SocketException exception)
{
@@ -77,14 +76,14 @@ namespace MQTTnet
/// Asynchronously writes a sequence of bytes to the socket.
/// </summary>
/// <param name="buffer">The buffer to write data from.</param>
public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

try
{
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)
@@ -97,11 +96,11 @@ namespace MQTTnet
/// Asynchronously reads a sequence of bytes from the socket.
/// </summary>
/// <param name="buffer">The buffer to write the data into.</param>
public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)


+ 13
- 13
Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs Прегледај датотеку

@@ -30,11 +30,11 @@ namespace MQTTnet
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
_cert = cert ?? throw new ArgumentNullException(nameof(cert));

if (_socket.Connected)
{
NetworkStream ns = new NetworkStream(_socket, true);
_sslStream = new SslStream(ns);
}
if (!_socket.Connected)
return;
NetworkStream ns = new NetworkStream(_socket, true);
_sslStream = new SslStream(ns);
}

public async Task Authenticate()
@@ -46,11 +46,11 @@ namespace MQTTnet
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
/// </summary>
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
public async Task ConnectAsync(MqttClientOptions options)
public Task ConnectAsync(MqttClientOptions options)
{
try
{
await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port,
return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port,
null);
}
catch (SocketException exception)
@@ -62,11 +62,11 @@ namespace MQTTnet
/// <summary>
/// Asynchronously disconnects the client from the server.
/// </summary>
public async Task DisconnectAsync()
public Task DisconnectAsync()
{
try
{
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
}
catch (SocketException exception)
{
@@ -78,14 +78,14 @@ namespace MQTTnet
/// Asynchronously writes a sequence of bytes to the socket.
/// </summary>
/// <param name="buffer">The buffer to write data from.</param>
public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));

try
{
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)
@@ -98,11 +98,11 @@ namespace MQTTnet
/// Asynchronously reads a sequence of bytes from the socket.
/// </summary>
/// <param name="buffer">The buffer to write the data into.</param>
public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
}
catch (Exception ex)
when (ex is SocketException || ex is IOException)


+ 8
- 8
Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs Прегледај датотеку

@@ -21,11 +21,11 @@ namespace MQTTnet
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
}

public async Task ConnectAsync(MqttClientOptions options)
public Task ConnectAsync(MqttClientOptions options)
{
try
{
await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null);
return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null);
}
catch (SocketException exception)
{
@@ -33,11 +33,11 @@ namespace MQTTnet
}
}

public async Task DisconnectAsync()
public Task DisconnectAsync()
{
try
{
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
}
catch (SocketException exception)
{
@@ -45,13 +45,13 @@ namespace MQTTnet
}
}

public async Task WriteAsync(byte[] buffer)
public Task WriteAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

try
{
await Task.Factory.FromAsync(
return Task.Factory.FromAsync(
// ReSharper disable once AssignNullToNotNullAttribute
_socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null),
_socket.EndSend);
@@ -62,11 +62,11 @@ namespace MQTTnet
}
}

public async Task ReadAsync(byte[] buffer)
public Task ReadAsync(byte[] buffer)
{
try
{
await Task.Factory.FromAsync(
return Task.Factory.FromAsync(
// ReSharper disable once AssignNullToNotNullAttribute
_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null),
_socket.EndReceive);


Loading…
Откажи
Сачувај