Skip to content

Commit f8d9231

Browse files
committed
🐛 ReadAsyncInternal
1 parent 69388a3 commit f8d9231

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

src/BD.Common8.Bcl/IO/Pipelines/DuplexPipeStream.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,45 +165,36 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
165165
}
166166

167167
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
168-
private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken)
168+
async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken)
169169
{
170170
while (true)
171171
{
172+
var result = await input.ReadAsync(cancellationToken);
173+
var readableBuffer = result.Buffer;
172174
try
173175
{
174-
var result = await input.ReadAsync(cancellationToken);
175-
var readableBuffer = result.Buffer;
176-
try
176+
if (throwOnCancelled && result.IsCanceled && cancelCalled)
177177
{
178-
if (throwOnCancelled && result.IsCanceled && cancelCalled)
179-
{
180-
// Reset the bool
181-
cancelCalled = false;
182-
throw new OperationCanceledException();
183-
}
184-
185-
if (!readableBuffer.IsEmpty)
186-
{
187-
// buffer.Count is int
188-
var count = (int)Math.Min(readableBuffer.Length, destination.Length);
189-
readableBuffer = readableBuffer.Slice(0, count);
190-
readableBuffer.CopyTo(destination.Span);
191-
return count;
192-
}
193-
194-
if (result.IsCompleted)
195-
{
196-
return 0;
197-
}
178+
// Reset the bool
179+
cancelCalled = false;
180+
throw new OperationCanceledException();
198181
}
199-
finally
182+
183+
if (!readableBuffer.IsEmpty)
200184
{
201-
input.AdvanceTo(readableBuffer.End, readableBuffer.End);
185+
// buffer.Count is int
186+
var count = (int)Math.Min(readableBuffer.Length, destination.Length);
187+
readableBuffer = readableBuffer.Slice(0, count);
188+
readableBuffer.CopyTo(destination.Span);
189+
return count;
202190
}
191+
192+
if (result.IsCompleted)
193+
return 0;
203194
}
204-
catch (OperationCanceledException)
195+
finally
205196
{
206-
return -1;
197+
input.AdvanceTo(readableBuffer.End, readableBuffer.End);
207198
}
208199
}
209200
}

0 commit comments

Comments
 (0)