Add more property implementations.

This commit is contained in:
Christian Kratky
2018-11-18 21:54:40 +01:00
parent ace25af0e8
commit 2fbb44ec66
27 changed files with 270 additions and 26 deletions
+1 -1
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()
{
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class AssignedClientIdentifierProperty : StringProperty
{
public AssignedClientIdentifierProperty(string value)
: base((byte)PropertyID.AssignedClientIdentifer, value)
{
}
}
}
@@ -0,0 +1,12 @@
using System;
namespace MQTTnet.Packets.Properties
{
public class AuthenticationDataProperty : BinaryDataProperty
{
public AuthenticationDataProperty(ArraySegment<byte> data)
: base((byte)PropertyID.AuthenticationData, data)
{
}
}
}
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class AuthenticationMethodProperty : StringProperty
{
public AuthenticationMethodProperty(string value)
: base((byte)PropertyID.AuthenticationMethod, value)
{
}
}
}
@@ -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);
}
}
}
@@ -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)
@@ -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);
}
}
}
@@ -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)
@@ -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);
}
}
}
@@ -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);
}
}
}
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class ContentTypeProperty : StringProperty
{
public ContentTypeProperty(string value)
: base((byte)PropertyID.ContentType, value)
{
}
}
}
@@ -0,0 +1,12 @@
using System;
namespace MQTTnet.Packets.Properties
{
public class CorrelationDataProperty : BinaryDataProperty
{
public CorrelationDataProperty(ArraySegment<byte> data)
: base((byte)PropertyID.CorrelationData, data)
{
}
}
}
@@ -2,8 +2,10 @@
namespace MQTTnet.Packets.Properties
{
public interface IPropertyValue
public interface IProperty
{
byte Id { get; }
void WriteTo(MqttPacketWriter writer);
}
}
@@ -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)
{
}
}
}
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class PayloadFormatIndicatorProperty : ByteProperty
{
public PayloadFormatIndicatorProperty(byte value)
: base((byte)PropertyID.PayloadFormatIndicator, value)
{
}
}
}
@@ -1,9 +0,0 @@
namespace MQTTnet.Packets.Properties
{
public class Property
{
public PropertyType Type { get; set; }
public IPropertyValue Value { get; set; }
}
}
@@ -1,6 +1,6 @@
namespace MQTTnet.Packets.Properties
{
public enum PropertyType
public enum PropertyID
{
PayloadFormatIndicator = 1,
MessageExpiryInterval = 2,
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class RequestProblemInformationProperty : ByteProperty
{
public RequestProblemInformationProperty(byte value)
: base((byte)PropertyID.RequestProblemInformation, value)
{
}
}
}
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class RequestResponseInformationProperty : ByteProperty
{
public RequestResponseInformationProperty(byte value)
: base((byte)PropertyID.RequestResponseInformation, value)
{
}
}
}
@@ -0,0 +1,10 @@
namespace MQTTnet.Packets.Properties
{
public class ResponseTopicProperty : StringProperty
{
public ResponseTopicProperty(string value)
: base((byte)PropertyID.ResponseTopic, value)
{
}
}
}
@@ -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)
{
}
}
}
@@ -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)
{
}
}
}
@@ -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)
{
}
}
}
@@ -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)
{
}
}
}
@@ -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;
@@ -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);
@@ -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);
}