public sealed override async Task<int> ReadAsync(
byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
var readBuffer = this.ReadBuffer;
var index = offset;
var pastEnd = offset + count;
// (index == offset) ensures that we only try to get more encoded data if we haven't yet copied anything
// into buffer
while ((index < pastEnd) && ((readBuffer.Index < readBuffer.Count) ||
((index == offset) && await readBuffer.ReadAsync(cancellationToken))))
{
var response = this.ReadByte(readBuffer, buffer, ref index);
if (response != null)
{
// TODO: The following is dangerous as another WriteAsync call could still be pending. It's probably
// best to use something like TaskQueue internally.
await this.WriteBuffer.WriteAsync(response, 0, response.Length, cancellationToken);
await this.WriteBuffer.FlushAsync(cancellationToken);
}
}
return index - offset;
}