Skip to content

Commit

Permalink
Fix GetRequiredBufferSize for CreateQueueMessage packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Apr 25, 2024
1 parent 2825cb4 commit 9a07002
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
36 changes: 36 additions & 0 deletions src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public static int WriteInt32(ref byte destination, int value)
Unsafe.WriteUnaligned(ref destination, BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value);
return sizeof(int);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetNullableInt32ByteCount(int? value)
{
var byteCount = sizeof(byte);
if (value.HasValue)
{
byteCount += sizeof(int);
}

return byteCount;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadNullableInt32(in ReadOnlySpan<byte> source, out int? value)
Expand Down Expand Up @@ -77,6 +89,18 @@ public static int WriteInt64(ref byte destination, long value)
Unsafe.WriteUnaligned(ref destination, BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value);
return sizeof(long);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetNullableInt64ByteCount(long? value)
{
var byteCount = sizeof(byte);
if (value.HasValue)
{
byteCount += sizeof(long);
}

return byteCount;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadNullableInt64(in ReadOnlySpan<byte> source, out long? value)
Expand Down Expand Up @@ -136,6 +160,18 @@ public static int WriteBool(ref byte destination, bool value)
return WriteByte(ref destination, value ? minusOne : zero);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetNullableBoolByteCount(bool? value)
{
var byteCount = sizeof(byte);
if (value.HasValue)
{
byteCount += sizeof(byte);
}

return byteCount;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadNullableBool(in ReadOnlySpan<byte> source, out bool? value)
{
Expand Down
26 changes: 13 additions & 13 deletions src/ArtemisNetCoreClient/Framing/CreateQueueMessageV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ public int GetRequiredBufferSize()
byteCount += sizeof(byte); // RoutingType
byteCount += sizeof(int); // MaxConsumers
byteCount += sizeof(bool); // PurgeOnNoConsumers
byteCount += sizeof(bool); // Exclusive
byteCount += sizeof(bool); // LastValue
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(Exclusive);
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(LastValue);
byteCount += ArtemisBinaryConverter.GetNullableSimpleStringByteCount(LastValueKey);
byteCount += sizeof(bool); // NonDestructive
byteCount += sizeof(int); // ConsumersBeforeDispatch
byteCount += sizeof(long); // DelayBeforeDispatch
byteCount += sizeof(bool); // GroupRebalance
byteCount += sizeof(int); // GroupBuckets
byteCount += sizeof(bool); // AutoDelete
byteCount += sizeof(long); // AutoDeleteDelay
byteCount += sizeof(long); // AutoDeleteMessageCount
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(NonDestructive);
byteCount += ArtemisBinaryConverter.GetNullableInt32ByteCount(ConsumersBeforeDispatch);
byteCount += ArtemisBinaryConverter.GetNullableInt64ByteCount(DelayBeforeDispatch);
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(GroupRebalance);
byteCount += ArtemisBinaryConverter.GetNullableInt32ByteCount(GroupBuckets);
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(AutoDelete);
byteCount += ArtemisBinaryConverter.GetNullableInt64ByteCount(AutoDeleteDelay);
byteCount += ArtemisBinaryConverter.GetNullableInt64ByteCount(AutoDeleteMessageCount);
byteCount += ArtemisBinaryConverter.GetNullableSimpleStringByteCount(GroupFirstKey);
byteCount += sizeof(long); // RingSize
byteCount += sizeof(bool); // Enabled
byteCount += sizeof(bool); // GroupRebalancePauseDispatch
byteCount += ArtemisBinaryConverter.GetNullableInt64ByteCount(RingSize);
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(Enabled);
byteCount += ArtemisBinaryConverter.GetNullableBoolByteCount(GroupRebalancePauseDispatch);

return byteCount;

Expand Down
15 changes: 9 additions & 6 deletions test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public void should_decode_bool(byte[] encoded, bool expected)
}

[Theory]
[InlineData(true, new[] { unchecked((byte) -1), unchecked((byte) -1) }, 2)]
[InlineData(false, new[] { unchecked((byte) -1), (byte) 0 }, 2)]
[InlineData(null, new byte[] { 0 }, 1)]
public void should_encode_nullable_bool(bool? value, byte[] encoded, int bufferSize)
[InlineData(true, new[] { unchecked((byte) -1), unchecked((byte) -1) })]
[InlineData(false, new[] { unchecked((byte) -1), (byte) 0 })]
[InlineData(null, new byte[] { 0 })]
public void should_encode_nullable_bool(bool? value, byte[] encoded)
{
// Arrange
var bufferSize = ArtemisBinaryConverter.GetNullableBoolByteCount(value);
var byteBuffer = new byte[bufferSize];

// Act
Expand Down Expand Up @@ -126,7 +127,8 @@ public void should_decode_int()
public void should_encode_nullable_int(int? value, byte[] encoded)
{
// Arrange
var byteBuffer = new byte[encoded.Length];
var bufferSize = ArtemisBinaryConverter.GetNullableInt32ByteCount(value);
var byteBuffer = new byte[bufferSize];

// Act
var writtenBytes = ArtemisBinaryConverter.WriteNullableInt32(ref byteBuffer.AsSpan().GetReference(), value);
Expand Down Expand Up @@ -204,7 +206,8 @@ public void should_decode_long()
public void should_encode_nullable_long(long? value, byte[] encoded)
{
// Arrange
var byteBuffer = new byte[encoded.Length];
var bufferSize = ArtemisBinaryConverter.GetNullableInt64ByteCount(value);
var byteBuffer = new byte[bufferSize];

// Act
var writtenBytes = ArtemisBinaryConverter.WriteNullableInt64(ref byteBuffer.AsSpan().GetReference(), value);
Expand Down

0 comments on commit 9a07002

Please sign in to comment.