public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
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);
// We don't do a CAS demand here because the contents of remoteEP aren't used by
// WSARecvFrom; all that matters is that we generate a unique-to-this-call SocketAddress
// with the right address family
Internals.SocketAddress socketAddress = SnapshotAndSerialize(ref remoteEP);
// Set up the result and set it to collect the context.
var asyncResult = new OriginalAddressOverlappedAsyncResult(this, state, callback);
asyncResult.StartPostingAsyncOp(false);
// Start the ReceiveFrom.
DoBeginReceiveFrom(buffer, offset, size, socketFlags, remoteEP, socketAddress, asyncResult);
// 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.Exit(this, asyncResult);
return asyncResult;
}