Skip to content

Commit

Permalink
Performance refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Jul 28, 2017
1 parent e021d5a commit 0b0239b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 92 deletions.
4 changes: 2 additions & 2 deletions MQTTnet.Core/Serializer/ByteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public bool Read()
return result;
}

public byte Read(int count)
public int Read(int count)
{
if (_index + count > 8)
{
Expand All @@ -42,7 +42,7 @@ public byte Read(int count)
_index++;
}

return (byte)result;
return result;
}
}
}
102 changes: 45 additions & 57 deletions MQTTnet.Core/Serializer/DefaultMqttV311PacketSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,86 +16,72 @@ public Task SerializeAsync(MqttBasePacket packet, IMqttCommunicationChannel dest
if (packet == null) throw new ArgumentNullException(nameof(packet));
if (destination == null) throw new ArgumentNullException(nameof(destination));

var connectPacket = packet as MqttConnectPacket;
if (connectPacket != null)
if (packet is MqttConnectPacket connectPacket)
{
return SerializeAsync(connectPacket, destination);
}

var connAckPacket = packet as MqttConnAckPacket;
if (connAckPacket != null)
if (packet is MqttConnAckPacket connAckPacket)
{
return SerializeAsync(connAckPacket, destination);
}

var disconnectPacket = packet as MqttDisconnectPacket;
if (disconnectPacket != null)
if (packet is MqttDisconnectPacket disconnectPacket)
{
return SerializeAsync(disconnectPacket, destination);
}

var pingReqPacket = packet as MqttPingReqPacket;
if (pingReqPacket != null)
if (packet is MqttPingReqPacket pingReqPacket)
{
return SerializeAsync(pingReqPacket, destination);
}

var pingRespPacket = packet as MqttPingRespPacket;
if (pingRespPacket != null)
if (packet is MqttPingRespPacket pingRespPacket)
{
return SerializeAsync(pingRespPacket, destination);
}

var publishPacket = packet as MqttPublishPacket;
if (publishPacket != null)
if (packet is MqttPublishPacket publishPacket)
{
return SerializeAsync(publishPacket, destination);
}

var pubAckPacket = packet as MqttPubAckPacket;
if (pubAckPacket != null)
if (packet is MqttPubAckPacket pubAckPacket)
{
return SerializeAsync(pubAckPacket, destination);
}

var pubRecPacket = packet as MqttPubRecPacket;
if (pubRecPacket != null)
if (packet is MqttPubRecPacket pubRecPacket)
{
return SerializeAsync(pubRecPacket, destination);
}

var pubRelPacket = packet as MqttPubRelPacket;
if (pubRelPacket != null)
if (packet is MqttPubRelPacket pubRelPacket)
{
return SerializeAsync(pubRelPacket, destination);
}

var pubCompPacket = packet as MqttPubCompPacket;
if (pubCompPacket != null)
if (packet is MqttPubCompPacket pubCompPacket)
{
return SerializeAsync(pubCompPacket, destination);
}

var subscribePacket = packet as MqttSubscribePacket;
if (subscribePacket != null)
if (packet is MqttSubscribePacket subscribePacket)
{
return SerializeAsync(subscribePacket, destination);
}

var subAckPacket = packet as MqttSubAckPacket;
if (subAckPacket != null)
if (packet is MqttSubAckPacket subAckPacket)
{
return SerializeAsync(subAckPacket, destination);
}

var unsubscribePacket = packet as MqttUnsubscribePacket;
if (unsubscribePacket != null)
if (packet is MqttUnsubscribePacket unsubscribePacket)
{
return SerializeAsync(unsubscribePacket, destination);
}

var unsubAckPacket = packet as MqttUnsubAckPacket;
if (unsubAckPacket != null)
if (packet is MqttUnsubAckPacket unsubAckPacket)
{
return SerializeAsync(unsubAckPacket, destination);
}
Expand Down Expand Up @@ -206,7 +192,7 @@ public async Task<MqttBasePacket> DeserializeAsync(IMqttCommunicationChannel sou
}
}

