private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false)
{
int port;
Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp);
using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port))
{
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < (expectedToTimeout ? 1 : TestSettings.UDPRedundancy); i++)
{
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.UserToken = waitHandle;
args.Completed += AsyncCompleted;
bool async = client.SendToAsync(args);
if (async)
{
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
}
Assert.Equal(1, args.BytesTransferred);
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
}
}
bool success = server.WaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout); // Make sure the bytes were received
if (!success)
{
throw new TimeoutException();
}
}
}