private static void ThreadPing(object hostObj)
{
#if NETFX_CORE
#else
HostInfo host = (HostInfo)((object[])hostObj)[0];
IPAddress address = IPAddress.Parse(host.ipAddress);
UdpClient Client = new UdpClient();
IPEndPoint ep = new IPEndPoint(address, host.port);
Client.Send(new byte[1], 1, ep);
DateTime start = DateTime.Now;
int counter = 0;
int maxTries = 50;
do
{
if (Client.Available != 0)
{
Client.Receive(ref ep);
if (ep.Address.ToString() == host.ipAddress && ep.Port == host.port)
break;
}
if (counter++ >= maxTries)
return; // TODO: Fire off a failed event
System.Threading.Thread.Sleep(1000);
Client.Send(new byte[1], 1, new IPEndPoint(address, host.port));
start = System.DateTime.Now;
} while (true);
int time = (int)(System.DateTime.Now - start).TotalMilliseconds;
host.SetPing(time);
if (pingReceivedInvoker != null)
pingReceivedInvoker(host, time);
#endif
}