private void ContinuousNATPunchthrough(BackgroundNATPunchthrough punchthrough)
{
var natPunchthroughApi = new NATPunchthroughApi();
natPunchthroughApi.Configuration.ApiKey["api_key"] = punchthrough.UserSession.ApiKey;
while (!punchthrough.ShouldStop)
{
var udpClient = punchthrough.UdpClient;
while (true)
{
NATNegotation negotation;
try
{
negotation = natPunchthroughApi.PunchthroughPut(punchthrough.UserSession.Id);
}
catch
{
continue;
}
if (negotation.Port == null)
{
throw new InvalidOperationException();
}
udpClient.Send(
negotation.Message,
negotation.Message.Length,
negotation.Host,
negotation.Port.Value);
Thread.Sleep(1000);
try
{
if (natPunchthroughApi.PunchthroughGet(punchthrough.UserSession.Id) == true)
{
// NAT punchthrough completed successfully.
continue;
}
}
catch
{
continue;
}
Thread.Sleep(1000);
}
}
}