public override int EndRead(IAsyncResult asyncResult)
{
if (NetEventSource.IsEnabled)
{
NetEventSource.Enter(this);
NetEventSource.Info(this, $"asyncResult: {asyncResult}");
}
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
HttpRequestStreamAsyncResult castedAsyncResult = asyncResult as HttpRequestStreamAsyncResult;
if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
{
throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
}
if (castedAsyncResult.EndCalled)
{
throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, nameof(EndRead)));
}
castedAsyncResult.EndCalled = true;
// wait & then check for errors
object returnValue = castedAsyncResult.InternalWaitForCompletion();
Exception exception = returnValue as Exception;
if (exception != null)
{
if (NetEventSource.IsEnabled)
{
NetEventSource.Info(this, "Rethrowing exception:" + exception);
NetEventSource.Error(this, exception.ToString());
}
throw exception;
}
uint dataRead = (uint)returnValue;
UpdateAfterRead((uint)castedAsyncResult.ErrorCode, dataRead);
if (NetEventSource.IsEnabled)
{
NetEventSource.Info(this, $"returnValue:{returnValue}");
NetEventSource.Exit(this);
}
return (int)dataRead + (int)castedAsyncResult._dataAlreadyRead;
}