Browse Source

Add more property implementations.

release/3.x.x
Christian Kratky 6 years ago
parent
commit
2fbb44ec66
27 changed files with 270 additions and 26 deletions
  1. +1
    -1
      Source/MQTTnet/Packets/MqttConnectPacket.cs
  2. +10
    -0
      Source/MQTTnet/Packets/Properties/AssignedClientIdentifierProperty.cs
  3. +12
    -0
      Source/MQTTnet/Packets/Properties/AuthenticationDataProperty.cs
  4. +10
    -0
      Source/MQTTnet/Packets/Properties/AuthenticationMethodProperty.cs
  5. +31
    -0
      Source/MQTTnet/Packets/Properties/BaseTypes/BinaryDataProperty.cs
  6. +5
    -2
      Source/MQTTnet/Packets/Properties/BaseTypes/ByteProperty.cs
  7. +27
    -0
      Source/MQTTnet/Packets/Properties/BaseTypes/FourByteIntegerProperty.cs
  8. +5
    -2
      Source/MQTTnet/Packets/Properties/BaseTypes/StringProperty.cs
  9. +7
    -4
      Source/MQTTnet/Packets/Properties/BaseTypes/TwoByteIntegerProperty.cs
  10. +26
    -0
      Source/MQTTnet/Packets/Properties/BaseTypes/VariableByteIntegerProperty.cs
  11. +10
    -0
      Source/MQTTnet/Packets/Properties/ContentTypeProperty.cs
  12. +12
    -0
      Source/MQTTnet/Packets/Properties/CorrelationDataProperty.cs
  13. +3
    -1
      Source/MQTTnet/Packets/Properties/IProperty.cs
  14. +12
    -0
      Source/MQTTnet/Packets/Properties/MessageExpiryIntervalProperty.cs
  15. +10
    -0
      Source/MQTTnet/Packets/Properties/PayloadFormatIndicatorProperty.cs
  16. +0
    -9
      Source/MQTTnet/Packets/Properties/Property.cs
  17. +1
    -1
      Source/MQTTnet/Packets/Properties/PropertyID.cs
  18. +10
    -0
      Source/MQTTnet/Packets/Properties/RequestProblemInformationProperty.cs
  19. +10
    -0
      Source/MQTTnet/Packets/Properties/RequestResponseInformationProperty.cs
  20. +10
    -0
      Source/MQTTnet/Packets/Properties/ResponseTopicProperty.cs
  21. +12
    -0
      Source/MQTTnet/Packets/Properties/ServerKeepAliveProperty.cs
  22. +12
    -0
      Source/MQTTnet/Packets/Properties/SessionExpiryIntervalProperty.cs
  23. +12
    -0
      Source/MQTTnet/Packets/Properties/SubscriptionIdentifierProperty.cs
  24. +12
    -0
      Source/MQTTnet/Packets/Properties/WillDelayIntervalProperty.cs
  25. +6
    -2
      Source/MQTTnet/Serializer/MqttPacketWriter.cs
  26. +1
    -1
      Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs
  27. +3
    -3
      Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs

+ 1
- 1
Source/MQTTnet/Packets/MqttConnectPacket.cs View File

@@ -24,7 +24,7 @@ namespace MQTTnet.Packets

public MqttApplicationMessage WillMessage { get; set; }

public List<Property> Properties { get; set; }
public List<IProperty> Properties { get; set; }

public override string ToString()
{


+ 10
- 0
Source/MQTTnet/Packets/Properties/AssignedClientIdentifierProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class AssignedClientIdentifierProperty : StringProperty
{
public AssignedClientIdentifierProperty(string value)
: base((byte)PropertyID.AssignedClientIdentifer, value)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/AuthenticationDataProperty.cs View File

@@ -0,0 +1,12 @@
using System;

namespace MQTTnet.Packets.Properties
{
public class AuthenticationDataProperty : BinaryDataProperty
{
public AuthenticationDataProperty(ArraySegment<byte> data)
: base((byte)PropertyID.AuthenticationData, data)
{
}
}
}

+ 10
- 0
Source/MQTTnet/Packets/Properties/AuthenticationMethodProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class AuthenticationMethodProperty : StringProperty
{
public AuthenticationMethodProperty(string value)
: base((byte)PropertyID.AuthenticationMethod, value)
{
}
}
}

+ 31
- 0
Source/MQTTnet/Packets/Properties/BaseTypes/BinaryDataProperty.cs View File

@@ -0,0 +1,31 @@
using System;
using MQTTnet.Serializer;

namespace MQTTnet.Packets.Properties
{
public class BinaryDataProperty : IProperty
{
public BinaryDataProperty(byte id, ArraySegment<byte> data)
{
if (data.Array == null)
{
throw new ArgumentNullException(nameof(data));
}

Id = id;
Data = data;
}

public byte Id { get; }

public ArraySegment<byte> Data { get; }

public void WriteTo(MqttPacketWriter writer)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));

writer.Write((ushort)Data.Count);
writer.Write(Data);
}
}
}

