Skip to content

Commit

Permalink
Use a long instead of an int to prevent possible overflow when measur…
Browse files Browse the repository at this point in the history
…ing the data rate.

svn path=/trunk/bitsharp/; revision=136086
  • Loading branch information
alanmcgovern committed Jun 14, 2009
1 parent c18c7c3 commit b83760a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/MonoTorrent/MonoTorrent.Common/SpeedMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SpeedMonitor
private int speedIndex;
private int[] speeds;
private int lastUpdated;
private int tempRecvCount;
private long tempRecvCount;


public int Rate
Expand Down Expand Up @@ -46,6 +46,12 @@ public void AddDelta(int speed)
this.tempRecvCount += speed;
}

public void AddDelta(long speed)
{
this.total += speed;
this.tempRecvCount += speed;
}


public void Reset()
{
Expand All @@ -62,7 +68,7 @@ public void Reset()
private void TimePeriodPassed()
{
int count = 0;
int total = 0;
long total = 0;

// Find how many milliseconds have passed since the last update and the current tick count
int difference = Environment.TickCount - this.lastUpdated;
Expand Down Expand Up @@ -93,7 +99,7 @@ private void TimePeriodPassed()
if (count == speeds.Length)
count--;

this.speed = (total / (speeds.Length - count));
this.speed = (int)(total / (speeds.Length - count));
this.tempRecvCount = 0;
this.lastUpdated = Environment.TickCount;
}
Expand Down

0 comments on commit b83760a

Please sign in to comment.