diff --git a/Source/MQTTnet/Packets/MqttConnectPacket.cs b/Source/MQTTnet/Packets/MqttConnectPacket.cs index 7c0b7a00..ec1b953a 100644 --- a/Source/MQTTnet/Packets/MqttConnectPacket.cs +++ b/Source/MQTTnet/Packets/MqttConnectPacket.cs @@ -24,7 +24,7 @@ namespace MQTTnet.Packets public MqttApplicationMessage WillMessage { get; set; } - public List Properties { get; set; } + public List Properties { get; set; } public override string ToString() { diff --git a/Source/MQTTnet/Packets/Properties/AssignedClientIdentifierProperty.cs b/Source/MQTTnet/Packets/Properties/AssignedClientIdentifierProperty.cs new file mode 100644 index 00000000..b7fd3cd7 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/AssignedClientIdentifierProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class AssignedClientIdentifierProperty : StringProperty + { + public AssignedClientIdentifierProperty(string value) + : base((byte)PropertyID.AssignedClientIdentifer, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/AuthenticationDataProperty.cs b/Source/MQTTnet/Packets/Properties/AuthenticationDataProperty.cs new file mode 100644 index 00000000..021d9e94 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/AuthenticationDataProperty.cs @@ -0,0 +1,12 @@ +using System; + +namespace MQTTnet.Packets.Properties +{ + public class AuthenticationDataProperty : BinaryDataProperty + { + public AuthenticationDataProperty(ArraySegment data) + : base((byte)PropertyID.AuthenticationData, data) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/AuthenticationMethodProperty.cs b/Source/MQTTnet/Packets/Properties/AuthenticationMethodProperty.cs new file mode 100644 index 00000000..7de63c3d --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/AuthenticationMethodProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class AuthenticationMethodProperty : StringProperty + { + public AuthenticationMethodProperty(string value) + : base((byte)PropertyID.AuthenticationMethod, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/BaseTypes/BinaryDataProperty.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/BinaryDataProperty.cs new file mode 100644 index 00000000..2037b8ef --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/BinaryDataProperty.cs @@ -0,0 +1,31 @@ +using System; +using MQTTnet.Serializer; + +namespace MQTTnet.Packets.Properties +{ + public class BinaryDataProperty : IProperty + { + public BinaryDataProperty(byte id, ArraySegment data) + { + if (data.Array == null) + { + throw new ArgumentNullException(nameof(data)); + } + + Id = id; + Data = data; + } + + public byte Id { get; } + + public ArraySegment Data { get; } + + public void WriteTo(MqttPacketWriter writer) + { + if (writer == null) throw new ArgumentNullException(nameof(writer)); + + writer.Write((ushort)Data.Count); + writer.Write(Data); + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/ByteValue.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/ByteProperty.cs similarity index 72% rename from Source/MQTTnet/Packets/Properties/ByteValue.cs rename to Source/MQTTnet/Packets/Properties/BaseTypes/ByteProperty.cs index 5c34cb90..e3e41210 100644 --- a/Source/MQTTnet/Packets/Properties/ByteValue.cs +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/ByteProperty.cs @@ -3,13 +3,16 @@ using MQTTnet.Serializer; namespace MQTTnet.Packets.Properties { - public class ByteValue : IPropertyValue + public class ByteProperty : IProperty { - public ByteValue(byte value) + public ByteProperty(byte id, byte value) { + Id = id; Value = value; } + public byte Id { get; } + public byte Value { get; } public void WriteTo(MqttPacketWriter writer) diff --git a/Source/MQTTnet/Packets/Properties/BaseTypes/FourByteIntegerProperty.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/FourByteIntegerProperty.cs new file mode 100644 index 00000000..56aafe34 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/FourByteIntegerProperty.cs @@ -0,0 +1,27 @@ +using System; +using MQTTnet.Serializer; + +namespace MQTTnet.Packets.Properties.BaseTypes +{ + public class FourByteIntegerValue : IProperty + { + public FourByteIntegerValue(byte id, uint value) + { + Id = id; + Value = value; + } + + public byte Id { get; } + + public uint Value { get; } + + public void WriteTo(MqttPacketWriter writer) + { + if (writer == null) throw new ArgumentNullException(nameof(writer)); + + // TODO: Check if order must be reversed like for ushort. + var bytes = BitConverter.GetBytes(Value); + writer.Write(bytes, 0, bytes.Length); + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/StringValue.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/StringProperty.cs similarity index 71% rename from Source/MQTTnet/Packets/Properties/StringValue.cs rename to Source/MQTTnet/Packets/Properties/BaseTypes/StringProperty.cs index fc614ad1..b7910698 100644 --- a/Source/MQTTnet/Packets/Properties/StringValue.cs +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/StringProperty.cs @@ -3,13 +3,16 @@ using MQTTnet.Serializer; namespace MQTTnet.Packets.Properties { - public class StringValue : IPropertyValue + public class StringProperty : IProperty { - public StringValue(string value) + public StringProperty(byte id, string value) { + Id = id; Value = value; } + public byte Id { get; } + public string Value { get; } public void WriteTo(MqttPacketWriter writer) diff --git a/Source/MQTTnet/Packets/Properties/TwoByteIntegerValue.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/TwoByteIntegerProperty.cs similarity index 54% rename from Source/MQTTnet/Packets/Properties/TwoByteIntegerValue.cs rename to Source/MQTTnet/Packets/Properties/BaseTypes/TwoByteIntegerProperty.cs index 79fc919c..4ed67766 100644 --- a/Source/MQTTnet/Packets/Properties/TwoByteIntegerValue.cs +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/TwoByteIntegerProperty.cs @@ -1,22 +1,25 @@ using System; using MQTTnet.Serializer; -namespace MQTTnet.Packets.Properties +namespace MQTTnet.Packets.Properties.BaseTypes { - public class TwoByteIntegerValue : IPropertyValue + public class TwoByteIntegerProperty : IProperty { - public TwoByteIntegerValue(ushort value) + public TwoByteIntegerProperty(byte id, ushort value) { + Id = id; Value = value; } + public byte Id { get; } + public ushort Value { get; } public void WriteTo(MqttPacketWriter writer) { if (writer == null) throw new ArgumentNullException(nameof(writer)); - throw new NotImplementedException(); + writer.Write(Value); } } } diff --git a/Source/MQTTnet/Packets/Properties/BaseTypes/VariableByteIntegerProperty.cs b/Source/MQTTnet/Packets/Properties/BaseTypes/VariableByteIntegerProperty.cs new file mode 100644 index 00000000..26eef480 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/BaseTypes/VariableByteIntegerProperty.cs @@ -0,0 +1,26 @@ +using System; +using MQTTnet.Serializer; + +namespace MQTTnet.Packets.Properties.BaseTypes +{ + public class VariableByteIntegerProperty : IProperty + { + public VariableByteIntegerProperty(byte id, uint value) + { + Id = id; + Value = value; + } + + public byte Id { get; } + + public uint Value { get; } + + public void WriteTo(MqttPacketWriter writer) + { + if (writer == null) throw new ArgumentNullException(nameof(writer)); + + var buffer = MqttPacketWriter.EncodeVariableByteInteger(Value); + writer.Write(buffer); + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/ContentTypeProperty.cs b/Source/MQTTnet/Packets/Properties/ContentTypeProperty.cs new file mode 100644 index 00000000..1e7ec422 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/ContentTypeProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class ContentTypeProperty : StringProperty + { + public ContentTypeProperty(string value) + : base((byte)PropertyID.ContentType, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/CorrelationDataProperty.cs b/Source/MQTTnet/Packets/Properties/CorrelationDataProperty.cs new file mode 100644 index 00000000..f9104e8c --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/CorrelationDataProperty.cs @@ -0,0 +1,12 @@ +using System; + +namespace MQTTnet.Packets.Properties +{ + public class CorrelationDataProperty : BinaryDataProperty + { + public CorrelationDataProperty(ArraySegment data) + : base((byte)PropertyID.CorrelationData, data) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/IPropertyValue.cs b/Source/MQTTnet/Packets/Properties/IProperty.cs similarity index 69% rename from Source/MQTTnet/Packets/Properties/IPropertyValue.cs rename to Source/MQTTnet/Packets/Properties/IProperty.cs index 005134fc..739e0cd4 100644 --- a/Source/MQTTnet/Packets/Properties/IPropertyValue.cs +++ b/Source/MQTTnet/Packets/Properties/IProperty.cs @@ -2,8 +2,10 @@ namespace MQTTnet.Packets.Properties { - public interface IPropertyValue + public interface IProperty { + byte Id { get; } + void WriteTo(MqttPacketWriter writer); } } diff --git a/Source/MQTTnet/Packets/Properties/MessageExpiryIntervalProperty.cs b/Source/MQTTnet/Packets/Properties/MessageExpiryIntervalProperty.cs new file mode 100644 index 00000000..7062e8b1 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/MessageExpiryIntervalProperty.cs @@ -0,0 +1,12 @@ +using MQTTnet.Packets.Properties.BaseTypes; + +namespace MQTTnet.Packets.Properties +{ + public class MessageExpiryIntervalProperty : FourByteIntegerValue + { + public MessageExpiryInterval(uint value) + : base((byte)PropertyID.MessageExpiryInterval, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/PayloadFormatIndicatorProperty.cs b/Source/MQTTnet/Packets/Properties/PayloadFormatIndicatorProperty.cs new file mode 100644 index 00000000..f30f5b16 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/PayloadFormatIndicatorProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class PayloadFormatIndicatorProperty : ByteProperty + { + public PayloadFormatIndicatorProperty(byte value) + : base((byte)PropertyID.PayloadFormatIndicator, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/Property.cs b/Source/MQTTnet/Packets/Properties/Property.cs deleted file mode 100644 index 1fc71d6d..00000000 --- a/Source/MQTTnet/Packets/Properties/Property.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MQTTnet.Packets.Properties -{ - public class Property - { - public PropertyType Type { get; set; } - - public IPropertyValue Value { get; set; } - } -} diff --git a/Source/MQTTnet/Packets/Properties/PropertyType.cs b/Source/MQTTnet/Packets/Properties/PropertyID.cs similarity index 97% rename from Source/MQTTnet/Packets/Properties/PropertyType.cs rename to Source/MQTTnet/Packets/Properties/PropertyID.cs index 8d60aad7..69a809ea 100644 --- a/Source/MQTTnet/Packets/Properties/PropertyType.cs +++ b/Source/MQTTnet/Packets/Properties/PropertyID.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Packets.Properties { - public enum PropertyType + public enum PropertyID { PayloadFormatIndicator = 1, MessageExpiryInterval = 2, diff --git a/Source/MQTTnet/Packets/Properties/RequestProblemInformationProperty.cs b/Source/MQTTnet/Packets/Properties/RequestProblemInformationProperty.cs new file mode 100644 index 00000000..456c9733 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/RequestProblemInformationProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class RequestProblemInformationProperty : ByteProperty + { + public RequestProblemInformationProperty(byte value) + : base((byte)PropertyID.RequestProblemInformation, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/RequestResponseInformationProperty.cs b/Source/MQTTnet/Packets/Properties/RequestResponseInformationProperty.cs new file mode 100644 index 00000000..9119675b --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/RequestResponseInformationProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class RequestResponseInformationProperty : ByteProperty + { + public RequestResponseInformationProperty(byte value) + : base((byte)PropertyID.RequestResponseInformation, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/ResponseTopicProperty.cs b/Source/MQTTnet/Packets/Properties/ResponseTopicProperty.cs new file mode 100644 index 00000000..ad41bf85 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/ResponseTopicProperty.cs @@ -0,0 +1,10 @@ +namespace MQTTnet.Packets.Properties +{ + public class ResponseTopicProperty : StringProperty + { + public ResponseTopicProperty(string value) + : base((byte)PropertyID.ResponseTopic, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/ServerKeepAliveProperty.cs b/Source/MQTTnet/Packets/Properties/ServerKeepAliveProperty.cs new file mode 100644 index 00000000..a247fb47 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/ServerKeepAliveProperty.cs @@ -0,0 +1,12 @@ +using MQTTnet.Packets.Properties.BaseTypes; + +namespace MQTTnet.Packets.Properties +{ + public class ServerKeepAliveProperty : TwoByteIntegerProperty + { + public ServerKeepAliveProperty(ushort value) + : base((byte)PropertyID.ServerKeepAlive, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/SessionExpiryIntervalProperty.cs b/Source/MQTTnet/Packets/Properties/SessionExpiryIntervalProperty.cs new file mode 100644 index 00000000..f28422e3 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/SessionExpiryIntervalProperty.cs @@ -0,0 +1,12 @@ +using MQTTnet.Packets.Properties.BaseTypes; + +namespace MQTTnet.Packets.Properties +{ + public class SessionExpiryIntervalProperty : FourByteIntegerValue + { + public SessionExpiryIntervalProperty(uint value) + : base((byte)PropertyID.SessionExpiryInterval, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/SubscriptionIdentifierProperty.cs b/Source/MQTTnet/Packets/Properties/SubscriptionIdentifierProperty.cs new file mode 100644 index 00000000..dca6ad58 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/SubscriptionIdentifierProperty.cs @@ -0,0 +1,12 @@ +using MQTTnet.Packets.Properties.BaseTypes; + +namespace MQTTnet.Packets.Properties +{ + public class SubscriptionIdentifierProperty : VariableByteIntegerProperty + { + public SubscriptionIdentifierProperty(uint value) + : base((byte)PropertyID.SubscriptionIdentifier, value) + { + } + } +} diff --git a/Source/MQTTnet/Packets/Properties/WillDelayIntervalProperty.cs b/Source/MQTTnet/Packets/Properties/WillDelayIntervalProperty.cs new file mode 100644 index 00000000..e285f868 --- /dev/null +++ b/Source/MQTTnet/Packets/Properties/WillDelayIntervalProperty.cs @@ -0,0 +1,12 @@ +using MQTTnet.Packets.Properties.BaseTypes; + +namespace MQTTnet.Packets.Properties +{ + public class WillDelayIntervalProperty : FourByteIntegerValue + { + public WillDelayIntervalProperty(uint value) + : base((byte)PropertyID.WillDelayInterval, value) + { + } + } +} diff --git a/Source/MQTTnet/Serializer/MqttPacketWriter.cs b/Source/MQTTnet/Serializer/MqttPacketWriter.cs index beb146fb..3df5d964 100644 --- a/Source/MQTTnet/Serializer/MqttPacketWriter.cs +++ b/Source/MQTTnet/Serializer/MqttPacketWriter.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Text; using MQTTnet.Protocol; @@ -27,7 +28,7 @@ namespace MQTTnet.Serializer return (byte)fixedHeader; } - public static ArraySegment EncodeVariableByteInteger(int value) + public static ArraySegment EncodeVariableByteInteger(uint value) { if (value <= 0) { @@ -54,7 +55,7 @@ namespace MQTTnet.Serializer return new ArraySegment(buffer, 0, bufferOffset); } - public void WriteVariableLengthInteger(int value) + public void WriteVariableLengthInteger(uint value) { Write(EncodeVariableByteInteger(value)); } @@ -146,6 +147,7 @@ namespace MQTTnet.Serializer Array.Resize(ref _buffer, MaxBufferSize); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EnsureAdditionalCapacity(int additionalCapacity) { var freeSpace = _buffer.Length - _position; @@ -157,6 +159,7 @@ namespace MQTTnet.Serializer EnsureCapacity(_buffer.Length + additionalCapacity - freeSpace); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void EnsureCapacity(int capacity) { var newBufferLength = _buffer.Length; @@ -174,6 +177,7 @@ namespace MQTTnet.Serializer Array.Resize(ref _buffer, newBufferLength); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void IncreasePostition(int length) { _position += length; diff --git a/Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs b/Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs index bb72db14..9d36c9e2 100644 --- a/Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs +++ b/Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs @@ -23,7 +23,7 @@ namespace MQTTnet.Serializer _packetWriter.Seek(5); var fixedHeader = SerializePacket(packet, _packetWriter); - var remainingLength = _packetWriter.Length - 5; + var remainingLength = (uint)(_packetWriter.Length - 5); var remainingLengthBuffer = MqttPacketWriter.EncodeVariableByteInteger(remainingLength); diff --git a/Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs b/Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs index 44ec36be..e0241555 100644 --- a/Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs +++ b/Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs @@ -82,11 +82,11 @@ namespace MQTTnet.Serializer var propertyWriter = new MqttPacketWriter(); foreach (var property in packet.Properties) { - propertyWriter.Write((byte)property.Key); - property.Value.WriteTo(propertyWriter); + propertyWriter.Write(property.Id); + property.WriteTo(propertyWriter); } - packetWriter.WriteVariableLengthInteger(propertyWriter.Length); + packetWriter.WriteVariableLengthInteger((uint)propertyWriter.Length); packetWriter.Write(propertyWriter); }