public IAsyncResult BeginReceiveMessageFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
{
if (NetEventSource.IsEnabled)
{
NetEventSource.Enter(this);
NetEventSource.Info(this, $"size:{size}");
}
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (remoteEP == null)
{
throw new ArgumentNullException(nameof(remoteEP));
}
if (!CanTryAddressFamily(remoteEP.AddressFamily))
{
throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, remoteEP.AddressFamily, _addressFamily), nameof(remoteEP));
}
if (offset < 0 || offset > buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (size < 0 || size > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
if (_rightEndPoint == null)
{
throw new InvalidOperationException(SR.net_sockets_mustbind);
}
SocketPal.CheckDualModeReceiveSupport(this);
// Set up the result and set it to collect the context.
ReceiveMessageOverlappedAsyncResult asyncResult = new ReceiveMessageOverlappedAsyncResult(this, state, callback);
asyncResult.StartPostingAsyncOp(false);
// Start the ReceiveFrom.
EndPoint oldEndPoint = _rightEndPoint;
// We don't do a CAS demand here because the contents of remoteEP aren't used by
// WSARecvMsg; all that matters is that we generate a unique-to-this-call SocketAddress
// with the right address family
Internals.SocketAddress socketAddress = SnapshotAndSerialize(ref remoteEP);
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
// Save a copy of the original EndPoint in the asyncResult.
asyncResult.SocketAddressOriginal = IPEndPointExtensions.Serialize(remoteEP);
SetReceivingPacketInformation();
if (_rightEndPoint == null)
{
_rightEndPoint = remoteEP;
}
errorCode = SocketPal.ReceiveMessageFromAsync(this, _handle, buffer, offset, size, socketFlags, socketAddress, asyncResult);
if (errorCode != SocketError.Success)
{
// WSARecvMsg() will never return WSAEMSGSIZE directly, since a completion is queued in this case. We wouldn't be able
// to handle this easily because of assumptions OverlappedAsyncResult makes about whether there would be a completion
// or not depending on the error code. If WSAEMSGSIZE would have been normally returned, it returns WSA_IO_PENDING instead.
// That same map is implemented here just in case.
if (errorCode == SocketError.MessageSize)
{
NetEventSource.Fail(this, "Returned WSAEMSGSIZE!");
errorCode = SocketError.IOPending;
}
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.WSARecvMsg returns:{errorCode} size:{size} returning AsyncResult:{asyncResult}");
}
catch (ObjectDisposedException)
{
_rightEndPoint = oldEndPoint;
throw;
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint = oldEndPoint;
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
// Capture the context, maybe call the callback, and return.
asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache);
if (asyncResult.CompletedSynchronously && !asyncResult.SocketAddressOriginal.Equals(asyncResult.SocketAddress))
{
try
{
remoteEP = remoteEP.Create(asyncResult.SocketAddress);
}
catch
{
}
}
if (NetEventSource.IsEnabled)
{
NetEventSource.Info(this, $"size:{size} returning AsyncResult:{asyncResult}");
NetEventSource.Exit(this, asyncResult);
}
return asyncResult;
}