mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 10:26:11 +08:00
Additional performance improvements
This commit is contained in:
@@ -85,7 +85,7 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
try
|
||||
{
|
||||
var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null);
|
||||
var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null).ConfigureAwait(false);
|
||||
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, null), new MqttPacketSerializer());
|
||||
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter));
|
||||
}
|
||||
@@ -94,7 +94,7 @@ namespace MQTTnet.Implementations
|
||||
MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint.");
|
||||
|
||||
//excessive CPU consumed if in endless loop of socket errors
|
||||
Thread.Sleep(TimeSpan.FromSeconds(1));
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,10 +105,10 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
try
|
||||
{
|
||||
var clientSocket = await Task.Factory.FromAsync(_tlsEndpointSocket.BeginAccept, _tlsEndpointSocket.EndAccept, null);
|
||||
var clientSocket = await Task.Factory.FromAsync(_tlsEndpointSocket.BeginAccept, _tlsEndpointSocket.EndAccept, null).ConfigureAwait(false);
|
||||
|
||||
var sslStream = new SslStream(new NetworkStream(clientSocket));
|
||||
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false);
|
||||
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false);
|
||||
|
||||
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer());
|
||||
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter));
|
||||
@@ -118,7 +118,7 @@ namespace MQTTnet.Implementations
|
||||
MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at TLS endpoint.");
|
||||
|
||||
//excessive CPU consumed if in endless loop of socket errors
|
||||
Thread.Sleep(TimeSpan.FromSeconds(1));
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace MQTTnet.Implementations
|
||||
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
|
||||
}
|
||||
|
||||
await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null);
|
||||
await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null).ConfigureAwait(false);
|
||||
|
||||
if (options.TlsOptions.UseTls)
|
||||
{
|
||||
@@ -99,12 +99,11 @@ namespace MQTTnet.Implementations
|
||||
|
||||
try
|
||||
{
|
||||
int totalBytes = 0;
|
||||
var totalBytes = 0;
|
||||
|
||||
do
|
||||
{
|
||||
var read = await _dataStream.ReadAsync(buffer, totalBytes, buffer.Length - totalBytes).ConfigureAwait(false);
|
||||
|
||||
if (read == 0)
|
||||
{
|
||||
throw new MqttCommunicationException(new SocketException((int)SocketError.Disconnecting));
|
||||
|
||||
@@ -17,15 +17,14 @@ namespace MQTTnet.Implementations
|
||||
private int WebSocketBufferSize;
|
||||
private int WebSocketBufferOffset;
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
public Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
_webSocket = null;
|
||||
|
||||
try
|
||||
{
|
||||
_webSocket = new ClientWebSocket();
|
||||
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
return _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
||||
}
|
||||
catch (WebSocketException exception)
|
||||
{
|
||||
@@ -33,9 +32,9 @@ namespace MQTTnet.Implementations
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
return _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -62,7 +61,7 @@ namespace MQTTnet.Implementations
|
||||
WebSocketReceiveResult response;
|
||||
do
|
||||
{
|
||||
response = await _webSocket.ReceiveAsync(new ArraySegment<byte>(temporaryBuffer), CancellationToken.None);
|
||||
response = await _webSocket.ReceiveAsync(new ArraySegment<byte>(temporaryBuffer), CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
temporaryBuffer.CopyTo(WebSocketBuffer, offset);
|
||||
offset += response.Count;
|
||||
@@ -72,7 +71,7 @@ namespace MQTTnet.Implementations
|
||||
WebSocketBufferSize = response.Count;
|
||||
if (response.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Buffer.BlockCopy(WebSocketBuffer, 0, buffer, 0, buffer.Length);
|
||||
|
||||
Reference in New Issue
Block a user