From d81813218d6bb4c5e466c5ea03b107ffaf9da0f4 Mon Sep 17 00:00:00 2001 From: Havret Date: Sat, 9 Mar 2024 23:11:14 +0100 Subject: [PATCH] Codec implementation --- src/ArtemisNetCoreClient/Framing/Codec.cs | 37 +++++++++++++++++++ .../Framing/CreateSessionMessageV2.cs | 12 +----- .../Framing/CreateSessionResponseMessage.cs | 6 +-- src/ArtemisNetCoreClient/Framing/Packet.cs | 2 +- 4 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/ArtemisNetCoreClient/Framing/Codec.cs diff --git a/src/ArtemisNetCoreClient/Framing/Codec.cs b/src/ArtemisNetCoreClient/Framing/Codec.cs new file mode 100644 index 0000000..c904e16 --- /dev/null +++ b/src/ArtemisNetCoreClient/Framing/Codec.cs @@ -0,0 +1,37 @@ +namespace ActiveMQ.Artemis.Core.Client.Framing; + +internal static class Codec +{ + public static void Encode(ByteBuffer buffer, Packet packet, long channelId) + { + buffer.WriteInt(0); // The length gets filled in at the end + + var type = packet switch + { + CreateSessionMessageV2 => CreateSessionMessageV2.Type, + _ => throw new ArgumentOutOfRangeException(nameof(packet), packet, $"{packet.GetType()} is not supported for encoding") + }; + buffer.WriteByte(type); + buffer.WriteLong(channelId); + + packet.Encode(buffer); + + buffer.WriteSize(); + } + + public static (Packet packet, long channelId) Decode(ByteBuffer buffer) + { + var type = buffer.ReadByte(); + var channelId = buffer.ReadLong(); + + Packet packet = type switch + { + CreateSessionResponseMessage.Type => new CreateSessionResponseMessage(), + _ => throw new ArgumentOutOfRangeException($"Type {type} is not supported for decoding") + }; + + packet.Decode(buffer); + + return (packet, channelId); + } +} \ No newline at end of file diff --git a/src/ArtemisNetCoreClient/Framing/CreateSessionMessageV2.cs b/src/ArtemisNetCoreClient/Framing/CreateSessionMessageV2.cs index ed280b5..52f85c2 100644 --- a/src/ArtemisNetCoreClient/Framing/CreateSessionMessageV2.cs +++ b/src/ArtemisNetCoreClient/Framing/CreateSessionMessageV2.cs @@ -2,7 +2,7 @@ namespace ActiveMQ.Artemis.Core.Client.Framing; internal class CreateSessionMessageV2 : Packet { - public override byte Type => unchecked((byte) -18); + public const byte Type = unchecked((byte) -18); public required string Name { get; init; } public long SessionChannelId { get; init; } public int Version { get; init; } @@ -16,16 +16,9 @@ internal class CreateSessionMessageV2 : Packet public int WindowSize { get; init; } public string? DefaultAddress { get; init; } public string? ClientId { get; init; } - public long ChannelId { get; init; } public override void Encode(ByteBuffer buffer) { - // header - buffer.WriteInt(0); // The length gets filled in at the end - buffer.WriteByte(Type); - buffer.WriteLong(ChannelId); - - // rest buffer.WriteString(Name); buffer.WriteLong(SessionChannelId); buffer.WriteInt(Version); @@ -39,9 +32,6 @@ public override void Encode(ByteBuffer buffer) buffer.WriteBool(PreAcknowledge); buffer.WriteNullableString(DefaultAddress); buffer.WriteNullableString(ClientId); - - // size - buffer.WriteSize(); } public override void Decode(ByteBuffer buffer) diff --git a/src/ArtemisNetCoreClient/Framing/CreateSessionResponseMessage.cs b/src/ArtemisNetCoreClient/Framing/CreateSessionResponseMessage.cs index 8c259db..997820d 100644 --- a/src/ArtemisNetCoreClient/Framing/CreateSessionResponseMessage.cs +++ b/src/ArtemisNetCoreClient/Framing/CreateSessionResponseMessage.cs @@ -2,11 +2,9 @@ namespace ActiveMQ.Artemis.Core.Client.Framing; internal class CreateSessionResponseMessage : Packet { - public long ChannelId { get; private set; } + public const byte Type = 31; public int ServerVersion { get; private set; } - public override byte Type { get; } = 31; - public override void Encode(ByteBuffer buffer) { throw new NotImplementedException(); @@ -14,8 +12,6 @@ public override void Encode(ByteBuffer buffer) public override void Decode(ByteBuffer buffer) { - _ = buffer.ReadByte(); // type - ChannelId = buffer.ReadLong(); ServerVersion = buffer.ReadInt(); } } \ No newline at end of file diff --git a/src/ArtemisNetCoreClient/Framing/Packet.cs b/src/ArtemisNetCoreClient/Framing/Packet.cs index dd40c5e..1913726 100644 --- a/src/ArtemisNetCoreClient/Framing/Packet.cs +++ b/src/ArtemisNetCoreClient/Framing/Packet.cs @@ -2,7 +2,7 @@ namespace ActiveMQ.Artemis.Core.Client.Framing; internal abstract class Packet { - public abstract byte Type { get; } public abstract void Encode(ByteBuffer buffer); + public abstract void Decode(ByteBuffer buffer); } \ No newline at end of file