mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Updated task methods.
Instead of awaiting most tasks, changed to simply returning the tasks.
This commit is contained in:
@@ -77,14 +77,14 @@ namespace MQTTnet
|
|||||||
/// Asynchronously writes a sequence of bytes to the socket.
|
/// Asynchronously writes a sequence of bytes to the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write data from.</param>
|
/// <param name="buffer">The buffer to write data from.</param>
|
||||||
public async Task WriteAsync(byte[] buffer)
|
public Task WriteAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
if (buffer == null)
|
if (buffer == null)
|
||||||
throw new ArgumentNullException(nameof(buffer));
|
throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
@@ -97,11 +97,11 @@ namespace MQTTnet
|
|||||||
/// Asynchronously reads a sequence of bytes from the socket.
|
/// Asynchronously reads a sequence of bytes from the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write the data into.</param>
|
/// <param name="buffer">The buffer to write the data into.</param>
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
|
|||||||
@@ -30,27 +30,27 @@ namespace MQTTnet
|
|||||||
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
||||||
_cert = cert ?? throw new ArgumentNullException(nameof(cert));
|
_cert = cert ?? throw new ArgumentNullException(nameof(cert));
|
||||||
|
|
||||||
if (_socket.Connected)
|
if (!_socket.Connected)
|
||||||
{
|
return;
|
||||||
NetworkStream ns = new NetworkStream(_socket, true);
|
|
||||||
_sslStream = new SslStream(ns);
|
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>
|
/// <summary>
|
||||||
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
|
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
|
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
|
||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _socket.ConnectAsync(options.Server, options.Port);
|
return _socket.ConnectAsync(options.Server, options.Port);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -78,14 +78,14 @@ namespace MQTTnet
|
|||||||
/// Asynchronously writes a sequence of bytes to the socket.
|
/// Asynchronously writes a sequence of bytes to the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write data from.</param>
|
/// <param name="buffer">The buffer to write data from.</param>
|
||||||
public async Task WriteAsync(byte[] buffer)
|
public Task WriteAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
if (buffer == null)
|
if (buffer == null)
|
||||||
throw new ArgumentNullException(nameof(buffer));
|
throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
@@ -98,11 +98,11 @@ namespace MQTTnet
|
|||||||
/// Asynchronously reads a sequence of bytes from the socket.
|
/// Asynchronously reads a sequence of bytes from the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write the data into.</param>
|
/// <param name="buffer">The buffer to write the data into.</param>
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ namespace MQTTnet
|
|||||||
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _socket.ConnectAsync(options.Server, options.Port);
|
return _socket.ConnectAsync(options.Server, options.Port);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -33,12 +33,12 @@ namespace MQTTnet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DisconnectAsync()
|
public Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_socket.Dispose();
|
_socket.Dispose();
|
||||||
await Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
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));
|
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
|
return _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -60,12 +60,12 @@ namespace MQTTnet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var buffer2 = new ArraySegment<byte>(buffer);
|
var buffer2 = new ArraySegment<byte>(buffer);
|
||||||
await _socket.ReceiveAsync(buffer2, SocketFlags.None);
|
return _socket.ReceiveAsync(buffer2, SocketFlags.None);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ namespace MQTTnet
|
|||||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||||
|
|
||||||
return new MqttClient(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 MqttChannelCommunicationAdapter(options.UseSSL ? new MqttClientSslChannel() : (IMqttCommunicationChannel)new MqttTcpChannel(),
|
||||||
new DefaultMqttV311PacketSerializer()));
|
new DefaultMqttV311PacketSerializer()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ namespace MQTTnet
|
|||||||
_sslStream = new SslStream(ns);
|
_sslStream = new SslStream(ns);
|
||||||
|
|
||||||
await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false);
|
await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -61,11 +60,11 @@ namespace MQTTnet
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously disconnects the client from the server.
|
/// Asynchronously disconnects the client from the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task DisconnectAsync()
|
public Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -77,14 +76,14 @@ namespace MQTTnet
|
|||||||
/// Asynchronously writes a sequence of bytes to the socket.
|
/// Asynchronously writes a sequence of bytes to the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write data from.</param>
|
/// <param name="buffer">The buffer to write data from.</param>
|
||||||
public async Task WriteAsync(byte[] buffer)
|
public Task WriteAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
if (buffer == null)
|
if (buffer == null)
|
||||||
throw new ArgumentNullException(nameof(buffer));
|
throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
@@ -97,11 +96,11 @@ namespace MQTTnet
|
|||||||
/// Asynchronously reads a sequence of bytes from the socket.
|
/// Asynchronously reads a sequence of bytes from the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write the data into.</param>
|
/// <param name="buffer">The buffer to write the data into.</param>
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ namespace MQTTnet
|
|||||||
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
||||||
_cert = cert ?? throw new ArgumentNullException(nameof(cert));
|
_cert = cert ?? throw new ArgumentNullException(nameof(cert));
|
||||||
|
|
||||||
if (_socket.Connected)
|
if (!_socket.Connected)
|
||||||
{
|
return;
|
||||||
NetworkStream ns = new NetworkStream(_socket, true);
|
|
||||||
_sslStream = new SslStream(ns);
|
NetworkStream ns = new NetworkStream(_socket, true);
|
||||||
}
|
_sslStream = new SslStream(ns);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Authenticate()
|
public async Task Authenticate()
|
||||||
@@ -46,11 +46,11 @@ namespace MQTTnet
|
|||||||
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
|
/// Asynchronously connects to the client described in the <see cref="MqttClientOptions"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
|
/// <param name="options">The <see cref="MqttClientOptions"/> describing the connection.</param>
|
||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
try
|
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);
|
null);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
@@ -62,11 +62,11 @@ namespace MQTTnet
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously disconnects the client from the server.
|
/// Asynchronously disconnects the client from the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task DisconnectAsync()
|
public Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -78,14 +78,14 @@ namespace MQTTnet
|
|||||||
/// Asynchronously writes a sequence of bytes to the socket.
|
/// Asynchronously writes a sequence of bytes to the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write data from.</param>
|
/// <param name="buffer">The buffer to write data from.</param>
|
||||||
public async Task WriteAsync(byte[] buffer)
|
public Task WriteAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
if (buffer == null)
|
if (buffer == null)
|
||||||
throw new ArgumentNullException(nameof(buffer));
|
throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
return _sslStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
@@ -98,11 +98,11 @@ namespace MQTTnet
|
|||||||
/// Asynchronously reads a sequence of bytes from the socket.
|
/// Asynchronously reads a sequence of bytes from the socket.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="buffer">The buffer to write the data into.</param>
|
/// <param name="buffer">The buffer to write the data into.</param>
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
return _sslStream.ReadAsync(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
when (ex is SocketException || ex is IOException)
|
when (ex is SocketException || ex is IOException)
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ namespace MQTTnet
|
|||||||
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
try
|
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)
|
catch (SocketException exception)
|
||||||
{
|
{
|
||||||
@@ -33,11 +33,11 @@ namespace MQTTnet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DisconnectAsync()
|
public Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null);
|
||||||
}
|
}
|
||||||
catch (SocketException exception)
|
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));
|
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Factory.FromAsync(
|
return Task.Factory.FromAsync(
|
||||||
// ReSharper disable once AssignNullToNotNullAttribute
|
// ReSharper disable once AssignNullToNotNullAttribute
|
||||||
_socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null),
|
_socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null),
|
||||||
_socket.EndSend);
|
_socket.EndSend);
|
||||||
@@ -62,11 +62,11 @@ namespace MQTTnet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ReadAsync(byte[] buffer)
|
public Task ReadAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Factory.FromAsync(
|
return Task.Factory.FromAsync(
|
||||||
// ReSharper disable once AssignNullToNotNullAttribute
|
// ReSharper disable once AssignNullToNotNullAttribute
|
||||||
_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null),
|
_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null),
|
||||||
_socket.EndReceive);
|
_socket.EndReceive);
|
||||||
|
|||||||
Reference in New Issue
Block a user