Update DownloadDepotChunkAsync to use array pool

pull/526/head
Pavel Djundik 1 year ago
parent a3fc9c4c45
commit baa64e9e51

@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
@ -1199,8 +1200,11 @@ namespace DepotDownloader
UncompressedLength = chunk.UncompressedLength
};
DepotChunk chunkData = null;
var written = 0;
var chunkBuffer = ArrayPool<byte>.Shared.Rent((int)data.UncompressedLength);
try
{
do
{
cts.Token.ThrowIfCancellationRequested();
@ -1212,14 +1216,17 @@ namespace DepotDownloader
connection = cdnPool.GetConnection(cts.Token);
DebugLog.WriteLine("ContentDownloader", "Downloading chunk {0} from {1} with {2}", chunkID, connection, cdnPool.ProxyServer != null ? cdnPool.ProxyServer : "no proxy");
chunkData = await cdnPool.CDNClient.DownloadDepotChunkAsync(
written = await cdnPool.CDNClient.DownloadDepotChunkAsync(
depot.DepotId,
data,
connection,
chunkBuffer,
depot.DepotKey,
cdnPool.ProxyServer).ConfigureAwait(false);
cdnPool.ReturnConnection(connection);
break;
}
catch (TaskCanceledException)
{
@ -1246,9 +1253,9 @@ namespace DepotDownloader
cdnPool.ReturnBrokenConnection(connection);
Console.WriteLine("Encountered unexpected error downloading chunk {0}: {1}", chunkID, e.Message);
}
} while (chunkData == null);
} while (written == 0);
if (chunkData == null)
if (written == 0)
{
Console.WriteLine("Failed to find any server with chunk {0} for depot {1}. Aborting.", chunkID, depot.DepotId);
cts.Cancel();
@ -1267,13 +1274,18 @@ namespace DepotDownloader
fileStreamData.fileStream = File.Open(fileFinalPath, FileMode.Open);
}
fileStreamData.fileStream.Seek((long)chunkData.ChunkInfo.Offset, SeekOrigin.Begin);
await fileStreamData.fileStream.WriteAsync(chunkData.Data.AsMemory(0, chunkData.Data.Length), cts.Token);
fileStreamData.fileStream.Seek((long)data.Offset, SeekOrigin.Begin);
await fileStreamData.fileStream.WriteAsync(chunkBuffer.AsMemory(0, written), cts.Token);
}
finally
{
fileStreamData.fileLock.Release();
}
}
finally
{
ArrayPool<byte>.Shared.Return(chunkBuffer);
}
var remainingChunks = Interlocked.Decrement(ref fileStreamData.chunksToDownload);
if (remainingChunks == 0)
@ -1285,7 +1297,7 @@ namespace DepotDownloader
ulong sizeDownloaded = 0;
lock (depotDownloadCounter)
{
sizeDownloaded = depotDownloadCounter.sizeDownloaded + (ulong)chunkData.Data.Length;
sizeDownloaded = depotDownloadCounter.sizeDownloaded + (ulong)written;
depotDownloadCounter.sizeDownloaded = sizeDownloaded;
depotDownloadCounter.depotBytesCompressed += chunk.CompressedLength;
depotDownloadCounter.depotBytesUncompressed += chunk.UncompressedLength;

@ -15,6 +15,6 @@
<ItemGroup>
<PackageReference Include="protobuf-net" Version="3.2.30" />
<PackageReference Include="QRCoder" Version="1.6.0" />
<PackageReference Include="SteamKit2" Version="3.0.0-Beta.1" />
<PackageReference Include="SteamKit2" Version="3.0.0-Beta.2" />
</ItemGroup>
</Project>

Loading…
Cancel
Save