Added try/catch to transport update loop

This commit is contained in:
Derek S 2021-04-08 21:01:49 -05:00
parent b84448ea11
commit 63de39ebf7

View file

@ -106,10 +106,10 @@ namespace LightReflectiveMirror
_currentConnections.Remove(clientID); _currentConnections.Remove(clientID);
_relay.HandleDisconnect(clientID); _relay.HandleDisconnect(clientID);
if(NATConnections.ContainsKey(clientID)) if (NATConnections.ContainsKey(clientID))
NATConnections.Remove(clientID); NATConnections.Remove(clientID);
if(_pendingNATPunches.TryGetByFirst(clientID, out _)) if (_pendingNATPunches.TryGetByFirst(clientID, out _))
_pendingNATPunches.Remove(clientID); _pendingNATPunches.Remove(clientID);
}; };
@ -150,12 +150,12 @@ namespace LightReflectiveMirror
{ {
natThread.Start(); natThread.Start();
} }
catch(Exception e) catch (Exception e)
{ {
WriteLogMessage("FAILED\n" + e, ConsoleColor.DarkRed); WriteLogMessage("FAILED\n" + e, ConsoleColor.DarkRed);
} }
} }
catch(Exception e) catch (Exception e)
{ {
WriteLogMessage("FAILED\nCheck if port is in use.", ConsoleColor.DarkRed, true); WriteLogMessage("FAILED\nCheck if port is in use.", ConsoleColor.DarkRed, true);
Console.WriteLine(e); Console.WriteLine(e);
@ -169,7 +169,7 @@ namespace LightReflectiveMirror
Environment.Exit(0); Environment.Exit(0);
} }
} }
catch(Exception e) catch (Exception e)
{ {
WriteLogMessage("FAILED\nException: " + e, ConsoleColor.DarkRed); WriteLogMessage("FAILED\nException: " + e, ConsoleColor.DarkRed);
Console.ReadKey(); Console.ReadKey();
@ -181,17 +181,25 @@ namespace LightReflectiveMirror
} }
while (true) while (true)
{
try
{ {
if (_updateMethod != null) _updateMethod.Invoke(transport, null); if (_updateMethod != null) _updateMethod.Invoke(transport, null);
if (_lateUpdateMethod != null) _lateUpdateMethod.Invoke(transport, null); if (_lateUpdateMethod != null) _lateUpdateMethod.Invoke(transport, null);
}
catch (Exception e)
{
WriteLogMessage("Error During Transport Update! " + e, ConsoleColor.Red);
}
_currentHeartbeatTimer++; _currentHeartbeatTimer++;
if(_currentHeartbeatTimer >= conf.UpdateHeartbeatInterval) if (_currentHeartbeatTimer >= conf.UpdateHeartbeatInterval)
{ {
_currentHeartbeatTimer = 0; _currentHeartbeatTimer = 0;
for(int i = 0; i < _currentConnections.Count; i++) for (int i = 0; i < _currentConnections.Count; i++)
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 })); transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 }));
if (conf.UseLoadBalancer) if (conf.UseLoadBalancer)
@ -220,7 +228,7 @@ namespace LightReflectiveMirror
await wc.DownloadStringTaskAsync($"http://{conf.LoadBalancerAddress}:{conf.LoadBalancerPort}/api/roomsupdated"); await wc.DownloadStringTaskAsync($"http://{conf.LoadBalancerAddress}:{conf.LoadBalancerPort}/api/roomsupdated");
} }
} }
catch {} // LLB might be down, ignore. catch { } // LLB might be down, ignore.
} }
private async Task<bool> RegisterSelfToLoadBalancer() private async Task<bool> RegisterSelfToLoadBalancer()