From 411c49adddf83d7228b496e9012234f6d4c0b9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Mon, 21 Oct 2024 12:56:17 +0200 Subject: [PATCH] Add proper support for server list provider cache --- ArchiSteamFarm/Core/ASF.cs | 4 ---- ArchiSteamFarm/Steam/Bot.cs | 3 +++ .../SteamKit2/InMemoryServerListProvider.cs | 16 +++++++++------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index c5c54fc54..bba1ca5ec 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -675,10 +675,6 @@ public static class ASF { throw new InvalidOperationException(nameof(WebBrowser)); } - // Kill SK2 servers cache in order to force refresh during initial connection - // TODO: This should be removed when SK2 learns to purge its stale cache itself - await GlobalDatabase.ServerListProvider.UpdateServerListAsync([]).ConfigureAwait(false); - HashSet botNames; try { diff --git a/ArchiSteamFarm/Steam/Bot.cs b/ArchiSteamFarm/Steam/Bot.cs index 4efb3e6f0..658bd19fe 100644 --- a/ArchiSteamFarm/Steam/Bot.cs +++ b/ArchiSteamFarm/Steam/Bot.cs @@ -353,6 +353,9 @@ public sealed class Bot : IAsyncDisposable, IDisposable { } ); + // Decrease the ServerList cache in order to fight with Steam gibberish data + SteamConfiguration.ServerList.ServerListBeforeRefreshTimeSpan = TimeSpan.FromHours(1); + // Initialize SteamClient = new SteamClient(SteamConfiguration, botName); diff --git a/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs b/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs index e67b0fc30..cf6ae5f6a 100644 --- a/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs +++ b/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs @@ -34,23 +34,25 @@ using SteamKit2.Discovery; namespace ArchiSteamFarm.Steam.SteamKit2; internal sealed class InMemoryServerListProvider : IServerListProvider { - // TODO - public DateTime LastServerListRefresh => DateTime.MinValue; + [JsonInclude] + public DateTime LastServerListRefresh { get; private set; } [JsonDisallowNull] [JsonInclude] - private ConcurrentHashSet ServerRecords { get; init; } = []; + private ConcurrentList ServerRecords { get; init; } = []; public Task> FetchServerListAsync() => Task.FromResult(ServerRecords.Where(static server => !string.IsNullOrEmpty(server.Host) && server is { Port: > 0, ProtocolTypes: > 0 }).Select(static server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes))); public Task UpdateServerListAsync(IEnumerable endpoints) { ArgumentNullException.ThrowIfNull(endpoints); - HashSet newServerRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes)).ToHashSet(); + LastServerListRefresh = DateTime.UtcNow; - if (ServerRecords.ReplaceIfNeededWith(newServerRecords)) { - ServerListUpdated?.Invoke(this, EventArgs.Empty); - } + IEnumerable serverRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes)); + + ServerRecords.ReplaceWith(serverRecords); + + ServerListUpdated?.Invoke(this, EventArgs.Empty); return Task.CompletedTask; }