Skip to content

Commit

Permalink
Codec implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 9, 2024
1 parent 6b2a0c6 commit d818132
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
37 changes: 37 additions & 0 deletions src/ArtemisNetCoreClient/Framing/Codec.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 1 addition & 11 deletions src/ArtemisNetCoreClient/Framing/CreateSessionMessageV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ 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();
}

public override void Decode(ByteBuffer buffer)
{
_ = buffer.ReadByte(); // type
ChannelId = buffer.ReadLong();
ServerVersion = buffer.ReadInt();
}
}
2 changes: 1 addition & 1 deletion src/ArtemisNetCoreClient/Framing/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit d818132

Please sign in to comment.