private async Task<int> ReadAsync(byte[] buffer, int offset, int count)
{
try
{
//return 0 if window size has been reached
if(_unreadFromStream <= 0)
return 0;
//get max amount of data to read
int max = Math.Min(count, _unreadFromStream);
//read
int read;
if(Scheduler.RunningSynchronously)
read = _innerStream.Read(buffer, offset, max);
else
read = await _innerStream.ReadAsync(buffer, offset, max).AutoConfigureAwait();
//update remaining
_unreadFromStream -= read;
//signal EOS when window reached
if(_unreadFromStream <= 0)
{
Scheduler.RunOnIOThread(() => _waitUntilAllFrameDataRead.TrySetResult(true));
//_waitUntilAllFrameDataRead.TrySetResult(true);
}
//return actual read count
return read;
}
catch(Exception ex)
{
//error no more to read
_unreadFromStream = 0;
//signal error as EOS
Scheduler.RunOnIOThread(() => _waitUntilAllFrameDataRead.SetException(ex));
//_waitUntilAllFrameDataRead.SetException(ex);
//rethrow
throw;
}
}