Skip to content

Commit 9ae1493

Browse files
committed
Fix torrent downloader speed calculation
1 parent f764517 commit 9ae1493

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/Assets/Scripts/Net/TorrentDownloader.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
5+
using System.Linq;
46
using System.Threading;
57
using Newtonsoft.Json.Linq;
68
using PatchKit.Unity.Patcher.Cancellation;
@@ -41,6 +43,10 @@ public void DownloadFile(string torrentPath, string destinationPath,
4143

4244
double lastProgress = 0.0;
4345

46+
const int maxSpeedListCount = 30;
47+
48+
List<double> lastSpeed = new List<double>();
49+
4450
while (!downloaded)
4551
{
4652
var statusResult = torrentClient.ExecuteCommand("status");
@@ -75,6 +81,17 @@ public void DownloadFile(string torrentPath, string destinationPath,
7581
float speed = CalculateDownloadSpeed(progress, lastProgress, totalBytes,
7682
stopwatch.ElapsedMilliseconds);
7783

84+
if (lastSpeed.Count > 0 && lastSpeed.Last() == 0.0)
85+
{
86+
lastSpeed.Clear();
87+
}
88+
89+
lastSpeed.Add(speed);
90+
while (lastSpeed.Count > maxSpeedListCount)
91+
{
92+
lastSpeed.RemoveAt(0);
93+
}
94+
7895
stopwatch.Reset();
7996
stopwatch.Start();
8097
lastProgress = progress;
@@ -83,7 +100,7 @@ public void DownloadFile(string torrentPath, string destinationPath,
83100
{
84101
DownloadedBytes = bytes,
85102
TotalBytes = totalBytes,
86-
KilobytesPerSecond = speed,
103+
KilobytesPerSecond = lastSpeed.Sum() / lastSpeed.Count,
87104
Progress = progress
88105
};
89106

0 commit comments

Comments
 (0)