Skip to content

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Apr 9, 2017
1 parent 8bfad43 commit 9816f12
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 136 deletions.
6 changes: 3 additions & 3 deletions Frameworks/MQTTnet.NetCoreApp/MQTTnet.NetCoreApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Product>MQTTnet</Product>
<Description>MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</Description>
<Copyright>Copyright © Christian Kratky 2016-2017</Copyright>
<Version>2.1.0.4</Version>
<AssemblyVersion>2.1.0.11</AssemblyVersion>
<FileVersion>2.1.0.11</FileVersion>
<Version>2.1.1.0</Version>
<AssemblyVersion>2.1.1.0</AssemblyVersion>
<FileVersion>2.1.1.0</FileVersion>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<AssemblyName>MQTTnet</AssemblyName>
<RootNamespace>MQTTnet</RootNamespace>
Expand Down
4 changes: 2 additions & 2 deletions Frameworks/MQTTnet.NetFramework/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("a480ef90-0eaa-4d9a-b271-47a9c47f6f7d")]
[assembly: AssemblyVersion("2.1.0.11")]
[assembly: AssemblyFileVersion("2.1.0.11")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.1.0.11")]
[assembly: AssemblyFileVersion("2.1.0.11")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
24 changes: 13 additions & 11 deletions MQTTnet.Core/Client/MqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public async Task DisconnectAsync()
await DisconnectInternalAsync();
}

public async Task<IList<MqttSubscribeResult>> SubscribeAsync(params TopicFilter[] topicFilters)
public Task<IList<MqttSubscribeResult>> SubscribeAsync(params TopicFilter[] topicFilters)
{
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));

return await SubscribeAsync(topicFilters.ToList());
return SubscribeAsync(topicFilters.ToList());
}

public async Task<IList<MqttSubscribeResult>> SubscribeAsync(IList<TopicFilter> topicFilters)
Expand All @@ -117,11 +117,11 @@ public async Task<IList<MqttSubscribeResult>> SubscribeAsync(IList<TopicFilter>
return topicFilters.Select((t, i) => new MqttSubscribeResult(t, response.SubscribeReturnCodes[i])).ToList();
}

public async Task Unsubscribe(params string[] topicFilters)
public Task Unsubscribe(params string[] topicFilters)
{
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));

await Unsubscribe(topicFilters.ToList());
return Unsubscribe(topicFilters.ToList());
}

public async Task Unsubscribe(IList<string> topicFilters)
Expand Down Expand Up @@ -274,14 +274,14 @@ private async Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket)
FireApplicationMessageReceivedEvent(originalPublishPacket);
}

private async Task SendAsync(MqttBasePacket packet)
private Task SendAsync(MqttBasePacket packet)
{
await _adapter.SendPacketAsync(packet, _options.DefaultCommunicationTimeout);
return _adapter.SendPacketAsync(packet, _options.DefaultCommunicationTimeout);
}

private async Task<TResponsePacket> SendAndReceiveAsync<TResponsePacket>(MqttBasePacket requestPacket) where TResponsePacket : MqttBasePacket
{
Func<MqttBasePacket, bool> responsePacketSelector = p =>
bool ResponsePacketSelector(MqttBasePacket p)
{
var p1 = p as TResponsePacket;
if (p1 == null)
Expand All @@ -301,10 +301,10 @@ private async Task<TResponsePacket> SendAndReceiveAsync<TResponsePacket>(MqttBas
}

return true;
};
}

await _adapter.SendPacketAsync(requestPacket, _options.DefaultCommunicationTimeout);
return (TResponsePacket)await _packetDispatcher.WaitForPacketAsync(responsePacketSelector, _options.DefaultCommunicationTimeout);
return (TResponsePacket)await _packetDispatcher.WaitForPacketAsync(ResponsePacketSelector, _options.DefaultCommunicationTimeout);
}

