Skip to content

Commit f33dd6c

Browse files
stebetlukebakken
authored andcommitted
Code cleanups.
1 parent 2a49674 commit f33dd6c

File tree

9 files changed

+83
-72
lines changed

9 files changed

+83
-72
lines changed

projects/client/Apigen/src/apigen/Apigen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ public void EmitMethodArgumentReader()
932932
EmitLine(" internal override Client.Impl.MethodBase DecodeMethodFrom(Memory<byte> memory)");
933933
EmitLine(" {");
934934
EmitLine(" ushort classId = Util.NetworkOrderDeserializer.ReadUInt16(memory);");
935-
EmitLine(" ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(memory, 2);");
935+
EmitLine(" ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(memory.Slice(2));");
936936
EmitLine(" Client.Impl.MethodBase result = DecodeMethodFrom(classId, methodId);");
937937
EmitLine(" if(result != null)");
938938
EmitLine(" {");

projects/client/RabbitMQ.Client/src/client/impl/BasicPublishBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal BasicPublishBatch (ModelBase model)
5555

5656
public void Add(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body)
5757
{
58-
var bp = basicProperties ?? _model.CreateBasicProperties();
58+
IBasicProperties bp = basicProperties ?? _model.CreateBasicProperties();
5959
var method = new BasicPublish
6060
{
6161
_exchange = exchange,

projects/client/RabbitMQ.Client/src/client/impl/ContentHeaderPropertyReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ContentHeaderPropertyReader
5050
protected ushort m_bitCount;
5151
protected ushort m_flagWord;
5252
private int _memoryOffset = 0;
53-
private Memory<byte> _memory;
53+
private readonly Memory<byte> _memory;
5454

5555
public ContentHeaderPropertyReader(Memory<byte> memory)
5656
{

projects/client/RabbitMQ.Client/src/client/impl/Frame.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -239,30 +239,35 @@ internal static InboundFrame ReadFrom(NetworkBinaryReader reader)
239239
ProcessProtocolHeader(reader);
240240
}
241241

242-
int channel = reader.ReadUInt16();
243-
int payloadSize = reader.ReadInt32(); // FIXME - throw exn on unreasonable value
244-
byte[] payload = ArrayPool<byte>.Shared.Rent(payloadSize);
245-
int bytesRead = 0;
246-
try
242+
using (IMemoryOwner<byte> headerMemory = MemoryPool<byte>.Shared.Rent(6))
247243
{
248-
while (bytesRead < payloadSize)
244+
Memory<byte> headerSlice = headerMemory.Memory.Slice(0, 6);
245+
reader.Read(headerSlice);
246+
int channel = NetworkOrderDeserializer.ReadUInt16(headerSlice);
247+
int payloadSize = NetworkOrderDeserializer.ReadInt32(headerSlice.Slice(2)); // FIXME - throw exn on unreasonable value
248+
byte[] payload = ArrayPool<byte>.Shared.Rent(payloadSize);
249+
int bytesRead = 0;
250+
try
249251
{
250-
bytesRead += reader.Read(payload, bytesRead, payloadSize - bytesRead);
252+
while (bytesRead < payloadSize)
253+
{
254+
bytesRead += reader.Read(payload, bytesRead, payloadSize - bytesRead);
255+
}
256+
}
257+
catch (Exception)
258+
{
259+
// Early EOF.
260+
throw new MalformedFrameException($"Short frame - expected to read {payloadSize} bytes, only got {bytesRead} bytes");
251261
}
252-
}
253-
catch (Exception)
254-
{
255-
// Early EOF.
256-
throw new MalformedFrameException($"Short frame - expected to read {payloadSize} bytes, only got {bytesRead} bytes");
257-
}
258262

259-
int frameEndMarker = reader.ReadByte();
260-
if (frameEndMarker != Constants.FrameEnd)
261-
{
262-
throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
263-
}
263+
int frameEndMarker = reader.ReadByte();
264+
if (frameEndMarker != Constants.FrameEnd)
265+
{
266+
throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
267+
}
264268

265-
return new InboundFrame((FrameType)type, channel, payload, payloadSize);
269+
return new InboundFrame((FrameType)type, channel, payload, payloadSize);
270+
}
266271
}
267272

268273
internal NetworkBinaryReader GetReader()

projects/client/RabbitMQ.Client/src/client/impl/ModelBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ abstract class ModelBase : IFullModel, IRecoverable
6767
private readonly RpcContinuationQueue _continuationQueue = new RpcContinuationQueue();
6868
private readonly ManualResetEvent _flowControlBlock = new ManualResetEvent(true);
6969

