private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
EndPoint receivedFrom = new IPEndPoint(connectTo, port);
SocketFlags socketFlags = SocketFlags.None;
IPPacketInformation ipPacketInformation;
IAsyncResult async = serverSocket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);
// Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.
// IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
// Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
// Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout);
if (!success)
{
throw new TimeoutException();
}
receivedFrom = new IPEndPoint(connectTo, port);
int received = serverSocket.EndReceiveMessageFrom(async, ref socketFlags, ref receivedFrom, out ipPacketInformation);
Assert.Equal(1, received);
Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));
IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
Assert.Equal(SocketFlags.None, socketFlags);
Assert.NotNull(ipPacketInformation);
Assert.Equal(connectTo, ipPacketInformation.Address);
}
}