Ver a proveniência

Refactoring

release/3.x.x
Christian há 6 anos
ascendente
cometimento
ae11e31048
7 ficheiros alterados com 35 adições e 40 eliminações
  1. +1
    -0
      Build/MQTTnet.nuspec
  2. +11
    -14
      Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs
  3. +3
    -4
      Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.cs
  4. +5
    -5
      Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs
  5. +8
    -10
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs
  6. +5
    -5
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs
  7. +2
    -2
      Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs

+ 1
- 0
Build/MQTTnet.nuspec Ver ficheiro

@@ -11,6 +11,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description>
<releaseNotes> * [Client] Added new overloads for the message builder.
* [Core] Performance optimizations (thanks to @ israellot)
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2018</copyright>
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>


+ 11
- 14
Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs Ver ficheiro

@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
@@ -53,14 +50,13 @@ namespace MQTTnet.Adapter

public async Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket[] packets)
{
for(var i=0;i<packets.Length;i++)
foreach (var packet in packets)
{
await SendPacketsAsync(timeout, cancellationToken, packets[i]).ConfigureAwait(false);
await SendPacketsAsync(timeout, cancellationToken, packet).ConfigureAwait(false);
}

}

public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket packet)
private Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket packet)
{
ThrowIfDisposed();

@@ -75,21 +71,23 @@ namespace MQTTnet.Adapter
{
return;
}

_logger.Verbose<MqttChannelAdapter>("TX >>> {0} [Timeout={1}]", packet, timeout);

var packetData = PacketSerializer.Serialize(packet);

if (cancellationToken.IsCancellationRequested)
{
return;
}

await _channel.SendStream.WriteAsync(
packetData.Array,
packetData.Offset,
(int)packetData.Count,
packetData.Count,
cancellationToken).ConfigureAwait(false);

await _channel.SendStream.FlushAsync(cancellationToken).ConfigureAwait(false);
});
}

@@ -113,12 +111,11 @@ namespace MQTTnet.Adapter
{
receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, linkedCts.Token).ConfigureAwait(false);
}
catch(OperationCanceledException ex)
catch (OperationCanceledException ex)
{
//check if timed out
if(linkedCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
var timedOut = linkedCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested;
if (timedOut)
{
//only timeout token was cancelled
throw new MqttCommunicationTimedOutException(ex);
}
else


+ 3
- 4
Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.cs Ver ficheiro

@@ -14,7 +14,7 @@ namespace MQTTnet.Implementations
{
public sealed class MqttTcpChannel : IMqttChannel
{
#if NET452 || NET461
#if NET452 || NET461 || NETSTANDARD2_0
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
public static int BufferSize { get; set; } = 4096 * 20; // Can be changed for fine tuning by library user.
@@ -60,7 +60,6 @@ namespace MQTTnet.Implementations
if (_socket == null)
{
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
}

#if NET452 || NET461
@@ -212,8 +211,8 @@ namespace MQTTnet.Implementations
{
stream = new NetworkStream(_socket, true);
}
#if NET452 || NET461
#if NET452 || NET461 || NETSTANDARD2_0
SendStream = new BufferedStream(stream, _bufferSize);
ReceiveStream = new BufferedStream(stream, _bufferSize);
#else


+ 5
- 5
Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs Ver ficheiro

@@ -38,8 +38,7 @@ namespace MQTTnet.Implementations

if (options.DefaultEndpointOptions.IsEnabled)
{
_defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
_defaultEndpointSocket.NoDelay = true;
_defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };

_defaultEndpointSocket.Bind(new IPEndPoint(options.DefaultEndpointOptions.BoundIPAddress, options.GetDefaultEndpointPort()));
_defaultEndpointSocket.Listen(options.ConnectionBacklog);
@@ -80,7 +79,7 @@ namespace MQTTnet.Implementations
_defaultEndpointSocket = null;

_tlsCertificate = null;
_tlsEndpointSocket?.Dispose();
_tlsEndpointSocket = null;

@@ -104,7 +103,8 @@ namespace MQTTnet.Implementations
#else
var clientSocket = await _defaultEndpointSocket.AcceptAsync().ConfigureAwait(false);
#endif
clientSocket.NoDelay=true;
clientSocket.NoDelay = true;

var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(clientSocket, null), new MqttPacketSerializer(), _logger);
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
}
@@ -139,7 +139,7 @@ namespace MQTTnet.Implementations

var sslStream = new SslStream(new NetworkStream(clientSocket));
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false);
var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer(), _logger);
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
}


+ 8
- 10
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs Ver ficheiro

@@ -2,7 +2,6 @@
using MQTTnet.Packets;
using MQTTnet.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
@@ -23,29 +22,28 @@ namespace MQTTnet.Serializer
using (var stream = new MemoryStream(128))
using (var writer = new MqttPacketWriter(stream))
{
//leave enough head space for max header (fixed + 4 variable remaining lenght)
// Leave enough head space for max header size (fixed + 4 variable remaining length)
stream.Position = 5;

var fixedHeader = SerializePacket(packet, writer);

var remainingLength = MqttPacketWriter.GetRemainingLength((int)stream.Length-5);
var remainingLength = MqttPacketWriter.GetRemainingLength((int)stream.Length - 5);

var headerSize = remainingLength.Length + 1;
var headerOffset = 5 - headerSize;

//position curson on correct offset on beginining of array
// Position cursor on correct offset on beginining of array (has leading 0x0)
stream.Position = headerOffset;
//write header

writer.Write(fixedHeader);
writer.Write(remainingLength,0,remainingLength.Length);
writer.Write(remainingLength, 0, remainingLength.Length);
#if NET461 || NET452 || NETSTANDARD2_0
var buffer = stream.GetBuffer();
#else
var buffer = stream.ToArray();
#endif
return new ArraySegment<byte>(buffer, headerOffset, (int)stream.Length- headerOffset);
return new ArraySegment<byte>(buffer, headerOffset, (int)stream.Length - headerOffset);
}
}

@@ -300,7 +298,7 @@ namespace MQTTnet.Serializer

return packet;
}
private static void ValidateConnectPacket(MqttConnectPacket packet)
{
if (packet == null) throw new ArgumentNullException(nameof(packet));


+ 5
- 5
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs Ver ficheiro

@@ -60,11 +60,11 @@ namespace MQTTnet.Serializer
{
if (length <= 0)
{
return new [] { (byte)0 };
return new[] { (byte)0 };
}

var bytes = new byte[4];
int arraySize = 0;
var offset = 0;

// Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html.
var x = length;
@@ -77,12 +77,12 @@ namespace MQTTnet.Serializer
encodedByte = encodedByte | 128;
}

bytes[arraySize] = (byte)encodedByte;
bytes[offset] = (byte)encodedByte;

arraySize++;
offset++;
} while (x > 0);

return bytes.Take(arraySize).ToArray();
return bytes.Take(offset).ToArray();
}
}
}

+ 2
- 2
Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs Ver ficheiro

@@ -44,7 +44,7 @@ namespace MQTTnet.TestApp.NetCore

try
{
await client.ConnectAsync(options);
await client.ConnectAsync(options).ConfigureAwait(false);
}
catch (Exception exception)
{
@@ -165,7 +165,7 @@ namespace MQTTnet.TestApp.NetCore
Console.WriteLine("Press any key to exit.");
Console.ReadLine();

await mqttServer.StopAsync();
await mqttServer.StopAsync().ConfigureAwait(false);
}
catch (Exception e)
{


Carregando…
Cancelar
Guardar