private void ReadLoop()
{
if (!Reading)
{
return;
}
var buffer = new byte[1024];
var signal = new EventSignal<CallbackDetail<int>>();
signal.Finished += (sender, e) =>
{
if (e.Result.IsFaulted)
{
Exception exception = e.Result.Exception.GetBaseException();
if (!IsRequestAborted(exception))
{
if (!(exception is IOException))
{
_connection.OnError(exception);
}
StopReading();
}
return;
}
int read = e.Result.Result;
if (read > 0)
{
// Put chunks in the buffer
_buffer.Add(buffer, read);
}
if (read == 0)
{
// Stop any reading we're doing
StopReading();
return;
}
// Keep reading the next set of data
ReadLoop();
if (read <= buffer.Length)
{
// If we read less than we wanted or if we filled the buffer, process it
ProcessBuffer();
}
};
StreamExtensions.ReadAsync(signal,_stream,buffer);
}