From 2602ac362332eb188e8db2e4253c7effb16b6c5b Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 22 Mar 2016 20:18:47 +0100 Subject: [PATCH] Improve Start/Stop mechanisms --- ArchiSteamFarm/Bot.cs | 58 +++++++++++++------------------------------ 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 5e8c94c2c..a764f8c59 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -308,7 +308,6 @@ namespace ArchiSteamFarm { return; } - await Stop().ConfigureAwait(false); await Start().ConfigureAwait(false); } @@ -318,7 +317,7 @@ namespace ArchiSteamFarm { } if (BotConfig.ShutdownOnFarmingFinished) { - await Shutdown().ConfigureAwait(false); + Stop(); } } @@ -354,7 +353,7 @@ namespace ArchiSteamFarm { case "!statusall": return ResponseStatusAll(steamID); case "!stop": - return await ResponseStop(steamID).ConfigureAwait(false); + return ResponseStop(steamID); case "!update": return await ResponseUpdate(steamID).ConfigureAwait(false); default: @@ -402,7 +401,7 @@ namespace ArchiSteamFarm { case "!status": return ResponseStatus(steamID, args[1]); case "!stop": - return await ResponseStop(steamID, args[1]).ConfigureAwait(false); + return ResponseStop(steamID, args[1]); default: return ResponseUnknown(steamID); } @@ -410,47 +409,24 @@ namespace ArchiSteamFarm { } private async Task Start() { - if (SteamClient.IsConnected) { - return; - } - if (!KeepRunning) { KeepRunning = true; Task.Run(() => HandleCallbacks()).Forget(); } - Logging.LogGenericInfo("Starting...", BotName); - // 2FA tokens are expiring soon, use limiter only when we don't have any pending if (TwoFactorAuth == null) { await Program.LimitSteamRequestsAsync().ConfigureAwait(false); } + Logging.LogGenericInfo("Starting...", BotName); SteamClient.Connect(); } - private async Task Stop() { - if (!SteamClient.IsConnected) { - return; - } - + private void Stop() { Logging.LogGenericInfo("Stopping...", BotName); - - for (byte i = 0; i < WebBrowser.MaxRetries && SteamClient.IsConnected; i++) { - SteamClient.Disconnect(); - await Utilities.SleepAsync(1000).ConfigureAwait(false); - } - - if (SteamClient.IsConnected) { - Logging.LogGenericWarning("Could not stop this bot instance!", BotName); - } else { - Logging.LogGenericInfo("Stopped!", BotName); - } - } - - private async Task Shutdown() { KeepRunning = false; - await Stop().ConfigureAwait(false); + SteamClient.Disconnect(); Program.OnBotShutdown(); } @@ -1118,7 +1094,7 @@ namespace ArchiSteamFarm { return await bot.ResponseStart(steamID).ConfigureAwait(false); } - private async Task ResponseStop(ulong steamID) { + private string ResponseStop(ulong steamID) { if (steamID == 0) { return null; } @@ -1131,11 +1107,11 @@ namespace ArchiSteamFarm { return "That bot instance is already inactive!"; } - await Shutdown().ConfigureAwait(false); + Stop(); return "Done!"; } - private static async Task ResponseStop(ulong steamID, string botName) { + private static string ResponseStop(ulong steamID, string botName) { if (steamID == 0 || string.IsNullOrEmpty(botName)) { return null; } @@ -1145,7 +1121,7 @@ namespace ArchiSteamFarm { return "Couldn't find any bot named " + botName + "!"; } - return await bot.ResponseStop(steamID).ConfigureAwait(false); + return bot.ResponseStop(steamID); } private string ResponseUnknown(ulong steamID) { @@ -1347,17 +1323,17 @@ namespace ArchiSteamFarm { } Logging.LogGenericInfo("Disconnected from Steam!", BotName); - await CardsFarmer.StopFarming().ConfigureAwait(false); - - if (!KeepRunning) { - return; - } + CardsFarmer.StopFarming().Forget(); // If we initiated disconnect, do not attempt to reconnect if (callback.UserInitiated) { return; } + if (!KeepRunning) { + return; + } + if (InvalidPassword) { InvalidPassword = false; if (!string.IsNullOrEmpty(BotDatabase.LoginKey)) { // InvalidPassword means usually that login key has expired, if we used it @@ -1371,7 +1347,7 @@ namespace ArchiSteamFarm { LoggedInElsewhere = false; if (Program.GlobalConfig.AccountPlayingDelay == 0) { - Shutdown().Forget(); + Stop(); return; } @@ -1607,7 +1583,7 @@ namespace ArchiSteamFarm { break; default: // Unexpected result, shutdown immediately Logging.LogGenericWarning("Unable to login to Steam: " + callback.Result, BotName); - await Shutdown().ConfigureAwait(false); + Stop(); break; } }