Skip to content

Commit 65b7a23

Browse files
committed
Add static method to check for end of stream and throw exception if necessary
1 parent bcde29e commit 65b7a23

File tree

1 file changed

+14
-12
lines changed
  • projects/RabbitMQ.Client/client/impl

1 file changed

+14
-12
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ internal static async ValueTask<InboundFrame> ReadFromPipeAsync(PipeReader reade
259259
.ConfigureAwait(false);
260260

261261
ReadOnlySequence<byte> buffer = result.Buffer;
262-
if (result.IsCompleted || buffer.Length == 0)
263-
{
264-
throw new EndOfStreamException("Pipe is completed.");
265-
}
262+
263+
MaybeThrowEndOfStream(result, buffer);
266264

267265
InboundFrame frame;
268266
// Loop until we have enough data to read an entire frame, or until the pipe is completed.
@@ -274,10 +272,7 @@ internal static async ValueTask<InboundFrame> ReadFromPipeAsync(PipeReader reade
274272
result = await reader.ReadAsync()
275273
.ConfigureAwait(false);
276274

277-
if (result.IsCompleted || buffer.Length == 0)
278-
{
279-
throw new EndOfStreamException("Pipe is completed.");
280-
}
275+
MaybeThrowEndOfStream(result, buffer);
281276

282277
buffer = result.Buffer;
283278
}
@@ -291,10 +286,8 @@ internal static bool TryReadFrameFromPipe(PipeReader reader, uint maxMessageSize
291286
if (reader.TryRead(out ReadResult result))
292287
{
293288
ReadOnlySequence<byte> buffer = result.Buffer;
294-
if (result.IsCompleted || buffer.Length == 0)
295-
{
296-
throw new EndOfStreamException("Pipe is completed.");
297-
}
289+
290+
MaybeThrowEndOfStream(result, buffer);
298291

299292
if (TryReadFrame(ref buffer, maxMessageSize, out frame))
300293
{
@@ -377,6 +370,15 @@ public override string ToString()
377370
{
378371
return $"(type={Type}, channel={Channel}, {Payload.Length} bytes of payload)";
379372
}
373+
374+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
375+
private static void MaybeThrowEndOfStream(ReadResult result, ReadOnlySequence<byte> buffer)
376+
{
377+
if (result.IsCompleted || buffer.Length == 0)
378+
{
379+
throw new EndOfStreamException("Pipe is completed.");
380+
}
381+
}
380382
}
381383

382384
internal enum FrameType : int

0 commit comments

Comments
 (0)