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

@ -15,6 +15,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="protobuf-net" Version="3.2.30" /> <PackageReference Include="protobuf-net" Version="3.2.30" />
<PackageReference Include="QRCoder" Version="1.6.0" /> <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> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save