Skip to content

Commit

Permalink
Add proper support for server list provider cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Oct 21, 2024
1 parent 6bc29e8 commit 411c49a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 0 additions & 4 deletions ArchiSteamFarm/Core/ASF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,6 @@ private static async Task RegisterBots() {
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<string> botNames;

try {
Expand Down
3 changes: 3 additions & 0 deletions ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ private Bot(string botName, BotConfig botConfig, BotDatabase botDatabase) {
}
);

// Decrease the ServerList cache in order to fight with Steam gibberish data
SteamConfiguration.ServerList.ServerListBeforeRefreshTimeSpan = TimeSpan.FromHours(1);

// Initialize
SteamClient = new SteamClient(SteamConfiguration, botName);

Expand Down
16 changes: 9 additions & 7 deletions ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@
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<ServerRecordEndPoint> ServerRecords { get; init; } = [];
private ConcurrentList<ServerRecordEndPoint> ServerRecords { get; init; } = [];

public Task<IEnumerable<ServerRecord>> 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<ServerRecord> endpoints) {
ArgumentNullException.ThrowIfNull(endpoints);

HashSet<ServerRecordEndPoint> newServerRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes)).ToHashSet();
LastServerListRefresh = DateTime.UtcNow;

IEnumerable<ServerRecordEndPoint> serverRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes));

ServerRecords.ReplaceWith(serverRecords);

if (ServerRecords.ReplaceIfNeededWith(newServerRecords)) {
ServerListUpdated?.Invoke(this, EventArgs.Empty);
}
ServerListUpdated?.Invoke(this, EventArgs.Empty);

return Task.CompletedTask;
}
Expand Down

0 comments on commit 411c49a

Please sign in to comment.