Skip to content

Commit

Permalink
CDN client tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuclearistt committed Feb 4, 2024
1 parent 061d149 commit f7e30dc
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/CDNClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace TEKSteamClient;
public class CDNClient
{
/// <summary>CDN server list.</summary>
private Uri[] _servers = [];
private string[] _servers = [];
/// <summary>HTTP client that downloads manifests and patches from the CDN.</summary>
private readonly HttpClient s_client = new() { DefaultRequestVersion = HttpVersion.Version20, Timeout = TimeSpan.FromSeconds(10) };
/// <summary>Decryption keys to use for decrypting depots' content.</summary>
Expand Down Expand Up @@ -50,9 +50,9 @@ private void CheckServerList()
}
return result;
});
_servers = new Uri[servers.Count];
_servers = new string[servers.Count];
for (int i = 0; i < servers.Count; i++)
_servers[i] = new(string.Concat("https://", servers[i].Host));
_servers[i] = servers[i].Host;
}
private static void DownloadThreadProcedure(object? arg)
{
Expand All @@ -62,8 +62,6 @@ private static void DownloadThreadProcedure(object? arg)
aes.Key = DepotDecryptionKeys[context.SharedContext.DepotId];
var lzmaDecoder = new Utils.LZMA.Decoder();
var httpClient = context.SharedContext.HttpClients[context.Index];
var requestUri = new Uri($"depot/{context.SharedContext.DepotId}/chunk/0000000000000000000000000000000000000000", UriKind.Relative);
ref byte uriGid = ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(requestUri.ToString().AsSpan()[^40..]));
var token = context.Cts.Token;
var downloadBuffer = new Memory<byte>(buffer, 0, 0x200000);
var bufferSpan = new Span<byte>(buffer);
Expand All @@ -82,9 +80,9 @@ private static void DownloadThreadProcedure(object? arg)
if (context.ChunkContext.FilePath is null)
return;
}
Unsafe.CopyBlockUnaligned(ref uriGid, ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(context.ChunkContext.Gid.ToString().AsSpan())), 80);
int compressedSize = context.ChunkContext.CompressedSize;
int uncompressedSize = context.ChunkContext.UncompressedSize;
var uri = new Uri(context.ChunkContext.Gid.ToString(), UriKind.Relative);
Exception? exception = null;
var dataSpan = (ReadOnlySpan<byte>)bufferSpan[16..compressedSize];
var uncompressedDataSpan = bufferSpan[..uncompressedSize];
Expand All @@ -95,7 +93,7 @@ private static void DownloadThreadProcedure(object? arg)
try
{
//Download encrypted chunk data
var request = new HttpRequestMessage(HttpMethod.Get, requestUri) { Version = HttpVersion.Version20 };
var request = new HttpRequestMessage(HttpMethod.Get, uri) { Version = HttpVersion.Version20 };
using var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).GetAwaiter().GetResult();
using var content = response.EnsureSuccessStatusCode().Content;
using var stream = content.ReadAsStream(token);
Expand Down Expand Up @@ -192,7 +190,7 @@ internal void DownloadContent(ItemState state, DepotManifest manifest, DepotDelt
for (int i = 0; i < _servers.Length; i++)
sharedContext.HttpClients[i] = new()
{
BaseAddress = _servers[i],
BaseAddress = new($"https://{_servers[i]}/depot/{state.Id}/chunk/"),
DefaultRequestVersion = HttpVersion.Version20,
Timeout = TimeSpan.FromSeconds(10)
};
Expand Down Expand Up @@ -222,7 +220,7 @@ internal void DownloadContent(ItemState state, DepotManifest manifest, DepotDelt
ProgressInitiated?.Invoke(ProgressType.Binary, delta.DownloadSize, state.DisplayProgress);
var threads = new Thread[contexts.Length];
for (int i = 0; i < threads.Length; i++)
threads[i] = new(DownloadThreadProcedure);
threads[i] = new(DownloadThreadProcedure) { Name = $"{state.Id} Download Thread {i}" };
for (int i = 0; i < threads.Length; i++)
threads[i].UnsafeStart(contexts[i]);
foreach (var thread in threads)
Expand Down Expand Up @@ -358,7 +356,7 @@ public DepotManifest GetManifest(uint appId, ItemIdentifier item, ulong manifest
foreach (var server in _servers)
try
{
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{server}depot/{item.DepotId}/manifest/{manifestId}/5/{requestCode}")) { Version = HttpVersion.Version20 };
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"https://{server}/depot/{item.DepotId}/manifest/{manifestId}/5/{requestCode}")) { Version = HttpVersion.Version20 };
using var response = s_client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).Result.EnsureSuccessStatusCode();
uint? crc = uint.TryParse(response.Headers.TryGetValues("x-content-crc", out var headerValue) ? headerValue.FirstOrDefault() : null, out uint value) ? value : null;
using var content = response.Content;
Expand Down Expand Up @@ -421,7 +419,7 @@ public DepotPatch GetPatch(uint appId, ItemIdentifier item, DepotManifest source
foreach (var server in _servers)
try
{
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{server}depot/{item.DepotId}/patch/{sourceManifest.Id}/{targetManifest.Id}")) { Version = HttpVersion.Version20 };
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"https://{server}/depot/{item.DepotId}/patch/{sourceManifest.Id}/{targetManifest.Id}")) { Version = HttpVersion.Version20 };
using var response = s_client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).Result.EnsureSuccessStatusCode();
uint? crc = uint.TryParse(response.Headers.TryGetValues("x-content-crc", out var headerValue) ? headerValue.FirstOrDefault() : null, out uint value) ? value : null;
using var content = response.Content;
Expand Down

0 comments on commit f7e30dc

Please sign in to comment.