Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,10 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
cancellationToken.ThrowIfCancellationRequested();
}

// The following loop will repeat at most twice depending whether some data are readily available in the buffer (one iteration) or not.
// In which case, it'll wait on RECEIVE or any of PEER_SEND_(SHUTDOWN|ABORTED) event and attempt to copy data in the second iteration.
// Loop copying available data and/or waiting on RECEIVE or PEER_SEND_(SHUTDOWN|ABORTED) events.
// Keeps waiting while nothing was copied and the buffer is empty (and not final) to ignore stale wake-ups on _receiveTcs.
Comment thread
ManickaP marked this conversation as resolved.
int totalCopied = 0;
bool empty;
do
{
// Concurrent call, this one lost the race.
Expand All @@ -333,7 +334,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
}

// Copy data from the buffer, reduce target and increment total.
int copied = _receiveBuffers.CopyTo(buffer, out bool complete, out bool empty);
int copied = _receiveBuffers.CopyTo(buffer, out bool complete, out empty);
buffer = buffer.Slice(copied);
totalCopied += copied;

Expand All @@ -357,7 +358,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
{
break;
}
} while (!buffer.IsEmpty && totalCopied == 0); // Exit the loop if target buffer is full we at least copied something.
} while (totalCopied == 0 && empty); // Keep waiting while we haven't copied anything and no data is available.

if (totalCopied > 0 && Interlocked.CompareExchange(ref _receivedNeedsEnable, 0, 1) == 1)
{
Expand Down
Loading