private ushort GetNewPacketIdentifier()
Expand All @@ -324,8 +324,9 @@ private async Task SendKeepAliveMessagesAsync(CancellationToken cancellationToke
await SendAndReceiveAsync<MqttPingRespPacket>(new MqttPingReqPacket());
}
}
catch (MqttCommunicationException)
catch (MqttCommunicationException exception)
{
MqttTrace.Warning(nameof(MqttClient), exception, "MQTT communication error while receiving packets.");
}
catch (Exception exception)
{
Expand All @@ -351,8 +352,9 @@ private async Task ReceivePackets(CancellationToken cancellationToken)
Task.Run(() => ProcessReceivedPacket(mqttPacket), cancellationToken).Forget();
}
}
catch (MqttCommunicationException)
catch (MqttCommunicationException exception)
{
MqttTrace.Warning(nameof(MqttClient), exception, "MQTT communication error while receiving packets.");
}
catch (Exception exception)
{
Expand Down
32 changes: 32 additions & 0 deletions MQTTnet.Core/Internal/AsyncAutoResetEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MQTTnet.Core.Internal
{
public class AsyncGate
{
private readonly Queue<TaskCompletionSource<bool>> _waitingTasks = new Queue<TaskCompletionSource<bool>>();

public Task WaitOneAsync()
{
var tcs = new TaskCompletionSource<bool>();
lock (_waitingTasks)
{
_waitingTasks.Enqueue(tcs);
}

return tcs.Task;
}

public void Set()
{
lock (_waitingTasks)
{
if (_waitingTasks.Count > 0)
{
_waitingTasks.Dequeue().SetResult(true);
}
}
}
}
}
6 changes: 3 additions & 3 deletions MQTTnet.Core/MQTTnet.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<Product>MQTTnet</Product>
<Company>Christian Kratky</Company>
<Authors>Christian Kratky</Authors>
<Version>2.1.0.11</Version>
<Version>2.1.1.0</Version>
<PackageId>MQTTnet.Core</PackageId>
<Copyright>Copyright © Christian Kratky 2016-2017</Copyright>
<PackageProjectUrl>https://github.com/chkr1011/MQTTnet</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png</PackageIconUrl>
<RepositoryUrl>https://github.com/chkr1011/MQTTnet</RepositoryUrl>
<PackageTags>MQTT MQTTClient MQTTServer MQTTBroker Broker</PackageTags>
<FileVersion>2.1.0.11</FileVersion>
<AssemblyVersion>2.1.0.11</AssemblyVersion>
<FileVersion>2.1.1.0</FileVersion>
<AssemblyVersion>2.1.1.0</AssemblyVersion>
<PackageLicenseUrl>https://github.com/chkr1011/MQTTnet/blob/master/LICENSE</PackageLicenseUrl>
</PropertyGroup>

Expand Down
20 changes: 13 additions & 7 deletions MQTTnet.Core/Serializer/ByteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@ namespace MQTTnet.Core.Serializer
{
public class ByteReader
{
private readonly int _source;
private int _index;
private readonly int _byte;

public ByteReader(byte @byte)
public ByteReader(int source)
{
_byte = @byte;
_source = source;
}

public bool Read()
{
if (_index >= 8)
{
throw new InvalidOperationException("End of the byte reached.");
throw new InvalidOperationException("End of byte reached.");
}

var result = ((1 << _index) & _byte) > 0;
var result = ((1 << _index) & _source) > 0;
_index++;

return result;
}

public byte Read(int count)
{
if (_index + count > 8)
{
throw new InvalidOperationException("End of byte will be reached.");
}

var result = 0;
for (var i = 0; i < count; i++)
{
if (Read())
if (((1 << _index) & _source) > 0)
{
result |= 1 << i;
}

_index++;
}

return (byte)result;
Expand Down
2 changes: 1 addition & 1 deletion MQTTnet.Core/Serializer/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ByteWriter

public byte Value => (byte)_byte;

public void Write(byte @byte, int count)
public void Write(int @byte, int count)
{
for (var i = 0; i < count; i++)
{
Expand Down
Loading

0 comments on commit 9816f12

Please sign in to comment.