Merge remote-tracking branch 'origin/pr/278' into sk-2.4.0

pull/286/head
Yaakov 4 years ago
commit e735f1aa61

@ -1,11 +1,10 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using SteamKit2; using SteamKit2.CDN;
namespace DepotDownloader namespace DepotDownloader
{ {
@ -18,11 +17,11 @@ namespace DepotDownloader
private readonly Steam3Session steamSession; private readonly Steam3Session steamSession;
private readonly uint appId; private readonly uint appId;
public CDNClient CDNClient { get; } public Client CDNClient { get; }
public CDNClient.Server ProxyServer { get; private set; } public Server ProxyServer { get; private set; }
private readonly ConcurrentStack<CDNClient.Server> activeConnectionPool; private readonly ConcurrentStack<Server> activeConnectionPool;
private readonly BlockingCollection<CDNClient.Server> availableServerEndpoints; private readonly BlockingCollection<Server> availableServerEndpoints;
private readonly AutoResetEvent populatePoolEvent; private readonly AutoResetEvent populatePoolEvent;
private readonly Task monitorTask; private readonly Task monitorTask;
@ -33,10 +32,10 @@ namespace DepotDownloader
{ {
this.steamSession = steamSession; this.steamSession = steamSession;
this.appId = appId; this.appId = appId;
CDNClient = new CDNClient(steamSession.steamClient); CDNClient = new Client(steamSession.steamClient);
activeConnectionPool = new ConcurrentStack<CDNClient.Server>(); activeConnectionPool = new ConcurrentStack<Server>();
availableServerEndpoints = new BlockingCollection<CDNClient.Server>(); availableServerEndpoints = new BlockingCollection<Server>();
populatePoolEvent = new AutoResetEvent(true); populatePoolEvent = new AutoResetEvent(true);
shutdownToken = new CancellationTokenSource(); shutdownToken = new CancellationTokenSource();
@ -50,32 +49,20 @@ namespace DepotDownloader
monitorTask.Wait(); monitorTask.Wait();
} }
private async Task<IReadOnlyCollection<CDNClient.Server>> FetchBootstrapServerListAsync() private async Task<IReadOnlyCollection<Server>> FetchBootstrapServerListAsync()
{ {
var backoffDelay = 0; try
while (!shutdownToken.IsCancellationRequested)
{ {
try var cdnServers = await this.steamSession.steamContent.GetServersForSteamPipe();
if (cdnServers != null)
{ {
var cdnServers = await ContentServerDirectoryService.LoadAsync(this.steamSession.steamClient.Configuration, ContentDownloader.Config.CellID, shutdownToken.Token); return cdnServers;
if (cdnServers != null)
{
return cdnServers;
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to retrieve content server list: {0}", ex.Message);
if (ex is SteamKitWebRequestException e && e.StatusCode == (HttpStatusCode)429)
{
// If we're being throttled, add a delay to the next request
backoffDelay = Math.Min(5, ++backoffDelay);
await Task.Delay(TimeSpan.FromSeconds(backoffDelay));
}
} }
} }
catch (Exception ex)
{
Console.WriteLine("Failed to retrieve content server list: {0}", ex.Message);
}
return null; return null;
} }
@ -104,7 +91,7 @@ namespace DepotDownloader
var weightedCdnServers = servers var weightedCdnServers = servers
.Where(server => .Where(server =>
{ {
var isEligibleForApp = server.AllowedAppIds == null || server.AllowedAppIds.Contains(appId); var isEligibleForApp = server.AllowedAppIds.Length == 0 || server.AllowedAppIds.Contains(appId);
return isEligibleForApp && (server.Type == "SteamCache" || server.Type == "CDN"); return isEligibleForApp && (server.Type == "SteamCache" || server.Type == "CDN");
}) })
.Select(server => .Select(server =>
@ -133,7 +120,7 @@ namespace DepotDownloader
} }
} }
private CDNClient.Server BuildConnection(CancellationToken token) private Server BuildConnection(CancellationToken token)
{ {
if (availableServerEndpoints.Count < ServerEndpointMinimumSize) if (availableServerEndpoints.Count < ServerEndpointMinimumSize)
{ {
@ -143,7 +130,7 @@ namespace DepotDownloader
return availableServerEndpoints.Take(token); return availableServerEndpoints.Take(token);
} }
public CDNClient.Server GetConnection(CancellationToken token) public Server GetConnection(CancellationToken token)
{ {
if (!activeConnectionPool.TryPop(out var connection)) if (!activeConnectionPool.TryPop(out var connection))
{ {
@ -153,7 +140,7 @@ namespace DepotDownloader
return connection; return connection;
} }
public async Task<string> AuthenticateConnection(uint appId, uint depotId, CDNClient.Server server) public async Task<string> AuthenticateConnection(uint appId, uint depotId, Server server)
{ {
var host = steamSession.ResolveCDNTopLevelHost(server.Host); var host = steamSession.ResolveCDNTopLevelHost(server.Host);
var cdnKey = $"{depotId:D}:{host}"; var cdnKey = $"{depotId:D}:{host}";
@ -169,14 +156,14 @@ namespace DepotDownloader
throw new Exception($"Failed to retrieve CDN token for server {server.Host} depot {depotId}"); throw new Exception($"Failed to retrieve CDN token for server {server.Host} depot {depotId}");
} }
public void ReturnConnection(CDNClient.Server server) public void ReturnConnection(Server server)
{ {
if (server == null) return; if (server == null) return;
activeConnectionPool.Push(server); activeConnectionPool.Push(server);
} }
public void ReturnBrokenConnection(CDNClient.Server server) public void ReturnBrokenConnection(Server server)
{ {
if (server == null) return; if (server == null) return;

@ -4,10 +4,10 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using SteamKit2; using SteamKit2;
using SteamKit2.CDN;
namespace DepotDownloader namespace DepotDownloader
{ {
@ -831,15 +831,15 @@ namespace DepotDownloader
{ {
cts.Token.ThrowIfCancellationRequested(); cts.Token.ThrowIfCancellationRequested();
CDNClient.Server connection = null; Server connection = null;
try try
{ {
connection = cdnPool.GetConnection(cts.Token); connection = cdnPool.GetConnection(cts.Token);
DebugLog.WriteLine("ContentDownloader", "Downloading manifest {0} from {1} with {2}", depot.manifestId, connection, cdnPool.ProxyServer != null ? cdnPool.ProxyServer : "no proxy"); DebugLog.WriteLine("ContentDownloader", "Downloading manifest {0} from {1} with {2}", depot.manifestId, connection, cdnPool.ProxyServer != null ? cdnPool.ProxyServer : "no proxy");
depotManifest = await cdnPool.CDNClient.DownloadManifestAsync(depot.id, depot.manifestId, depotManifest = await cdnPool.CDNClient.DownloadManifestAsync(depot.id, depot.manifestId, manifestRequestCode: 0,
connection, null, depot.depotKey, proxyServer: cdnPool.ProxyServer).ConfigureAwait(false); connection, depot.depotKey, cdnPool.ProxyServer).ConfigureAwait(false);
cdnPool.ReturnConnection(connection); cdnPool.ReturnConnection(connection);
} }
@ -1223,13 +1223,13 @@ namespace DepotDownloader
data.CompressedLength = chunk.CompressedLength; data.CompressedLength = chunk.CompressedLength;
data.UncompressedLength = chunk.UncompressedLength; data.UncompressedLength = chunk.UncompressedLength;
CDNClient.DepotChunk chunkData = null; DepotChunk chunkData = null;
do do
{ {
cts.Token.ThrowIfCancellationRequested(); cts.Token.ThrowIfCancellationRequested();
CDNClient.Server connection = null; Server connection = null;
try try
{ {
@ -1237,7 +1237,7 @@ namespace DepotDownloader
DebugLog.WriteLine("ContentDownloader", "Downloading chunk {0} from {1} with {2}", chunkID, connection, cdnPool.ProxyServer != null ? cdnPool.ProxyServer : "no proxy"); DebugLog.WriteLine("ContentDownloader", "Downloading chunk {0} from {1} with {2}", chunkID, connection, cdnPool.ProxyServer != null ? cdnPool.ProxyServer : "no proxy");
chunkData = await cdnPool.CDNClient.DownloadDepotChunkAsync(depot.id, data, chunkData = await cdnPool.CDNClient.DownloadDepotChunkAsync(depot.id, data,
connection, null, depot.depotKey, proxyServer: cdnPool.ProxyServer).ConfigureAwait(false); connection, depot.depotKey, cdnPool.ProxyServer).ConfigureAwait(false);
cdnPool.ReturnConnection(connection); cdnPool.ReturnConnection(connection);
} }

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@ -40,6 +40,7 @@ namespace DepotDownloader
public SteamClient steamClient; public SteamClient steamClient;
public SteamUser steamUser; public SteamUser steamUser;
public SteamContent steamContent;
readonly SteamApps steamApps; readonly SteamApps steamApps;
readonly SteamCloud steamCloud; readonly SteamCloud steamCloud;
readonly SteamUnifiedMessages.UnifiedService<IPublishedFile> steamPublishedFile; readonly SteamUnifiedMessages.UnifiedService<IPublishedFile> steamPublishedFile;
@ -101,6 +102,7 @@ namespace DepotDownloader
this.steamCloud = this.steamClient.GetHandler<SteamCloud>(); this.steamCloud = this.steamClient.GetHandler<SteamCloud>();
var steamUnifiedMessages = this.steamClient.GetHandler<SteamUnifiedMessages>(); var steamUnifiedMessages = this.steamClient.GetHandler<SteamUnifiedMessages>();
this.steamPublishedFile = steamUnifiedMessages.CreateService<IPublishedFile>(); this.steamPublishedFile = steamUnifiedMessages.CreateService<IPublishedFile>();
this.steamContent = this.steamClient.GetHandler<SteamContent>();
this.callbacks = new CallbackManager(this.steamClient); this.callbacks = new CallbackManager(this.steamClient);

Loading…
Cancel
Save