From 9d4b9b52b7383795cb8112f5f41a9b85de12bda7 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Tue, 17 Jun 2025 18:31:46 -0700 Subject: [PATCH] clean(error spam when node fails to conenct to controller): now it will print readable message without spamming error --- .../Werewolf Node/Program.cs | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Werewolf for Telegram/Werewolf Node/Program.cs b/Werewolf for Telegram/Werewolf Node/Program.cs index 9aec7ff3..439ea3bf 100644 --- a/Werewolf for Telegram/Werewolf Node/Program.cs +++ b/Werewolf for Telegram/Werewolf Node/Program.cs @@ -1,16 +1,18 @@ -using System; +using Database; +using Microsoft.Win32; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Dynamic; using System.IO; using System.Linq; +using System.Net.Sockets; using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Xml.Linq; -using Microsoft.Win32; -using Newtonsoft.Json; using TcpFramework; using Telegram.Bot; using Telegram.Bot.Types; @@ -427,6 +429,8 @@ internal static void Connect() Client.DataReceived += ClientOnDataReceived; Client.DelimiterDataReceived += ClientOnDelimiterDataReceived; //connection lost, let's try to reconnect + bool hasLoggedRefusedMessage = false; + while (Client.TcpClient == null || !Client.TcpClient.Connected) { try @@ -435,15 +439,34 @@ internal static void Connect() var regInfo = new ClientRegistrationInfo { ClientId = ClientId }; var json = JsonConvert.SerializeObject(regInfo); Client.WriteLine(json); + + Console.WriteLine($"Connected to {Settings.ServerIP}:{Settings.Port}"); } catch (Exception ex) { - while (ex.InnerException != null) - ex = ex.InnerException; - Console.WriteLine($"Error in reconnect: {ex.Message}\n{ex.StackTrace}\n"); + // Dig to root exception + Exception root = ex; + while (root.InnerException != null) + root = root.InnerException; + + if (root is SocketException sockEx && sockEx.SocketErrorCode == SocketError.ConnectionRefused) + { + if (!hasLoggedRefusedMessage) + { + Console.WriteLine($"Waiting for connection at {Settings.ServerIP}:{Settings.Port}... (connection refused — control process might not be started yet)"); + hasLoggedRefusedMessage = true; + } + } + else + { + // Print full error if it's not a connection refused case + Console.WriteLine($"Error in reconnect: {root.Message}\n{root.StackTrace}\n"); + } } + Thread.Sleep(100); } + } public static void KeepAlive()