Author: alanmc
Date: 2006-11-02 09:16:43 -0500 (Thu, 02 Nov 2006)
New Revision: 67268
Added:
trunk/bitsharp/src/MonoTorrent.Client/RateLimiter.cs
Modified:
trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionMonitor.cs
trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
trunk/bitsharp/src/TestClient/main.cs
Log:
More accurate (+-5% or thereabouts) Download/upload speed monitoring. Accurate
rate limiting implemented (worst case scenario so far it settles 10% lower than
the requested speed, generally within 5%). Rate limiting is still limited to a
per-torrent basis. Refactor of ConnectionMonitor to make it faster. Added
TotalDownloadSpeed and TotalUploadSpeed method to ClientEngine.
Modified: trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-11-02
14:06:43 UTC (rev 67267)
+++ trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-11-02
14:16:43 UTC (rev 67268)
@@ -32,7 +32,6 @@
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
-//using MonoTorrent.Common;
using System.Timers;
using System.Net;
using MonoTorrent.Client.PeerMessages;
@@ -124,9 +123,9 @@
/// The timer used to call the logic methods for the torrent managers
/// </summary>
private System.Timers.Timer timer;
+ private int tickCount;
-
/// <summary>
/// The TorrentManager's loaded into the engine
/// </summary>
@@ -421,20 +420,21 @@
/// <param name="e"></param>
private void LogicTick(object sender, ElapsedEventArgs e)
{
+ tickCount++;
foreach (KeyValuePair<string, TorrentManager> keypair in
this.torrents)
{
switch (keypair.Value.State)
{
case (TorrentState.Downloading):
- keypair.Value.DownloadLogic();
+ keypair.Value.DownloadLogic(tickCount);
break;
case (TorrentState.Seeding):
- keypair.Value.SeedingLogic();
+ keypair.Value.SeedingLogic(tickCount);
break;
case (TorrentState.SuperSeeding):
- keypair.Value.SuperSeedingLogic();
+ keypair.Value.SuperSeedingLogic(tickCount);
break;
default:
@@ -492,13 +492,6 @@
{
lock (id)
{
- if (id.Peer.Connection == null)
- {
- id.Peer.Connection.Dispose();
- id.Peer.Connection = null;
- return;
- }
-
int bytesRecieved = id.Peer.Connection.EndReceive(result,
out id.ErrorCode);
if (bytesRecieved == 0)
{
@@ -514,7 +507,15 @@
}
HandshakeMessage handshake = new HandshakeMessage();
- handshake.Decode(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve);
+ try
+ {
+ handshake.Decode(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve);
+ }
+ catch
+ {
+ CleanupSocket(id);
+ return;
+ }
foreach (TorrentManager manager in this.torrents.Values)
if (ToolBox.ByteMatch(handshake.infoHash,
manager.Torrent.InfoHash))
@@ -550,6 +551,10 @@
{
CleanupSocket(id);
}
+ catch (NullReferenceException)
+ {
+#warning Why is this happening?
+ }
}
@@ -559,6 +564,7 @@
/// <param name="id"></param>
private void CleanupSocket(PeerConnectionID id)
{
+ lock(id)
id.Peer.Connection.Dispose();
}
@@ -588,6 +594,26 @@
this.listener.Dispose();
this.timer.Dispose();
}
+
+
+ public double TotalDownloadSpeed()
+ {
+ double total = 0;
+ foreach (KeyValuePair<string, TorrentManager> keypair in
this.torrents)
+ total += keypair.Value.DownloadSpeed();
+
+ return total;
+ }
+
+
+ public double TotalUploadSpeed()
+ {
+ double total = 0;
+ foreach (KeyValuePair<string, TorrentManager> keypair in
this.torrents)
+ total += keypair.Value.UploadSpeed();
+
+ return total;
+ }
#endregion
}
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2006-11-02 14:06:43 UTC (rev 67267)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2006-11-02 14:16:43 UTC (rev 67268)
@@ -58,7 +58,7 @@
#region Member Variables
- private const int ChunkLength = 2048; // Download in 2kB chunks to
allow for better rate limiting
+ public const int ChunkLength = 2048; // Download in 2kB chunks to
allow for better rate limiting
private EngineSettings settings;
@@ -210,11 +210,12 @@
lock (id)
{
id.Peer.FailedConnectionAttempts++;
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
+
if (id.Peer.Connection != null)
{
+
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
+ id.Peer.Connection.sendBuffer = null;
id.Peer.Connection.ProcessingQueue = false;
id.Peer.Connection.Dispose();
}
@@ -423,6 +424,10 @@
}
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="result"></param>
private void OnPeerBitfieldSent(IAsyncResult result)
{
bool cleanUp = false;
@@ -544,10 +549,10 @@
else
{
- //id.TorrentManager.downloadQueue.Enqueue(id);
+ id.TorrentManager.downloadQueue.Enqueue(id);
+ id.TorrentManager.ResumePeers();
//int bytesRemaining =
(id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved) >
ChunkLength ? ChunkLength : (id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesRecieved);
-
-
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved,
SocketFlags.None, peerMessageRecieved, id, out id.ErrorCode);
+
//id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved,
SocketFlags.None, peerMessageRecieved, id, out id.ErrorCode);
}
}
}
@@ -604,13 +609,9 @@
if (id.Peer.Connection.BytesRecieved !=
id.Peer.Connection.BytesToRecieve)
{
-
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
-
id.Peer.Connection.BytesRecieved,
-
id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved,
- SocketFlags.None,
-
this.peerMessageRecieved, id, out id.ErrorCode);
+ id.TorrentManager.downloadQueue.Enqueue(id);
+ id.TorrentManager.ResumePeers();
return;
- //id.TorrentManager.downloadQueue.Enqueue(id);
}
IPeerMessageInternal message =
PeerwireEncoder.Decode(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, id.TorrentManager);
@@ -702,12 +703,8 @@
if (id.Peer.Connection.BytesSent !=
id.Peer.Connection.BytesToSend)
{
-
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
-
id.Peer.Connection.BytesSent,
-
id.Peer.Connection.BytesToSend - id.Peer.Connection.BytesSent,
- SocketFlags.None,
- this.peerMessageSent,
id, out id.ErrorCode);
- //id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.ResumePeers();
return;
}
if(this.OnPeerMessages != null)
@@ -765,12 +762,9 @@
id.Peer.Connection.BytesSent = 0;
id.Peer.Connection.BytesToSend =
msg.Encode(id.Peer.Connection.sendBuffer, 0);
id.Peer.Connection.CurrentlySendingMessage = msg;
- id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
- id.Peer.Connection.BytesSent,
- id.Peer.Connection.BytesToSend,
- SocketFlags.None,
- this.peerMessageSent, id, out
id.ErrorCode);
- //id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.ResumePeers();
+ return;
}
catch (SocketException ex)
{
@@ -785,8 +779,9 @@
/// </summary>
/// <param name="id">The peer to resume</param>
/// <param name="downloading">True if you want to resume downloading,
false if you want to resume uploading</param>
- internal void ResumePeer(PeerConnectionID id, bool downloading)
+ internal int ResumePeer(PeerConnectionID id, bool downloading)
{
+ int bytesRemaining;
bool cleanUp = false;
try
@@ -796,24 +791,24 @@
if (id.Peer.Connection == null)
{
cleanUp = true;
- return;
+ return 0;
}
if (downloading)
{
- int bytesRemaining =
(id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved) >
ChunkLength ? ChunkLength : (id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesRecieved);
+ bytesRemaining = (id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesRecieved) > ChunkLength ? ChunkLength :
(id.Peer.Connection.BytesToRecieve - id.Peer.Connection.BytesRecieved);
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
id.Peer.Connection.BytesRecieved, bytesRemaining, SocketFlags.None,
peerMessageRecieved, id, out id.ErrorCode);
}
else
{
- int bytesRemaining = (id.Peer.Connection.BytesToSend -
id.Peer.Connection.BytesSent) > ChunkLength ? ChunkLength :
(id.Peer.Connection.BytesToSend - id.Peer.Connection.BytesSent);
+ bytesRemaining = (id.Peer.Connection.BytesToSend -
id.Peer.Connection.BytesSent) > ChunkLength ? ChunkLength :
(id.Peer.Connection.BytesToSend - id.Peer.Connection.BytesSent);
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
id.Peer.Connection.BytesSent, bytesRemaining, SocketFlags.None,
peerMessageSent, id, out id.ErrorCode);
}
}
- return;
+
+ return bytesRemaining;
}
- catch (SocketException ex)
+ catch (SocketException)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
cleanUp = true;
}
finally
@@ -821,6 +816,7 @@
if (cleanUp)
CleanupSocket(id);
}
+ return 0;
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionMonitor.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionMonitor.cs
2006-11-02 14:06:43 UTC (rev 67267)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionMonitor.cs
2006-11-02 14:16:43 UTC (rev 67268)
@@ -38,6 +38,9 @@
/// </summary>
internal class ConnectionMonitor
{
+ private const int ArraySize = 8;
+
+
#region Member Variables
private int tempSentCount;
private int tempRecvCount;
@@ -58,6 +61,7 @@
}
private long bytesDownloaded;
+
/// <summary>
/// Returns the total bytes uploaded to this peer
/// </summary>
@@ -66,83 +70,55 @@
get { return this.bytesUploaded; }
}
private long bytesUploaded;
- #endregion
- #region Constructors
/// <summary>
- /// Creates a new ConnectionMonitor
+ /// The current average download speed in bytes per second
/// </summary>
- public ConnectionMonitor()
+ /// <returns></returns>
+ public double DownloadSpeed
{
- this.lastUpdateTime = Environment.TickCount;
- this.uploadSpeeds = new double[16];
- this.downloadSpeeds = new double[16];
+ get { return this.downloadSpeed; }
}
- #endregion
+ private double downloadSpeed;
- #region Helper Methods
/// <summary>
- /// Calculates the current average download speed
+ /// The current average upload speed in byte/second
/// </summary>
/// <returns></returns>
- public double DownloadSpeed()
+ public double UploadSpeed
{
- int count = 0;
- double total = 0;
-
- lock (this.downloadSpeeds)
- for (int i = 0; i < this.downloadSpeeds.Length; i++)
- {
- if (this.downloadSpeeds[i] == 0)
- count++;
-
- total += this.downloadSpeeds[i];
- }
-
- if (count == this.downloadSpeeds.Length)
- count--;
-
- return total / (this.downloadSpeeds.Length - count);
+ get { return this.uploadSpeed; }
}
+ private double uploadSpeed;
+ #endregion
+ #region Constructors
/// <summary>
- /// Calculates the current average upload speed
+ /// Creates a new ConnectionMonitor
/// </summary>
- /// <returns></returns>
- public double UploadSpeed()
+ public ConnectionMonitor()
{
- int count = 0;
- double total = 0;
-
- lock (this.uploadSpeeds)
- for (int i = 0; i < this.uploadSpeeds.Length; i++)
- {
- if (this.uploadSpeeds[i] == 0)
- count++;
-
- total += this.uploadSpeeds[i];
- }
-
- if (count == this.uploadSpeeds.Length)
- count--;
-
- return total / (this.uploadSpeeds.Length - count);
+ this.lastUpdateTime = Environment.TickCount;
+ this.uploadSpeeds = new double[ArraySize];
+ this.downloadSpeeds = new double[ArraySize];
}
+ #endregion
+ #region Helper Methods
/// <summary>
/// Update the ConnectionManager with bytes uploaded
/// </summary>
/// <param name="bytesUploaded">Bytes uploaded in the last time
period</param>
- public void BytesSent(int bytesUploaded)
+ internal void BytesSent(int bytesUploaded)
{
lock (this.uploadSpeeds)
{
this.bytesUploaded += bytesUploaded;
- this.tempSentCount += bytesUploaded;
+ this.tempSentCount += bytesUploaded;
}
}
@@ -151,7 +127,7 @@
/// Update the ConnectionManager with bytes downloaded
/// </summary>
/// <param name="bytesDownloaded">Bytes downloaded in the last time
period</param>
- public void BytesRecieved(int bytesDownloaded)
+ internal void BytesRecieved(int bytesDownloaded)
{
lock (this.downloadSpeeds)
{
@@ -164,31 +140,59 @@
/// <summary>
/// Called every time you want the stats to update. Ideally between
every 0.5 and 2 seconds
/// </summary>
- public void TimePeriodPassed()
+ internal void TimePeriodPassed()
{
lock (this.downloadSpeeds)
{
lock (this.uploadSpeeds)
{
+ int count = 0;
+ double total = 0;
int currentTime = Environment.TickCount;
int difference = currentTime - this.lastUpdateTime;
-
+
if (difference < 0)
difference = currentTime; // Accounts for the
rollover of Env.TickCount
- if (difference < 500)
- return;
-#warning This isn't the best way to solve the issue of NaN download speeds. I
think it's because it can take >20ms for the locking to allow a thread to run
through
+
this.downloadSpeeds[this.downloadSpeedIndex++] =
tempRecvCount / (difference / 1000.0);
this.uploadSpeeds[this.uploadSpeedIndex++] = tempSentCount
/ (difference / 1000.0);
- if (this.downloadSpeedIndex == this.downloadSpeeds.Length)
+ if (this.downloadSpeedIndex == ArraySize)
this.downloadSpeedIndex = 0;
- if (this.uploadSpeedIndex == this.uploadSpeeds.Length)
+ if (this.uploadSpeedIndex == ArraySize)
this.uploadSpeedIndex = 0;
+
+ for (int i = 0; i < this.downloadSpeeds.Length; i++)
+ {
+ if (this.downloadSpeeds[i] == 0)
+ count++;
+
+ total += this.downloadSpeeds[i];
+ }
+ if (count == ArraySize)
+ count--;
+
+ this.downloadSpeed = (total / (ArraySize - count));
+
+
+ count = 0;
+ total = 0;
+ for (int i = 0; i < this.uploadSpeeds.Length; i++)
+ {
+ if (this.uploadSpeeds[i] == 0)
+ count++;
+
+ total += this.uploadSpeeds[i];
+ }
+ if (count == this.uploadSpeeds.Length)
+ count--;
+
+ this.uploadSpeed = (total / (ArraySize - count));
+
this.tempRecvCount = 0;
this.tempSentCount = 0;
this.lastUpdateTime = currentTime;
@@ -197,4 +201,4 @@
}
#endregion
}
-}
\ No newline at end of file
+}
Modified: trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
2006-11-02 14:06:43 UTC (rev 67267)
+++ trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
2006-11-02 14:16:43 UTC (rev 67268)
@@ -72,6 +72,7 @@
<Compile Include="PeerMessages\UnknownMessage.cs" />
<Compile Include="Peers\Peer.cs" />
<Compile Include="ProtocolException.cs" />
+ <Compile Include="RateLimiter.cs" />
<Compile Include="Tests\AllowedFastAlgorithmTest.cs" />
<Compile Include="Tracker.cs" />
<Compile Include="ConnectionListener.cs" />
Added: trunk/bitsharp/src/MonoTorrent.Client/RateLimiter.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/RateLimiter.cs 2006-11-02
14:06:43 UTC (rev 67267)
+++ trunk/bitsharp/src/MonoTorrent.Client/RateLimiter.cs 2006-11-02
14:16:43 UTC (rev 67268)
@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading;
+
+namespace MonoTorrent.Client
+{
+ internal struct RateLimiter
+ {
+ #region Member Variables
+ public int SavedErrorDownload;
+ public int SavedErrorUpload;
+ public int DownloadChunks;
+ public int UploadChunks;
+ #endregion
+
+
+ #region Methods
+ /// <summary>
+ /// Method which calculates how many chunks of data we are able to
send to allow for rate limiting (if enabled)
+ /// </summary>
+ public void UpdateDownloadChunks(int maxDownloadSpeed, int
maxUploadSpeed, int actualDownloadSpeed, int actualUploadSpeed)
+ {
+ int errorRateDown = maxDownloadSpeed - actualDownloadSpeed;
+ int changeAmountDown = (int)(0.4 * errorRateDown + 0.6 *
this.SavedErrorDownload);
+ this.SavedErrorDownload = errorRateDown;
+
+ int errorRateUp = maxUploadSpeed - actualUploadSpeed;
+ int changeAmountUp = (int)(0.4 * errorRateUp + 0.6 *
this.SavedErrorUpload);
+ this.SavedErrorUpload = errorRateUp;
+
+
+ int increaseAmount = (int)((maxDownloadSpeed +
this.SavedErrorDownload) / ConnectionManager.ChunkLength);
+ Interlocked.Add(ref this.DownloadChunks, increaseAmount);
+ if (this.DownloadChunks > (increaseAmount * 3))
+ Interlocked.Exchange(ref this.DownloadChunks, (increaseAmount
* 3));
+
+ increaseAmount = (int)((maxUploadSpeed + this.SavedErrorUpload) /
ConnectionManager.ChunkLength);
+ Interlocked.Add(ref this.UploadChunks, increaseAmount);
+ if (this.UploadChunks > increaseAmount * 3)
+ Interlocked.Exchange(ref this.UploadChunks, (increaseAmount *
3));
+ }
+ #endregion
+ }
+}
Property changes on: trunk/bitsharp/src/MonoTorrent.Client/RateLimiter.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/bitsharp/src/TestClient/main.cs
===================================================================
--- trunk/bitsharp/src/TestClient/main.cs 2006-11-02 14:06:43 UTC (rev
67267)
+++ trunk/bitsharp/src/TestClient/main.cs 2006-11-02 14:16:43 UTC (rev
67268)
@@ -98,7 +98,7 @@
bool running = true;
while (running)
{
- if ((i++) % 50 == 0)
+ if ((i++) % 5 == 0)
{
running = false;
Console.Clear();
@@ -108,21 +108,22 @@
running = true;
Console.WriteLine("Torrent: " +
manager.Torrent.Name);
- //Debug.WriteLine("Uploading to: " +
manager.UploadingTo.ToString());
- //Debug.WriteLine("Half opens: " +
ClientEngine.connectionManager.HalfOpenConnections);
- //Debug.WriteLine("Max open: " +
ClientEngine.connectionManager.MaxOpenConnections);
+ Console.WriteLine("Uploading to: " +
manager.UploadingTo.ToString());
+ Console.WriteLine("Half opens: " +
ClientEngine.ConnectionManager.HalfOpenConnections);
+ Console.WriteLine("Max open: " +
ClientEngine.ConnectionManager.MaxOpenConnections);
Console.WriteLine("Progress: " +
string.Format(manager.Progress().ToString(), ("{0:0.00}")));
Console.WriteLine("Download Speed: " +
string.Format("{0:0.00}", manager.DownloadSpeed() / 1024));
Console.WriteLine("Upload Speed: " +
string.Format("{0:0.00}", manager.UploadSpeed() / 1024));
- //Debug.WriteLine("Torrent State: " +
manager.State.ToString());
- //Debug.WriteLine("Number of seeds: " +
manager.Seeds());
- //Debug.WriteLine("Number of leechs: " +
manager.Leechs());
- //Debug.WriteLine("Total available: " +
manager.AvailablePeers);
+ Console.WriteLine("Torrent State: " +
manager.State.ToString());
+ Console.WriteLine("Number of seeds: " +
manager.Seeds());
+ Console.WriteLine("Number of leechs: " +
manager.Leechs());
+ Console.WriteLine("Total available: " +
manager.AvailablePeers);
Console.WriteLine("Downloaded: " +
manager.DataBytesDownloaded / 1024.0);
Console.WriteLine("Uploaded: " +
manager.DataBytesUploaded / 1024.0);
- //Debug.WriteLine("Protocol Download:" +
manager.ProtocolBytesDownloaded / 1024.0);
- //Debug.WriteLine("Protocol Upload: " +
manager.ProtocolBytesUploaded / 1024.0);
- //Debug.WriteLine("Tracker Status: " +
manager.TrackerManager.CurrentTracker.State.ToString());
+ Console.WriteLine("Tracker Status: " +
manager.TrackerManager.CurrentTracker.State.ToString());
+ Console.WriteLine("Protocol Download:" +
manager.ProtocolBytesDownloaded / 1024.0);
+ Console.WriteLine("Protocol Upload: " +
manager.ProtocolBytesUploaded / 1024.0);
+ Console.WriteLine("Tracker Status: " +
manager.TrackerManager.CurrentTracker.State.ToString());
//Console.WriteLine("\n");
}
}
@@ -147,8 +148,7 @@
static void UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
- Console.WriteLine("Unhandled");
- Debug.WriteLine(sender.ToString());
+ shutdown();
}
static void CurrentDomain_ProcessExit(object sender, EventArgs e)
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches