public int EndSend(IAsyncResult asyncResult, out SocketError errorCode)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
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, "EndSend"));
}
int bytesTransferred = castedAsyncResult.InternalWaitForCompletionInt32Result();
castedAsyncResult.EndCalled = true;
if (s_perfCountersEnabled)
{
if (bytesTransferred > 0)
{
SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketBytesSent, bytesTransferred);
if (Transport == TransportType.Udp)
{
SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketDatagramsSent);
}
}
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"bytesTransffered:{bytesTransferred}");
// Throw an appropriate SocketException if the native call failed asynchronously.
errorCode = (SocketError)castedAsyncResult.ErrorCode;
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
UpdateStatusAfterSocketError(errorCode);
if (NetEventSource.IsEnabled)
{
NetEventSource.Error(this, new SocketException((int)errorCode));
NetEventSource.Exit(this, 0);
}
return 0;
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, bytesTransferred);
return bytesTransferred;
}