-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
下载限速 #492
下载限速 #492
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -733,6 +733,7 @@ private async Task<List<DownloadTask>> GetPkgVersionsAsync(string url) | |
public async Task DownloadAsync(CancellationToken cancellationToken) | ||
{ | ||
const int bufferSize = 1 << 16; | ||
int speed = AppConfig.DowlondSpeed * 500; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数字 500 含义不明 |
||
try | ||
{ | ||
State = DownloadGameState.Downloading; | ||
|
@@ -778,8 +779,32 @@ public async Task DownloadAsync(CancellationToken cancellationToken) | |
|
||
var buffer = new byte[bufferSize]; | ||
int length; | ||
long total_read = 0; | ||
DateTime begin = DateTime.Now; | ||
while ((length = await hs.ReadAsync(buffer, token).ConfigureAwait(false)) != 0) | ||
{ | ||
if (AppConfig.DowlondSpeed != 100) | ||
{ | ||
total_read += bufferSize; | ||
double totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds; | ||
double byteper = total_read / totalSeconds; | ||
if (double.IsInfinity(byteper)) | ||
{ | ||
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false); | ||
Interlocked.Add(ref progressBytes, length); | ||
continue; | ||
} | ||
else | ||
{ | ||
double speedPer = byteper / 1024; | ||
if (speedPer >= speed) | ||
{ | ||
double tempData = ((speedPer - speed) < 1) ? 1 : speedPer - speed; | ||
int sleepMS = Convert.ToInt32(tempData * (1000 / speed) + 100); | ||
Thread.Sleep((sleepMS > 1000) ? 1000 : sleepMS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用 |
||
} | ||
} | ||
} | ||
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false); | ||
Interlocked.Add(ref progressBytes, length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为这部分没有移动到 |
||
} | ||
|
@@ -837,6 +862,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken) | |
public async Task DownloadSeparateFilesAsync(CancellationToken cancellationToken) | ||
{ | ||
const int bufferSize = 1 << 16; | ||
int speed = AppConfig.DowlondSpeed * 500; | ||
try | ||
{ | ||
State = DownloadGameState.Downloading; | ||
|
@@ -883,8 +909,32 @@ public async Task DownloadSeparateFilesAsync(CancellationToken cancellationToken | |
|
||
var buffer = new byte[bufferSize]; | ||
int length; | ||
long total_read = 0; | ||
DateTime begin = DateTime.Now; | ||
while ((length = await hs.ReadAsync(buffer, token).ConfigureAwait(false)) != 0) | ||
{ | ||
if (AppConfig.DowlondSpeed != 100) | ||
{ | ||
total_read += bufferSize; | ||
double totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds; | ||
double byteper = total_read / totalSeconds; | ||
if (double.IsInfinity(byteper)) | ||
{ | ||
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false); | ||
Interlocked.Add(ref progressBytes, length); | ||
continue; | ||
} | ||
else | ||
{ | ||
double speedPer = byteper / 1024; | ||
if (speedPer >= speed) | ||
{ | ||
double tempData = ((speedPer - speed) < 1) ? 1 : speedPer - speed; | ||
int sleepMS = Convert.ToInt32(tempData * (1000 / speed) + 100); | ||
Thread.Sleep((sleepMS > 1000) ? 1000 : sleepMS); | ||
} | ||
} | ||
} | ||
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false); | ||
Interlocked.Add(ref progressBytes, length); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你的方法中
DownloadSpeed
参数仅限制了单线程的下载速度,多线程的下载速度总值会远远超过设定值。