Skip to content

Commit

Permalink
Add more logging in heartbeat thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Crotalus committed Jul 29, 2024
1 parent f2c29c2 commit 2eef4cb
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/Faforever.Qai.Irc/QaIrc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void Run()
_logger.LogInformation("Connecting to IRC server {0} ({1}), port {2}", _hostname, address, port);
_client.Connect(_hostname, false, _userInfo);

_logger.Log(LogLevel.Debug, "Starting heartbeat thread...");
_heartbeatThread = new Thread(HeartbeatThread);
_heartbeatThread.Start();
}
Expand Down Expand Up @@ -173,7 +174,7 @@ private void OnClientRegistered(object sender, EventArgs args)
private bool connecting;
private void OnClientDisconnected(object sender, EventArgs args)
{
_logger.Log(LogLevel.Information, "client disconnected");
_logger.Log(LogLevel.Critical, "client disconnected");
connecting = false;
}

Expand All @@ -193,7 +194,7 @@ private void OnError(object sender, IrcErrorEventArgs args)

private void OnSaslMessage(object sender, IrcSaslMessageEventArgs e)
{
_logger.Log(LogLevel.Critical, "SASL Message: {message}", e.Message);
_logger.Log(LogLevel.Information, "SASL Message: {message}", e.Message);
if (e.Code == 904)
nextConnectAttempt = DateTime.Now.AddSeconds(10);
}
Expand Down Expand Up @@ -237,30 +238,41 @@ private void HeartbeatThread()
{
const int PING_INTERVAL = 60;

_logger.Log(LogLevel.Debug, "Heartbeat thread started");
// Ping the server regulary to detect if the socket is dead
_logger.LogInformation("Heartbeat thread started");
// Ping the server regularly to detect if the socket is dead

DateTime nextPing = DateTime.Now.AddSeconds(PING_INTERVAL);

while(true)
try
{
if(!_client.IsConnected)
{
if (!connecting)
TryReconnect();
}
else
while (true)
{
if (nextPing < DateTime.Now)
if (!_client.IsConnected)
{
_logger.LogDebug("Client is not connected");
if (!connecting)
{
_logger.LogInformation("Attempting to reconnect");
TryReconnect();
}
}
else
{
_logger.Log(LogLevel.Debug, "Pinging {_hostname}", _hostname);
_client.Ping(_hostname);
if (nextPing < DateTime.Now)
{
_logger.LogDebug("Pinging {_hostname}", _hostname);
_client.Ping(_hostname);

nextPing = DateTime.Now.AddSeconds(PING_INTERVAL);
nextPing = DateTime.Now.AddSeconds(PING_INTERVAL);
}
}
}

Thread.Sleep(1000);
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception in heartbeat thread");
}
}
}
Expand Down

0 comments on commit 2eef4cb

Please sign in to comment.