diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 181b579ff..eecabfce6 100644 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -210,9 +210,11 @@ namespace ArchiSteamFarm { return; } - SteamClient.Connect(); IsRunning = true; + Program.LimitSteamRequests(); + SteamClient.Connect(); + Task.Run(() => HandleCallbacks()); } @@ -368,7 +370,7 @@ namespace ArchiSteamFarm { } Logging.LogGenericWarning(BotName, "Disconnected from Steam, reconnecting..."); - await Utilities.SleepAsync(CallbackSleep).ConfigureAwait(false); + await Program.LimitSteamRequestsAsync().ConfigureAwait(false); SteamClient.Connect(); } @@ -528,7 +530,6 @@ namespace ArchiSteamFarm { case EResult.TryAnotherCM: Logging.LogGenericWarning(BotName, "Unable to login to Steam: " + callback.Result + " / " + callback.ExtendedResult + ", retrying..."); await Stop().ConfigureAwait(false); - await Utilities.SleepAsync(CallbackSleep).ConfigureAwait(false); Start(); break; default: diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 18eb6d154..4ad1a86e4 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -44,6 +44,7 @@ namespace ArchiSteamFarm { private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest"; internal static readonly object ConsoleLock = new object(); + private static readonly SemaphoreSlim SteamSemaphore = new SemaphoreSlim(1); private static readonly ManualResetEvent ShutdownResetEvent = new ManualResetEvent(false); private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); private static readonly string ExecutablePath = Assembly.Location; @@ -89,6 +90,18 @@ namespace ArchiSteamFarm { Environment.Exit(0); } + internal static async Task LimitSteamRequestsAsync() { + await SteamSemaphore.WaitAsync().ConfigureAwait(false); + await Utilities.SleepAsync(Utilities.GetRandomDelay() * 1000).ConfigureAwait(false); // We must add some delay to not get caught by Steam anty-DoS + SteamSemaphore.Release(); + } + + internal static void LimitSteamRequests() { + SteamSemaphore.Wait(); + Thread.Sleep(Utilities.GetRandomDelay() * 1000); // We must add some delay to not get caught by Steam anty-DoS + SteamSemaphore.Release(); + } + internal static string GetUserInput(string botLogin, EUserInputType userInputType) { string result; lock (ConsoleLock) { @@ -150,9 +163,8 @@ namespace ArchiSteamFarm { if (!bot.Enabled) { Logging.LogGenericInfo(botName, "Not starting this instance because it's disabled in config file"); } - Random random = new Random(); - int randomNumber = random.Next(5, 15); // pick random number between 5 and 15 - Thread.Sleep(randomNumber*1000); // Try to avoid spamming steam + + LimitSteamRequests(); // We must add some delay to not get caught by Steam anty-DoS } // Check if we got any bots running diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index f73b401af..a598d5c0f 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -35,10 +35,16 @@ using System.Threading.Tasks; namespace ArchiSteamFarm { internal static class Utilities { + private static readonly Random Random = new Random(); + internal static async Task SleepAsync(int miliseconds) { await Task.Delay(miliseconds).ConfigureAwait(false); } + internal static int GetRandomDelay() { + return Random.Next(5, 16); + } + internal static ulong OnlyNumbers(string inputString) { if (string.IsNullOrEmpty(inputString)) { return 0;