Source/MQTTnet/Packets/Properties/ByteValue.cs → Source/MQTTnet/Packets/Properties/BaseTypes/ByteProperty.cs View File

@@ -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)

+ 27
- 0
Source/MQTTnet/Packets/Properties/BaseTypes/FourByteIntegerProperty.cs View File

@@ -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);
}
}
}

Source/MQTTnet/Packets/Properties/StringValue.cs → Source/MQTTnet/Packets/Properties/BaseTypes/StringProperty.cs View File

@@ -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)

Source/MQTTnet/Packets/Properties/TwoByteIntegerValue.cs → Source/MQTTnet/Packets/Properties/BaseTypes/TwoByteIntegerProperty.cs View File

@@ -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);
}
}
}

+ 26
- 0
Source/MQTTnet/Packets/Properties/BaseTypes/VariableByteIntegerProperty.cs View File

@@ -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);
}
}
}

+ 10
- 0
Source/MQTTnet/Packets/Properties/ContentTypeProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class ContentTypeProperty : StringProperty
{
public ContentTypeProperty(string value)
: base((byte)PropertyID.ContentType, value)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/CorrelationDataProperty.cs View File

@@ -0,0 +1,12 @@
using System;

namespace MQTTnet.Packets.Properties
{
public class CorrelationDataProperty : BinaryDataProperty
{
public CorrelationDataProperty(ArraySegment<byte> data)
: base((byte)PropertyID.CorrelationData, data)
{
}
}
}

Source/MQTTnet/Packets/Properties/IPropertyValue.cs → Source/MQTTnet/Packets/Properties/IProperty.cs View File

@@ -2,8 +2,10 @@

namespace MQTTnet.Packets.Properties
{
public interface IPropertyValue
public interface IProperty
{
byte Id { get; }

void WriteTo(MqttPacketWriter writer);
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/MessageExpiryIntervalProperty.cs View File

@@ -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)
{
}
}
}

+ 10
- 0
Source/MQTTnet/Packets/Properties/PayloadFormatIndicatorProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class PayloadFormatIndicatorProperty : ByteProperty
{
public PayloadFormatIndicatorProperty(byte value)
: base((byte)PropertyID.PayloadFormatIndicator, value)
{
}
}
}

+ 0
- 9
Source/MQTTnet/Packets/Properties/Property.cs View File

@@ -1,9 +0,0 @@
namespace MQTTnet.Packets.Properties
{
public class Property
{
public PropertyType Type { get; set; }

public IPropertyValue Value { get; set; }
}
}

Source/MQTTnet/Packets/Properties/PropertyType.cs → Source/MQTTnet/Packets/Properties/PropertyID.cs View File

@@ -1,6 +1,6 @@
namespace MQTTnet.Packets.Properties
{
public enum PropertyType
public enum PropertyID
{
PayloadFormatIndicator = 1,
MessageExpiryInterval = 2,

+ 10
- 0
Source/MQTTnet/Packets/Properties/RequestProblemInformationProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class RequestProblemInformationProperty : ByteProperty
{
public RequestProblemInformationProperty(byte value)
: base((byte)PropertyID.RequestProblemInformation, value)
{
}
}
}

+ 10
- 0
Source/MQTTnet/Packets/Properties/RequestResponseInformationProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class RequestResponseInformationProperty : ByteProperty
{
public RequestResponseInformationProperty(byte value)
: base((byte)PropertyID.RequestResponseInformation, value)
{
}
}
}

+ 10
- 0
Source/MQTTnet/Packets/Properties/ResponseTopicProperty.cs View File

@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class ResponseTopicProperty : StringProperty
{
public ResponseTopicProperty(string value)
: base((byte)PropertyID.ResponseTopic, value)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/ServerKeepAliveProperty.cs View File

@@ -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)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/SessionExpiryIntervalProperty.cs View File

@@ -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)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/SubscriptionIdentifierProperty.cs View File

@@ -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)
{
}
}
}

+ 12
- 0
Source/MQTTnet/Packets/Properties/WillDelayIntervalProperty.cs View File

@@ -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)
{
}
}
}

+ 6
- 2
Source/MQTTnet/Serializer/MqttPacketWriter.cs View File

@@ -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<byte> EncodeVariableByteInteger(int value)
public static ArraySegment<byte> EncodeVariableByteInteger(uint value)
{
if (value <= 0)
{
@@ -54,7 +55,7 @@ namespace MQTTnet.Serializer
return new ArraySegment<byte>(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;


+ 1
- 1
Source/MQTTnet/Serializer/MqttV310PacketSerializer.cs View File

@@ -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);



+ 3
- 3
Source/MQTTnet/Serializer/MqttV500PacketSerializer.cs View File

@@ -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);
}



Loading…
Cancel
Save