70-
private readonly object _eventLock = new object();
7170
private readonly object _shutdownLock = new object();
7271
private readonly object _rpcLock = new object();
7372

projects/client/RabbitMQ.Client/src/client/impl/WireFormatting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static void DecimalToAmqp(decimal value, out byte scale, out int mantissa
9898
public static IList ReadArray(Memory<byte> memory, out int bytesRead)
9999
{
100100
IList array = new List<object>();
101-
long arrayLength = NetworkOrderDeserializer.ReadUInt32(memory, 0);
101+
long arrayLength = NetworkOrderDeserializer.ReadUInt32(memory);
102102
bytesRead = 4;
103103
while (bytesRead - 4 < arrayLength)
104104
{
Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,109 @@
11
using System;
22
using System.Buffers.Binary;
33
using System.Linq;
4+
using System.Runtime.CompilerServices;
45
using System.Runtime.InteropServices;
56

67
namespace RabbitMQ.Util
78
{
89
internal static class NetworkOrderDeserializer
910
{
10-
internal static double ReadDouble(Memory<byte> memory, int offset = 0)
11+
internal static double ReadDouble(ReadOnlyMemory<byte> memory) => ReadDouble(memory.Span);
12+
13+
internal static double ReadDouble(ReadOnlySpan<byte> span)
1114
{
12-
if (memory.Length - offset < 8)
15+
if (span.Length < 8)
1316
{
14-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode Double from memory.");
17+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Double from memory.");
1518
}
1619

17-
long val = BinaryPrimitives.ReadInt64BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
18-
return BitConverter.Int64BitsToDouble(val);
20+
long val = BinaryPrimitives.ReadInt64BigEndian(span);
21+
return Unsafe.As<long, double>(ref val);
1922
}
2023

21-
internal static short ReadInt16(Memory<byte> memory, int offset = 0)
24+
internal static short ReadInt16(ReadOnlyMemory<byte> memory) => ReadInt16(memory.Span);
25+
26+
internal static short ReadInt16(ReadOnlySpan<byte> span)
2227
{
23-
if (memory.Length - offset < 2)
28+
if (span.Length < 2)
2429
{
25-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode Int16 from memory.");
30+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int16 from memory.");
2631
}
2732

28-
return BinaryPrimitives.ReadInt16BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
33+
return BinaryPrimitives.ReadInt16BigEndian(span);
2934
}
3035

31-
internal static int ReadInt32(Memory<byte> memory, int offset = 0)
36+
internal static int ReadInt32(ReadOnlyMemory<byte> memory) => ReadInt32(memory.Span);
37+
38+
internal static int ReadInt32(ReadOnlySpan<byte> span)
3239
{
33-
if (memory.Length - offset < 4)
40+
if (span.Length < 4)
3441
{
35-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode Int32 from memory.");
42+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int32 from memory.");
3643
}
3744

38-
return BinaryPrimitives.ReadInt32BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
45+
return BinaryPrimitives.ReadInt32BigEndian(span);
3946
}
4047

41-
internal static long ReadInt64(Memory<byte> memory, int offset = 0)
48+
internal static long ReadInt64(ReadOnlyMemory<byte> memory) => ReadInt64(memory.Span);
49+
50+
internal static long ReadInt64(ReadOnlySpan<byte> span)
4251
{
43-
if (memory.Length - offset < 8)
52+
if (span.Length < 8)
4453
{
45-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode Int64 from memory.");
54+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int64 from memory.");
4655
}
4756

48-
return BinaryPrimitives.ReadInt64BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
57+
return BinaryPrimitives.ReadInt64BigEndian(span);
4958
}
5059

51-
internal static float ReadSingle(Memory<byte> memory, int offset = 0)
52-
{
53-
if (memory.Length - offset < 4)
54-
{
55-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode Single from memory.");
56-
}
57-
58-
Memory<byte> bytes = memory.Slice(offset, 4);
59-
if (BitConverter.IsLittleEndian)
60-
{
61-
bytes.Span.Reverse();
62-
}
60+
internal static float ReadSingle(ReadOnlyMemory<byte> memory) => ReadSingle(memory.Span);
6361

64-
if (MemoryMarshal.TryGetArray(bytes, out ArraySegment<byte> segment))
62+
internal static float ReadSingle(ReadOnlySpan<byte> span)
63+
{
64+
if (span.Length < 4)
6565
{
66-
return BitConverter.ToSingle(segment.Array, segment.Offset);
66+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Single from memory.");
6767
}
6868

69-
throw new InvalidOperationException("Unable to get ArraySegment from memory.");
69+
int num = BinaryPrimitives.ReadInt32BigEndian(span);
70+
return Unsafe.As<int, float>(ref num);
7071
}
7172

72-
internal static ushort ReadUInt16(Memory<byte> memory, int offset = 0)
73+
internal static ushort ReadUInt16(Memory<byte> memory) => ReadUInt16(memory.Span);
74+
75+
internal static ushort ReadUInt16(ReadOnlySpan<byte> span)
7376
{
74-
if (memory.Length - offset < 2)
77+
if (span.Length < 2)
7578
{
76-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode UInt16 from memory.");
79+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt16 from memory.");
7780
}
7881

79-
return BinaryPrimitives.ReadUInt16BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
82+
return BinaryPrimitives.ReadUInt16BigEndian(span);
8083
}
8184

82-
internal static uint ReadUInt32(Memory<byte> memory, int offset = 0)
85+
internal static uint ReadUInt32(Memory<byte> memory) => ReadUInt32(memory.Span);
86+
87+
internal static uint ReadUInt32(ReadOnlySpan<byte> span)
8388
{
84-
if (memory.Length - offset < 4)
89+
if (span.Length < 4)
8590
{
86-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode UInt32 from memory.");
91+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt32 from memory.");
8792
}
8893

89-
return BinaryPrimitives.ReadUInt32BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
94+
return BinaryPrimitives.ReadUInt32BigEndian(span);
9095
}
9196

92-
internal static ulong ReadUInt64(Memory<byte> memory, int offset = 0)
97+
internal static ulong ReadUInt64(Memory<byte> memory) => ReadUInt64(memory.Span);
98+
99+
internal static ulong ReadUInt64(ReadOnlySpan<byte> span)
93100
{
94-
if (memory.Length - offset < 8)
101+
if (span.Length < 8)
95102
{
96-
throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to decode UInt64 from memory.");
103+
throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt64 from memory.");
97104
}
98105

99-
return BinaryPrimitives.ReadUInt64BigEndian((offset > 0 ? memory.Slice(offset) : memory).Span);
106+
return BinaryPrimitives.ReadUInt64BigEndian(span);
100107
}
101108
}
102109
}

projects/client/Unit/src/unit/TestFieldTableFormatting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void TestTableEncoding_S()
9797
int bytesNeeded = WireFormatting.GetTableByteCount(t);
9898
byte[] bytes = new byte[bytesNeeded];
9999
WireFormatting.WriteTable(bytes, t);
100-
IDictionary nt = (IDictionary)WireFormatting.ReadTable(bytes, out int bytesRead);
100+
WireFormatting.ReadTable(bytes, out int bytesRead);
101101
Assert.AreEqual(bytesNeeded, bytesRead);
102102
Check(bytes, new byte[] {
103103
0,0,0,9, // table length
@@ -117,7 +117,7 @@ public void TestTableEncoding_x()
117117
int bytesNeeded = WireFormatting.GetTableByteCount(t);
118118
byte[] bytes = new byte[bytesNeeded];
119119
WireFormatting.WriteTable(bytes, t);
120-
IDictionary nt = (IDictionary)WireFormatting.ReadTable(bytes, out int bytesRead);
120+
WireFormatting.ReadTable(bytes, out int bytesRead);
121121
Assert.AreEqual(bytesNeeded, bytesRead);
122122
Check(bytes, new byte[] {
123123
0,0,0,9, // table length

projects/client/Unit/src/unit/TestFieldTableFormattingGeneric.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void TestTableEncoding_S()
9999
int bytesNeeded = WireFormatting.GetTableByteCount(t);
100100
byte[] bytes = new byte[bytesNeeded];
101101
WireFormatting.WriteTable(bytes, t);
102-
IDictionary nt = (IDictionary)WireFormatting.ReadTable(bytes, out int bytesRead);
102+
WireFormatting.ReadTable(bytes, out int bytesRead);
103103
Assert.AreEqual(bytesNeeded, bytesRead);
104104
Check(bytes, new byte[] {
105105
0,0,0,9, // table length
@@ -119,7 +119,7 @@ public void TestTableEncoding_x()
119119
int bytesNeeded = WireFormatting.GetTableByteCount(t);
120120
byte[] bytes = new byte[bytesNeeded];
121121
WireFormatting.WriteTable(bytes, t);
122-
IDictionary nt = (IDictionary)WireFormatting.ReadTable(bytes, out int bytesRead);
122+
WireFormatting.ReadTable(bytes, out int bytesRead);
123123
Assert.AreEqual(bytesNeeded, bytesRead);
124124
Check(bytes, new byte[] {
125125
0,0,0,9, // table length

0 commit comments

Comments
 (0)