Skip to content

Commit 9057fe7

Browse files
committed
更改判断下载内容为异步
1 parent b70b9a0 commit 9057fe7

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/dotnetCampus.FileDownloader/Utils/BreakpointResumptionTransmissions/BreakpointResumptionTransmissionManager.Crc64.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
using System;
44
using System.Buffers.Binary;
5+
using System.IO;
6+
using System.Threading.Tasks;
57

68
namespace dotnetCampus.FileDownloader.Utils.BreakpointResumptionTransmissions;
79
internal partial class BreakpointResumptionTransmissionManager
@@ -184,5 +186,37 @@ private static ulong[] GenerateTable(ulong polynomial)
184186
return table;
185187
}
186188
}
189+
190+
static class CrcHelper
191+
{
192+
public static async Task<bool> CheckCrcAsync(Stream stream, ulong expectedCrc, long checkLength, ISharedArrayPool sharedArrayPool, int bufferLength)
193+
{
194+
var buffer = sharedArrayPool.Rent(bufferLength);
195+
try
196+
{
197+
var crc64 = new Crc64();
198+
ulong checksum = 0;
199+
var remainLength = checkLength;
200+
while (remainLength > 0)
201+
{
202+
var readLength = (int) Math.Min(bufferLength, remainLength);
203+
var read = await stream.ReadAsync(buffer, 0, readLength);
204+
if (read != readLength)
205+
{
206+
return false;
207+
}
208+
209+
checksum = crc64.Append(buffer.AsSpan(0, read));
210+
remainLength -= readLength;
211+
}
212+
213+
return checksum == expectedCrc;
214+
}
215+
finally
216+
{
217+
sharedArrayPool.Return(buffer);
218+
}
219+
}
220+
}
187221
}
188222
#endif

src/dotnetCampus.FileDownloader/Utils/BreakpointResumptionTransmissions/BreakpointResumptionTransmissionManager.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task<SegmentManager> CreateSegmentManagerAsync(FileStream downloadF
5959

6060
if (info is not null && info.DownloadLength == DownloadLength && info.DownloadedInfo is not null && info.DownloadedInfo.Count > 0)
6161
{
62-
var downloadSegmentList = GetDownloadSegmentList(info.DownloadedInfo, downloadFileStream);
62+
var downloadSegmentList = await GetDownloadSegmentList(info.DownloadedInfo, downloadFileStream);
6363

6464
var segmentManager = new SegmentManager(downloadSegmentList);
6565
for (var i = 0; i < downloadSegmentList.Count; i++)
@@ -113,7 +113,7 @@ public async Task<SegmentManager> CreateSegmentManagerAsync(FileStream downloadF
113113
/// <param name="downloadedInfo"></param>
114114
/// <param name="downloadFileStream"></param>
115115
/// <returns></returns>
116-
private List<DownloadSegment> GetDownloadSegmentList(List<DataRange> downloadedInfo, FileStream downloadFileStream)
116+
private async Task<List<DownloadSegment>> GetDownloadSegmentList(List<DataRange> downloadedInfo, FileStream downloadFileStream)
117117
{
118118
downloadedInfo.Sort(new DataRangeComparer());
119119
var list = downloadedInfo;
@@ -140,7 +140,7 @@ private List<DownloadSegment> GetDownloadSegmentList(List<DataRange> downloadedI
140140
}
141141

142142
// 需要对原文件进行校验,确保下载下去的和断点续传记录的相同
143-
bool IsDownloaded()
143+
async Task<bool> IsDownloaded()
144144
{
145145
var endPoint = current.StartPoint + current.Length;
146146
if (downloadFileStream.Length < endPoint)
@@ -149,35 +149,13 @@ bool IsDownloaded()
149149
}
150150

151151
downloadFileStream.Seek(current.StartPoint, SeekOrigin.Begin);
152-
var buffer = SharedArrayPool.Rent(BufferLength);
153-
try
154-
{
155-
var crc64 = new Crc64();
156-
ulong checksum = 0;
157-
var remainLength = current.Length;
158-
while (remainLength > 0)
159-
{
160-
var readLength = (int) Math.Min(BufferLength, remainLength);
161-
var read = downloadFileStream.Read(buffer, 0, readLength);
162-
if (read != readLength)
163-
{
164-
return false;
165-
}
166-
167-
checksum = crc64.Append(buffer.AsSpan(0, read));
168-
remainLength -= readLength;
169-
}
170152

171-
return checksum == current.Checksum;
172-
}
173-
finally
174-
{
175-
SharedArrayPool.Return(buffer);
176-
}
153+
return await CrcHelper.CheckCrcAsync(downloadFileStream, current.Checksum, current.Length, SharedArrayPool,
154+
BufferLength);
177155
}
178156

179157
// 如果判断当前不是下载完成的内容,则配置状态不是 Finished 而是需要下载
180-
var isDownloaded = IsDownloaded();
158+
var isDownloaded = await IsDownloaded();
181159

182160
var currentDownloadSegment = new DownloadSegment(current.StartPoint, current.StartPoint + current.Length)
183161
{

0 commit comments

Comments
 (0)