public int EndReceiveFrom(IAsyncResult asyncResult, ref EndPoint endPoint)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (endPoint == null)
{
throw new ArgumentNullException(nameof(endPoint));
}
if (!CanTryAddressFamily(endPoint.AddressFamily))
{
throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, endPoint.AddressFamily, _addressFamily), nameof(endPoint));
}
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
OverlappedAsyncResult castedAsyncResult = asyncResult as OverlappedAsyncResult;
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, "EndReceiveFrom"));
}
Internals.SocketAddress socketAddressOriginal = SnapshotAndSerialize(ref endPoint);
int bytesTransferred = castedAsyncResult.InternalWaitForCompletionInt32Result();
castedAsyncResult.EndCalled = true;
// Update socket address size.
castedAsyncResult.SocketAddress.InternalSize = castedAsyncResult.GetSocketAddressSize();
if (!socketAddressOriginal.Equals(castedAsyncResult.SocketAddress))
{
try
{
endPoint = endPoint.Create(castedAsyncResult.SocketAddress);
}
catch
{
}
}
if (s_perfCountersEnabled)
{
if (bytesTransferred > 0)
{
SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketBytesReceived, bytesTransferred);
if (Transport == TransportType.Udp)
{
SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketDatagramsReceived);
}
}
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"bytesTransferred:{bytesTransferred}");
// Throw an appropriate SocketException if the native call failed asynchronously.
if ((SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException(castedAsyncResult.ErrorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, bytesTransferred);
return bytesTransferred;
}