Skip to content

Commit

Permalink
defining packet types
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Apr 12, 2024
1 parent 6922e27 commit 0b441f5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/TurboMqtt.Core/PacketTypes/MqttPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace TurboMqtt.Core.PacketTypes;

/// <summary>
/// Base for all MQTT packets.
/// </summary>
public abstract class MqttPacket
{
public abstract MqttPacketType PacketType { get; }

public virtual bool Duplicate => false;

public virtual QualityOfService QualityOfService => QualityOfService.AtMostOnce;

public virtual bool RetainRequested => false;

public override string ToString()
{
return $"{GetType().Name}[Type={PacketType}, QualityOfService={QualityOfService}, Duplicate={Duplicate}, Retain={RetainRequested}]";
}
}

/// <summary>
/// Base for MQTT packets that require a packet identifier.
/// </summary>
public abstract class MqttPacketWithId : MqttPacket
{
/// <summary>
/// The unique identifier assigned to the packet.
/// </summary>
/// <remarks>
/// Not all packets require an identifier.
/// </remarks>
public int PacketId { get; set; }
}
20 changes: 20 additions & 0 deletions src/TurboMqtt.Core/PacketTypes/PingReqPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace TurboMqtt.Core.PacketTypes;

/// <summary>
/// Packet sent to the client by the server in response to a <see cref="PingReqPacket"/>.
/// </summary>
/// <remarks>
/// Used to keep the connection alive.
/// </remarks>
public sealed class PingReqPacket : MqttPacket
{
public static readonly PingReqPacket Instance = new PingReqPacket();

private PingReqPacket(){}
public override MqttPacketType PacketType => MqttPacketType.PingReq;

public override string ToString()
{
return "PingReq";
}
}
17 changes: 17 additions & 0 deletions src/TurboMqtt.Core/PacketTypes/PingRespPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TurboMqtt.Core.PacketTypes;

/// <summary>
/// Packet sent to the client by the server in response to a <see cref="PingReqPacket"/>.
/// </summary>
public sealed class PingRespPacket : MqttPacket
{
public static readonly PingRespPacket Instance = new PingRespPacket();

private PingRespPacket(){}
public override MqttPacketType PacketType => MqttPacketType.PingResp;

public override string ToString()
{
return "PingResp";
}
}
67 changes: 67 additions & 0 deletions src/TurboMqtt.Core/PacketTypes/PublishPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace TurboMqtt.Core.PacketTypes;

/// <summary>
/// Used to send data to the server or client.
/// </summary>
/// <param name="PacketId">The unique id of this packet - may not be needed with <see cref="QualityOfService.AtMostOnce"/>.</param>
/// <param name="Qos">The delivery guarantee for this packet.</param>
/// <param name="Duplicate">Is this packet a duplicate?</param>
/// <param name="RetainRequested">Indicates whether or not this value has been retained by the MQTT broker.</param>
public sealed class PublishPacket(int PacketId, QualityOfService Qos, bool Duplicate, bool RetainRequested) : MqttPacketWithId

Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Parameter 'PacketId' is unread.

Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs

View workflow job for this annotation

GitHub Actions / Test-ubuntu-latest

Parameter 'PacketId' is unread.

Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Parameter 'PacketId' is unread.

Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs

View workflow job for this annotation

GitHub Actions / Test-windows-latest

Parameter 'PacketId' is unread.
{
public override MqttPacketType PacketType => MqttPacketType.Publish;

public override bool Duplicate { get; } = Duplicate;

public override QualityOfService QualityOfService { get; } = Qos;

public override bool RetainRequested { get; } = RetainRequested;

/// <summary>
/// Optional for <see cref="QualityOfService.AtMostOnce"/>
/// </summary>
public string? TopicName { get; set; }

public int PacketIdentifier { get; set; } // Only needed for QoS 1 and 2

// Payload
public ReadOnlyMemory<byte> Payload { get; set; } = ReadOnlyMemory<byte>.Empty;

// MQTT 3.1.1 and 5.0 - Optional Properties

/// <summary>
/// The Content Type property, available in MQTT 5.0.
/// This property is optional and indicates the MIME type of the application message.
/// </summary>
public string? ContentType { get; set; } // MQTT 5.0 only

/// <summary>
/// Response Topic property, available in MQTT 5.0.
/// It specifies the topic name for a response message.
/// </summary>
public string? ResponseTopic { get; set; } // MQTT 5.0 only

/// <summary>
/// Correlation Data property, available in MQTT 5.0.
/// This property is used by the sender of the request message to identify which request the response message is for when it receives a response.
/// </summary>
public byte[]? CorrelationData { get; set; } // MQTT 5.0 only

/// <summary>
/// User Property, available in MQTT 5.0.
/// This is a key-value pair that can be sent multiple times to convey additional information that is not covered by other means.
/// </summary>
public IDictionary<string, string>? UserProperties { get; set; } // MQTT 5.0 only

/// <summary>
/// Subscription Identifiers, available in MQTT 5.0.
/// This property allows associating the publication with multiple subscriptions.
/// Each identifier corresponds to a different subscription that matches the published message.
/// </summary>
public List<uint>? SubscriptionIdentifiers { get; set; } // MQTT 5.0 only

public override string ToString()
{
return $"Publish: [Topic={TopicName}] [PayloadLength={Payload.Length}] [QoSLevel={QualityOfService}] [Dup={Duplicate}] [Retain={RetainRequested}] [PacketIdentifier={PacketIdentifier}]";
}
}
3 changes: 3 additions & 0 deletions src/TurboMqtt.Core/QualityOfService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace TurboMqtt.Core;

/// <summary>
/// QoS value - corresponds to the MQTT specification.
/// </summary>
public enum QualityOfService
{
AtMostOnce = 0,
Expand Down

0 comments on commit 0b441f5

Please sign in to comment.