From 527641439b768d4cf26e14c07423ce48c73d75fb Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 8 Aug 2016 20:06:20 +0200 Subject: [PATCH] Implement enhanced HeartBeat The objective of this feature is to detect network malfunctions as well as SK2 connection issues early and initiate a reconnect as soon as possible, instead of relying on failures in SK2 code. This is because those failures are very usually coming too late, when connection was already lost for a dozen or more minutes behind, and it also increases likehood of getting weird SK2 freezes like the one in #318. Therefore, let's see how it works, it's possible that I'll revert it later when SK2 code improves or we find a better way to do that. The introduced overhead both CPU-wise and bandwidth-wise is negligible. --- ArchiSteamFarm/Bot.cs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index a71fea2df..b1e6b52da 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -68,7 +68,7 @@ namespace ArchiSteamFarm { private readonly SteamApps SteamApps; private readonly SteamFriends SteamFriends; private readonly SteamUser SteamUser; - private readonly Timer AcceptConfirmationsTimer, SendItemsTimer; + private readonly Timer AcceptConfirmationsTimer, HeartBeatTimer, SendItemsTimer; private readonly Trading Trading; [JsonProperty] @@ -230,6 +230,13 @@ namespace ArchiSteamFarm { CardsFarmer = new CardsFarmer(this); Trading = new Trading(this); + HeartBeatTimer = new Timer( + async e => await HeartBeat().ConfigureAwait(false), + null, + TimeSpan.FromMinutes(1), // Delay + TimeSpan.FromMinutes(1) // Period + ); + if ((AcceptConfirmationsTimer == null) && (BotConfig.AcceptConfirmationsPeriod > 0)) { AcceptConfirmationsTimer = new Timer( async e => await AcceptConfirmations(true).ConfigureAwait(false), @@ -267,6 +274,7 @@ namespace ArchiSteamFarm { AcceptConfirmationsTimer?.Dispose(); ArchiWebHandler?.Dispose(); CardsFarmer?.Dispose(); + HeartBeatTimer?.Dispose(); SendItemsTimer?.Dispose(); Trading?.Dispose(); } @@ -486,6 +494,23 @@ namespace ArchiSteamFarm { } } + private async Task HeartBeat() { + if (!SteamClient.IsConnected) { + return; + } + + try { + await SteamApps.PICSGetProductInfo(0, null); + } catch { + if (!SteamClient.IsConnected) { + return; + } + + Logging.LogGenericWarning("Connection to Steam Network lost, reconnecting...", BotName); + SteamClient.Connect(); + } + } + private async Task Start() { if (!KeepRunning) { KeepRunning = true;