private async Task<MqttBasePacket> DeserializeUnsubscribeAsync(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializeUnsubscribeAsync(MqttPacketReader reader)
{
var packet = new MqttUnsubscribePacket
{
Expand All @@ -221,7 +207,7 @@ private async Task<MqttBasePacket> DeserializeUnsubscribeAsync(MqttPacketReader
return packet;
}

private async Task<MqttBasePacket> DeserializeSubscribeAsync(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializeSubscribeAsync(MqttPacketReader reader)
{
var packet = new MqttSubscribePacket
{
Expand All @@ -238,7 +224,7 @@ await reader.ReadRemainingDataStringWithLengthPrefixAsync(),
return packet;
}

private async Task<MqttBasePacket> DeserializePublishAsync(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializePublishAsync(MqttPacketReader reader)
{
var fixedHeader = new ByteReader(reader.FixedHeader);
var retain = fixedHeader.Read();
Expand Down Expand Up @@ -266,13 +252,10 @@ private async Task<MqttBasePacket> DeserializePublishAsync(MqttPacketReader read
return packet;
}

private async Task<MqttBasePacket> DeserializeConnectAsync(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializeConnectAsync(MqttPacketReader reader)
{
var packet = new MqttConnectPacket();

await reader.ReadRemainingDataByteAsync();
await reader.ReadRemainingDataByteAsync();

await reader.ReadRemainingDataAsync(2); // Skip 2 bytes

var protocolName = await reader.ReadRemainingDataAsync(4);

if (Encoding.UTF8.GetString(protocolName, 0, protocolName.Length) != "MQTT")
Expand All @@ -285,7 +268,12 @@ private async Task<MqttBasePacket> DeserializeConnectAsync(MqttPacketReader read

var connectFlagsReader = new ByteReader(connectFlags);
connectFlagsReader.Read(); // Reserved.
packet.CleanSession = connectFlagsReader.Read();

var packet = new MqttConnectPacket
{
CleanSession = connectFlagsReader.Read()
};

var willFlag = connectFlagsReader.Read();
var willQoS = connectFlagsReader.Read(2);
var willRetain = connectFlagsReader.Read();
Expand Down Expand Up @@ -318,7 +306,7 @@ await reader.ReadRemainingDataWithLengthPrefixAsync(),
return packet;
}

private async Task<MqttBasePacket> DeserializeSubAck(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializeSubAck(MqttPacketReader reader)
{
var packet = new MqttSubAckPacket
{
Expand All @@ -333,7 +321,7 @@ private async Task<MqttBasePacket> DeserializeSubAck(MqttPacketReader reader)
return packet;
}

private async Task<MqttBasePacket> DeserializeConnAck(MqttPacketReader reader)
private static async Task<MqttBasePacket> DeserializeConnAck(MqttPacketReader reader)
{
var variableHeader1 = await reader.ReadRemainingDataByteAsync();
var variableHeader2 = await reader.ReadRemainingDataByteAsync();
Expand All @@ -347,15 +335,15 @@ private async Task<MqttBasePacket> DeserializeConnAck(MqttPacketReader reader)
return packet;
}

private void ValidateConnectPacket(MqttConnectPacket packet)
private static void ValidateConnectPacket(MqttConnectPacket packet)
{
if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession)
{
throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7].");
}
}

private void ValidatePublishPacket(MqttPublishPacket packet)
private static void ValidatePublishPacket(MqttPublishPacket packet)
{
if (packet.QualityOfServiceLevel == 0 && packet.Dup)
{
Expand All @@ -365,7 +353,7 @@ private void ValidatePublishPacket(MqttPublishPacket packet)

private static readonly byte[] MqttPrefix = Encoding.UTF8.GetBytes("MQTT");

private Task SerializeAsync(MqttConnectPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttConnectPacket packet, IMqttCommunicationChannel destination)
{
ValidateConnectPacket(packet);

Expand Down Expand Up @@ -420,7 +408,7 @@ private Task SerializeAsync(MqttConnectPacket packet, IMqttCommunicationChannel
}
}

private Task SerializeAsync(MqttConnAckPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttConnAckPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -435,22 +423,22 @@ private Task SerializeAsync(MqttConnAckPacket packet, IMqttCommunicationChannel
}
}

private Task SerializeAsync(MqttDisconnectPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttDisconnectPacket packet, IMqttCommunicationChannel destination)
{
return SerializeEmptyPacketAsync(MqttControlPacketType.Disconnect, destination);
}

private Task SerializeAsync(MqttPingReqPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPingReqPacket packet, IMqttCommunicationChannel destination)
{
return SerializeEmptyPacketAsync(MqttControlPacketType.PingReq, destination);
}

private Task SerializeAsync(MqttPingRespPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPingRespPacket packet, IMqttCommunicationChannel destination)
{
return SerializeEmptyPacketAsync(MqttControlPacketType.PingResp, destination);
}

private Task SerializeAsync(MqttPublishPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPublishPacket packet, IMqttCommunicationChannel destination)
{
ValidatePublishPacket(packet);

Expand Down Expand Up @@ -485,7 +473,7 @@ private Task SerializeAsync(MqttPublishPacket packet, IMqttCommunicationChannel
}
}

private Task SerializeAsync(MqttPubAckPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPubAckPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -496,7 +484,7 @@ private Task SerializeAsync(MqttPubAckPacket packet, IMqttCommunicationChannel d
}
}

private Task SerializeAsync(MqttPubRecPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPubRecPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -507,7 +495,7 @@ private Task SerializeAsync(MqttPubRecPacket packet, IMqttCommunicationChannel d
}
}

private async Task SerializeAsync(MqttPubRelPacket packet, IMqttCommunicationChannel destination)
private static async Task SerializeAsync(MqttPubRelPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -518,7 +506,7 @@ private async Task SerializeAsync(MqttPubRelPacket packet, IMqttCommunicationCha
}
}

private Task SerializeAsync(MqttPubCompPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttPubCompPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -529,7 +517,7 @@ private Task SerializeAsync(MqttPubCompPacket packet, IMqttCommunicationChannel
}
}

private Task SerializeAsync(MqttSubscribePacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttSubscribePacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -549,7 +537,7 @@ private Task SerializeAsync(MqttSubscribePacket packet, IMqttCommunicationChanne
}
}

private Task SerializeAsync(MqttSubAckPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttSubAckPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -568,7 +556,7 @@ private Task SerializeAsync(MqttSubAckPacket packet, IMqttCommunicationChannel d
}
}

private Task SerializeAsync(MqttUnsubscribePacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttUnsubscribePacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -587,7 +575,7 @@ private Task SerializeAsync(MqttUnsubscribePacket packet, IMqttCommunicationChan
}
}

private Task SerializeAsync(MqttUnsubAckPacket packet, IMqttCommunicationChannel destination)
private static Task SerializeAsync(MqttUnsubAckPacket packet, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand All @@ -598,7 +586,7 @@ private Task SerializeAsync(MqttUnsubAckPacket packet, IMqttCommunicationChannel
}
}

private Task SerializeEmptyPacketAsync(MqttControlPacketType type, IMqttCommunicationChannel destination)
private static Task SerializeEmptyPacketAsync(MqttControlPacketType type, IMqttCommunicationChannel destination)
{
using (var output = new MqttPacketWriter())
{
Expand Down
Loading

0 comments on commit 0b0239b

Please sign in to comment.