Author: alanmc
Date: 2007-02-11 12:12:46 -0500 (Sun, 11 Feb 2007)
New Revision: 72625
Added:
trunk/bitsharp/src/MonoTorrent.Client/Logger.cs
Removed:
trunk/bitsharp/src/MonoTorrent.Client/Peers/Peers.cs
Modified:
trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
trunk/bitsharp/src/MonoTorrent.Client/PeerList.cs
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
Log:
Added logging code for debugging purposes. Moved more peer logic into the
PeerList class. Bugfix to make sure all references to peers are removed from
the internal lists when a peer is disposed.
Modified: trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2007-02-11
17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2007-02-11
17:12:46 UTC (rev 72625)
@@ -507,7 +507,7 @@
ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 68;
-
+ Logger.Log(id, "CE Peer incoming connection accepted");
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None, peerHandshakeReceived, id,
out id.ErrorCode);
}
catch (SocketException)
@@ -542,6 +542,7 @@
int bytesReceived = id.Peer.Connection.EndReceive(result,
out id.ErrorCode);
if (bytesReceived == 0)
{
+ Logger.Log(id, "CE Recieved 0 for handshake");
CleanupSocket(id);
return;
}
@@ -552,6 +553,7 @@
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
id.Peer.Connection.BytesReceived, id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesReceived, SocketFlags.None, peerHandshakeReceived, id,
out id.ErrorCode);
return;
}
+ Logger.Log(id, "CE Recieved handshake");
HandshakeMessage handshake = new HandshakeMessage();
try
@@ -560,6 +562,7 @@
}
catch
{
+ Logger.Log(id, "CE Invalid handshake");
CleanupSocket(id);
return;
}
@@ -570,6 +573,7 @@
if (man == null) // We're not hosting that torrent
{
+ Logger.Log(id, "CE Not tracking torrent");
CleanupSocket(id);
return;
}
@@ -577,7 +581,7 @@
id.Peer.PeerId = handshake.PeerId;
id.TorrentManager = man;
handshake.Handle(id);
-
+ Logger.Log(id, "CE Handshake successful");
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
id.Peer.Connection.ClientApp = new
PeerID(handshake.PeerId);
@@ -590,6 +594,7 @@
id.Peer.Connection.BytesToSend =
handshake.Encode(id.Peer.Connection.sendBuffer, 0);
id.Peer.Connection.BytesToSend +=
bf.Encode(id.Peer.Connection.sendBuffer, id.Peer.Connection.BytesToSend);
+ Logger.Log(id, "CE Sending to torrent manager");
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer, 0,
id.Peer.Connection.BytesToSend, SocketFlags.None, new
AsyncCallback(ClientEngine.ConnectionManager.IncomingConnectionAccepted), id,
out id.ErrorCode);
id.Peer.Connection.ProcessingQueue = false;
return;
@@ -598,6 +603,7 @@
catch (SocketException)
{
+ Logger.Log(id, "CE Exception with handshake");
CleanupSocket(id);
}
catch (NullReferenceException)
@@ -615,12 +621,17 @@
{
lock (id)
{
+ Logger.Log(id, "***********CE Cleaning up*************");
if (id.Peer.Connection != null)
{
BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
id.Peer.Connection.Dispose();
}
+ else
+ {
+ Logger.Log(id, "!!!!!!!!!!CE Already null!!!!!!!!");
+ }
}
}
Added: trunk/bitsharp/src/MonoTorrent.Client/Logger.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Logger.cs 2007-02-11 17:09:34 UTC
(rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/Logger.cs 2007-02-11 17:12:46 UTC
(rev 72625)
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.IO;
+
+namespace MonoTorrent.Client
+{
+ public static class Logger
+ {
+ private static Dictionary<PeerConnectionID, StringBuilder> log;
+
+ static Logger()
+ {
+ log = new Dictionary<PeerConnectionID, StringBuilder>();
+ }
+
+ public static void Log(PeerConnectionID id, string message)
+ {
+ return;
+ if (!log.ContainsKey(id))
+ log.Add(id, new StringBuilder(512));
+
+ log[id].AppendLine(message);
+ }
+
+ public static void FlushToDisk()
+ {
+ Random r = new Random();
+ int number = r.Next(0, 10000);
+ if (!Directory.Exists(@"C:\logs\" + number))
+ Directory.CreateDirectory(@"C:\logs\" + number);
+
+ foreach (KeyValuePair<PeerConnectionID, StringBuilder> keypair in
log)
+ File.WriteAllText(@"C:\logs\" + number + "\\" +
keypair.Key.Peer.Location.GetHashCode(), keypair.Value.ToString());
+ }
+ }
+}
Property changes on: trunk/bitsharp/src/MonoTorrent.Client/Logger.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -152,12 +152,12 @@
for (i = 0; i < length; i++)
{
id = manager.Peers.AvailablePeers[0];
- manager.Peers.RemovePeer(id, PeerList.PeerType.Available);
+ manager.Peers.RemovePeer(id, PeerType.Available);
// If the peer is a known seeder and i'm a seeder, don't
bother trying to connect
if (manager.State == TorrentState.Seeding && id.Peer.IsSeeder)
{
- manager.Peers.AddPeer(id, PeerList.PeerType.Available);
+ manager.Peers.AddPeer(id, PeerType.Available);
id = null;
continue;
}
@@ -172,8 +172,8 @@
lock (id)
{
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting****");
- manager.Peers.AddPeer(id, PeerList.PeerType.Connecting);
+ Logger.Log(id, "Connecting");
+ manager.Peers.AddPeer(id, PeerType.Connecting);
System.Threading.Interlocked.Increment(ref
this.halfOpenConnections);
id.Peer.Connection = new TCPConnection(id.Peer.Location,
id.TorrentManager.Torrent.Pieces.Length, new NoEncryption());
id.Peer.Connection.ProcessingQueue = true;
@@ -192,7 +192,6 @@
{
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
-
try
{
@@ -201,18 +200,23 @@
lock (id)
{
if (id.Peer.Connection == null)
+ {
+ Logger.Log(id, "Connection null");
return;
+ }
id.Peer.Connection.EndConnect(result);
+ Logger.Log(id, "Connected");
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connected****");
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Connecting);
- id.TorrentManager.Peers.AddPeer(id,
PeerList.PeerType.Connected);
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.Connecting);
+ id.TorrentManager.Peers.AddPeer(id,
PeerType.Connected);
if (this.PeerConnected != null)
this.PeerConnected(null, new
PeerConnectionEventArgs(id, Direction.Outgoing));
if (this.openConnections > this.MaxOpenConnections)
{
+ Logger.Log(id, "Too many connections");
cleanUp = true;
return;
}
@@ -231,53 +235,50 @@
catch (SocketException ex)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
lock (id.TorrentManager.listLock)
{
lock (id)
{
+ Logger.Log(id, "failed to connect " + ex.Message);
id.Peer.FailedConnectionAttempts++;
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting Failed****");
if (id.Peer.Connection != null)
{
-
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
id.Peer.Connection.ProcessingQueue = false;
id.Peer.Connection.Dispose();
}
id.Peer.Connection = null;
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Connecting);
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.Connecting);
if (id.Peer.FailedConnectionAttempts < 2) // We
couldn't connect this time, so re-add to available
- id.TorrentManager.Peers.AddPeer(id,
PeerList.PeerType.Available);
+ id.TorrentManager.Peers.AddPeer(id,
PeerType.Available);
}
}
}
- catch (ArgumentException)
- {
- lock (id.TorrentManager.listLock)
- {
- lock (id)
- {
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting Failed****");
+ //catch (ArgumentException)
+ //{
+ // lock (id.TorrentManager.listLock)
+ // {
+ // lock (id)
+ // {
+ // Logger.Log(id, "Failed to connect, argument
exception");
+ // ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
+ // id.Peer.FailedConnectionAttempts++;
+ // id.Peer.Connection.ProcessingQueue = false;
+ // if (id.Peer.Connection != null)
+ // id.Peer.Connection.Dispose();
+ // id.Peer.Connection = null;
- ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
- id.Peer.FailedConnectionAttempts++;
- id.Peer.Connection.ProcessingQueue = false;
- if (id.Peer.Connection != null)
- id.Peer.Connection.Dispose();
- id.Peer.Connection = null;
+ // id.TorrentManager.Peers.RemovePeer(id,
PeerType.Connecting);
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Connecting);
+ // if (id.Peer.FailedConnectionAttempts < 4) // We
couldn't connect this time, so re-add to available
+ // id.TorrentManager.Peers.AddPeer(id,
PeerType.Available);
- if (id.Peer.FailedConnectionAttempts < 4) // We
couldn't connect this time, so re-add to available
- id.TorrentManager.Peers.AddPeer(id,
PeerList.PeerType.Available);
-
- }
- }
- }
+ // }
+ // }
+ //}
finally
{
System.Threading.Interlocked.Decrement(ref
this.halfOpenConnections);
@@ -308,6 +309,7 @@
int bytesSent = id.Peer.Connection.EndSend(result, out
id.ErrorCode);
if (id.ErrorCode != SocketError.Success || bytesSent
== 0)
{
+ Logger.Log(id, "Couldn't send handshake");
cleanUp = true;
return;
}
@@ -321,8 +323,8 @@
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
id.Peer.Connection.BytesSent, id.Peer.Connection.BytesToSend -
id.Peer.Connection.BytesSent, SocketFlags.None, handshakeSentCallback, id, out
id.ErrorCode);
return;
}
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() +
"Handshake");
-
+ Logger.Log(id, "Sent Handshake");
+ Logger.Log(id, "Recieving handshake");
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesReceived = 0;
@@ -333,6 +335,7 @@
catch (SocketException ex)
{
+ Logger.Log(id, "Exception sending handshake");
Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
cleanUp = true;
}
@@ -365,11 +368,15 @@
lock (id)
{
if (id.Peer.Connection == null)
+ {
+ Logger.Log(id, "Connection null");
return;
+ }
int bytesReceived =
id.Peer.Connection.EndReceive(result, out id.ErrorCode);
if (id.ErrorCode != SocketError.Success ||
bytesReceived == 0)
{
+ Logger.Log(id, "Recieved 0 byte handshake");
cleanUp = true;
return;
}
@@ -383,7 +390,7 @@
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
id.Peer.Connection.BytesReceived, id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesReceived, SocketFlags.None,
this.handshakeReceievedCallback, id, out id.ErrorCode);
return;
}
-
+ Logger.Log(id, "Handshake recieved");
msg = new HandshakeMessage();
msg.Decode(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve);
HandshakeMessage handshake = msg as HandshakeMessage;
@@ -395,6 +402,7 @@
// If the infohash doesn't match, dump the connection
if (!ToolBox.ByteMatch(handshake.infoHash,
id.TorrentManager.Torrent.InfoHash))
{
+ Logger.Log(id, "Invalid infohash");
cleanUp = true;
return;
}
@@ -402,6 +410,7 @@
// If the peer id's don't match, dump the connection
if (id.Peer.PeerId != handshake.PeerId)
{
+ Logger.Log(id, "Invalid peerid");
cleanUp = true;
return;
}
@@ -426,7 +435,7 @@
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.LargeMessageBuffer);
-
+ Logger.Log(id, "Sending bitfield: " +
msg.GetType().Name);
id.Peer.Connection.BytesSent = 0;
id.Peer.Connection.BytesToSend =
msg.Encode(id.Peer.Connection.sendBuffer, 0);
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer, 0,
id.Peer.Connection.BytesToSend, SocketFlags.None, this.bitfieldSentCallback,
id, out id.ErrorCode);
@@ -435,6 +444,7 @@
catch (SocketException ex)
{
+ Logger.Log(id, "Exception recieving handshake");
Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
cleanUp = true;
}
@@ -466,11 +476,15 @@
lock (id)
{
if (id.Peer.Connection == null)
+ {
+ Logger.Log(id, "Connection null for sending
bitfield");
return;
+ }
int bytesSent = id.Peer.Connection.EndSend(result, out
id.ErrorCode);
if (id.ErrorCode != SocketError.Success || (bytesSent
== 0))
{
+ Logger.Log(id, "Sent 0 bytes for bitfield");
cleanUp = true;
return;
}
@@ -494,6 +508,7 @@
for (int i = 0; i <
id.Peer.Connection.AmAllowedFastPieces.Count; i++)
id.Peer.Connection.EnQueue(new
AllowedFastMessage(id.Peer.Connection.AmAllowedFastPieces[i]));
+ Logger.Log(id, "Queuing");
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 4;
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None,
this.messageLengthReceivedCallback, id, out id.ErrorCode);
@@ -501,7 +516,7 @@
}
catch (SocketException ex)
{
- Debug.WriteLine(ex.ToString());
+ Logger.Log(id, "Exception sending bitfield");
cleanUp = true;
}
//catch (ArgumentException)
@@ -532,16 +547,14 @@
lock (id)
{
if (id.Peer.Connection == null)
- return;
-
- int bytesReceived =
id.Peer.Connection.EndReceive(result, out id.ErrorCode);
- if (id.ErrorCode != SocketError.Success)
{
- cleanUp = true;
+ Logger.Log(id, "Connection null for message
length");
return;
}
- if (bytesReceived == 0)
+ int bytesReceived =
id.Peer.Connection.EndReceive(result, out id.ErrorCode);
+ if (id.ErrorCode != SocketError.Success ||
bytesReceived == 0)
{
+ Logger.Log(id, "Recieved 0 for message length");
cleanUp = true;
return;
}
@@ -555,7 +568,7 @@
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
id.Peer.Connection.BytesReceived, id.Peer.Connection.BytesToRecieve -
id.Peer.Connection.BytesReceived, SocketFlags.None,
this.messageLengthReceivedCallback, id, out id.ErrorCode);
return;
}
-
+ Logger.Log(id, "Recieved message length");
int networkOrderLength =
BitConverter.ToInt32(id.Peer.Connection.recieveBuffer, 0);
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve =
System.Net.IPAddress.NetworkToHostOrder(networkOrderLength);
@@ -575,6 +588,7 @@
}
else
{
+ Logger.Log(id, "Recieving message");
if (id.TorrentManager.Settings.MaxDownloadSpeed ==
0) // No rate limiting needed
{
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None,
this.messageReceivedCallback, id, out id.ErrorCode);
@@ -582,7 +596,7 @@
}
else // Apply rate limiting
{
- id.TorrentManager.downloadQueue.Enqueue(id);
+ id.TorrentManager.Peers.Enqueue(id,
PeerType.DownloadQueue);
id.TorrentManager.ResumePeers();
}
}
@@ -590,6 +604,7 @@
}
catch (SocketException ex)
{
+ Logger.Log(id, "Exception recieving message length");
Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
cleanUp = true;
}
@@ -621,11 +636,15 @@
lock (id)
{
if (id.Peer.Connection == null)
+ {
+ Logger.Log(id, "Connection null recieving
message");
return;
+ }
int bytesReceived =
id.Peer.Connection.EndReceive(result, out id.ErrorCode);
if (id.ErrorCode != SocketError.Success ||
(bytesReceived == 0))
{
+ Logger.Log(id, "Recieved 0 for message");
cleanUp = true;
return;
}
@@ -642,7 +661,7 @@
}
else
{
- id.TorrentManager.downloadQueue.Enqueue(id);
+ id.TorrentManager.Peers.Enqueue(id,
PeerType.DownloadQueue);
id.TorrentManager.ResumePeers();
}
return;
@@ -655,6 +674,7 @@
}
catch (ProtocolException ex)
{
+ Logger.Log(id, "Invalid message recieved");
Trace.WriteLine(ex.Message);
cleanUp = true;
return;
@@ -668,10 +688,11 @@
}
catch (TorrentException ex)
{
+ Logger.Log(id, "Couldn't handle message");
cleanUp = true;
return;
}
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "\t" +
message.ToString());
+ Logger.Log(id, "Recieved message: "
+message.GetType().Name);
if (!(message is PieceMessage))
{ // The '-4' is because of the messagelength int
which has already been counted in a different method
id.TorrentManager.DataBytesDownloaded -=
(message.ByteLength - 4);
@@ -680,7 +701,11 @@
id.TorrentManager.Monitor.BytesReceived(message.ByteLength);
if (id.Peer.HashFails == 3)
+ {
+ Logger.Log(id, "3 hashfails");
cleanUp = true;
+ return;
+ }
id.Peer.Connection.LastMessageReceived = DateTime.Now;
@@ -695,24 +720,9 @@
}
catch (SocketException ex)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
+ Logger.Log(id, "Exception recieving message");
cleanUp = true;
}
-// catch (ArgumentException ex)
-// {
-//#warning should be unneccessary
-// cleanUp = true;
-// }
-// catch (NullReferenceException)
-// {
-//#warning should be unneccessary
-// cleanUp = true;
-// }
-// catch (Exception)
-// {
-//#warning remove this.
-// cleanUp = true;
-// }
finally
{
if (cleanUp)
@@ -737,11 +747,15 @@
lock (id)
{
if (id.Peer.Connection == null)
+ {
+ Logger.Log(id, "Connection null sending message");
return;
+ }
int bytesSent = id.Peer.Connection.EndSend(result, out
id.ErrorCode);
if (id.ErrorCode != SocketError.Success || (bytesSent
== 0))
{
+ Logger.Log(id, "Sent 0 for message");
cleanUp = true;
return;
}
@@ -762,23 +776,24 @@
}
else
{
- id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.Peers.Enqueue(id,
PeerType.UploadQueue);
id.TorrentManager.ResumePeers();
}
return;
}
+
if (this.PeerMessageTransferred != null)
this.PeerMessageTransferred(id, new
PeerMessageEventArgs((IPeerMessage)id.Peer.Connection.CurrentlySendingMessage,
Direction.Outgoing));
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
- //Console.WriteLine("SENT " +
id.Peer.Connection.CurrentlySendingMessage.ToString());
+ Logger.Log(id, "Sent message: "
+id.Peer.Connection.CurrentlySendingMessage.GetType().Name);
id.Peer.Connection.LastMessageSent = DateTime.Now;
this.ProcessQueue(id);
}
}
catch (SocketException ex)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
+ Logger.Log(id, "Exception sending message");
cleanUp = true;
}
//catch (ArgumentException)
@@ -825,20 +840,21 @@
id.Peer.Connection.BytesToSend =
msg.Encode(id.Peer.Connection.sendBuffer, 0);
id.Peer.Connection.CurrentlySendingMessage = msg;
+ Logger.Log(id, "Sending message from queue: " +
msg.ToString());
if (id.TorrentManager.Settings.MaxUploadSpeed == 0)
{
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer, 0,
id.Peer.Connection.BytesToSend, SocketFlags.None, this.messageSentCallback, id,
out id.ErrorCode);
}
else
{
- id.TorrentManager.uploadQueue.Enqueue(id);
+ id.TorrentManager.Peers.Enqueue(id, PeerType.UploadQueue);
id.TorrentManager.ResumePeers();
}
return;
}
catch (SocketException ex)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
+ Logger.Log(id, "Exception dequeuing message");
CleanupSocket(id);
}
}
@@ -910,6 +926,7 @@
bytesSent = id.Peer.Connection.EndSend(result, out
id.ErrorCode);
if (bytesSent == 0)
{
+ Logger.Log(id, "Sent 0 for incoming connection
accepted");
cleanUp = true;
return;
}
@@ -923,26 +940,29 @@
if (id.Peer.PeerId == ClientEngine.PeerId) // The
tracker gave us our own IP/Port combination
{
+ Logger.Log(id, "Recieved myself");
cleanUp = true;
return;
}
if
(id.TorrentManager.Peers.ConnectedPeers.Contains(id) ||
id.TorrentManager.Peers.ConnectingToPeers.Contains(id))
{
+ Logger.Log(id, "Already connected to peer");
id.Peer.Connection.Dispose();
return;
}
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Available);
- id.TorrentManager.Peers.AddPeer(id,
PeerList.PeerType.Connected);
+ Logger.Log(id, "Peer accepted ok");
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.Available);
+ id.TorrentManager.Peers.AddPeer(id,
PeerType.Connected);
- //id.Peer.MessageHistory.AppendLine("Their ID: " +
id.Peer.PeerId);
ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
if (this.PeerConnected != null)
this.PeerConnected(null, new
PeerConnectionEventArgs(id, Direction.Incoming));
+ Logger.Log(id, "Recieving message length");
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 4;
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer,
id.Peer.Connection.BytesReceived, id.Peer.Connection.BytesToRecieve,
SocketFlags.None, this.messageLengthReceivedCallback, id, out id.ErrorCode);
@@ -951,7 +971,7 @@
}
catch (SocketException ex)
{
- Debug.WriteLine(ex.NativeErrorCode + ": " + ex.Message);
+ Logger.Log(id, "Exception when accepting peer");
cleanUp = true;
}
finally
@@ -972,6 +992,7 @@
{
lock (id)
{
+ Logger.Log(id, "*******Cleaning up*******");
System.Threading.Interlocked.Decrement(ref
this.openConnections);
id.TorrentManager.PieceManager.RemoveRequests(id);
id.Peer.CleanedUpCount++;
@@ -991,6 +1012,11 @@
id.Peer.Connection.Dispose();
id.Peer.Connection = null;
}
+ else
+ {
+ Logger.Log(id, "!!!!Connection already null!!!!");
+ }
+
int found = 0;
if(id.TorrentManager.Peers.ConnectedPeers.Contains(id))
found++;
@@ -1004,15 +1030,18 @@
Console.WriteLine("Found: " + found.ToString());
}
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.UploadQueue);
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.DownloadQueue);
+
if (id.TorrentManager.Peers.ConnectedPeers.Contains(id))
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Connected);
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.Connected);
if (id.TorrentManager.Peers.ConnectingToPeers.Contains(id))
- id.TorrentManager.Peers.RemovePeer(id,
PeerList.PeerType.Connecting);
+ id.TorrentManager.Peers.RemovePeer(id,
PeerType.Connecting);
if (id.Peer.PeerId != ClientEngine.PeerId)
if
(!id.TorrentManager.Peers.AvailablePeers.Contains(id) && id.Peer.CleanedUpCount
< 5)
- id.TorrentManager.Peers.AddPeer(id,
PeerList.PeerType.Available);
+ id.TorrentManager.Peers.AddPeer(id,
PeerType.Available);
}
}
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -100,7 +100,7 @@
}
- internal IPeerMessageInternal PickPiece(PeerConnectionID id, Peers
otherPeers)
+ internal IPeerMessageInternal PickPiece(PeerConnectionID id,
List<PeerConnectionID> otherPeers)
{
if ((this.MyBitField.Length - this.MyBitField.TrueCount < 15) &&
this.piecePicker is StandardPicker)
this.piecePicker = new EndGamePicker(this.MyBitField,
id.TorrentManager.Torrent);
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -157,7 +157,7 @@
internal object listLock = new object();
- public PeerList Peers
+ internal PeerList Peers
{
get { return this.peers; }
}
@@ -276,11 +276,6 @@
this.trackerManager = new TrackerManager(this);
this.peers = new PeerList();
- this.peers.ConnectedPeers = new Peers(16);
- this.peers.AvailablePeers = new Peers(16);
- this.uploadQueue = new Queue<PeerConnectionID>(16);
- this.downloadQueue = new Queue<PeerConnectionID>(16);
- this.peers.ConnectingToPeers = new
Peers(ClientEngine.ConnectionManager.MaxHalfOpenConnections);
this.savePath = savePath;
@@ -357,9 +352,7 @@
if(this.fileManager.StreamsOpen)
this.FileManager.CloseFileStreams();
this.SaveFastResume();
- this.peers.ConnectedPeers = new Peers();
- this.peers.AvailablePeers = new Peers();
- this.peers.ConnectingToPeers = new Peers();
+ this.peers.ClearAll();
return handle;
}
@@ -399,7 +392,7 @@
PeerConnectionID id;
lock (this.listLock)
- if (this.downloadQueue.Count > 0 || this.uploadQueue.Count > 0)
+ if (this.peers.DownloadQueue.Count > 0 ||
this.peers.UploadQueue.Count > 0)
this.ResumePeers();
DateTime nowTime = DateTime.Now;
@@ -677,10 +670,7 @@
#region Rate limiting
- internal Queue<PeerConnectionID> downloadQueue;
- internal Queue<PeerConnectionID> uploadQueue;
-
/// <summary>
/// Restarts peers which have been suspended from
downloading/uploading due to rate limiting
/// </summary>
@@ -689,12 +679,12 @@
{
lock (this.listLock)
{
- while (this.downloadQueue.Count > 0 &&
((this.rateLimiter.DownloadChunks > 0) || this.settings.MaxDownloadSpeed == 0))
- if
(ClientEngine.ConnectionManager.ResumePeer(this.downloadQueue.Dequeue(), true)
> ConnectionManager.ChunkLength / 2.0)
+ while (this.peers.DownloadQueue.Count > 0 &&
((this.rateLimiter.DownloadChunks > 0) || this.settings.MaxDownloadSpeed == 0))
+ if
(ClientEngine.ConnectionManager.ResumePeer(this.peers.Dequeue(PeerType.DownloadQueue),
true) > ConnectionManager.ChunkLength / 2.0)
Interlocked.Decrement(ref
this.rateLimiter.DownloadChunks);
- while (this.uploadQueue.Count > 0 &&
((this.rateLimiter.UploadChunks > 0) || this.settings.MaxUploadSpeed == 0))
- if
(ClientEngine.ConnectionManager.ResumePeer(this.uploadQueue.Dequeue(), false) >
ConnectionManager.ChunkLength / 2.0)
+ while (this.peers.UploadQueue.Count > 0 &&
((this.rateLimiter.UploadChunks > 0) || this.settings.MaxUploadSpeed == 0))
+ if
(ClientEngine.ConnectionManager.ResumePeer(this.peers.Dequeue(PeerType.UploadQueue),
false) > ConnectionManager.ChunkLength / 2.0)
Interlocked.Decrement(ref
this.rateLimiter.UploadChunks);
}
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerList.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerList.cs 2007-02-11 17:09:34 UTC
(rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerList.cs 2007-02-11 17:12:46 UTC
(rev 72625)
@@ -4,49 +4,75 @@
namespace MonoTorrent.Client
{
- public class PeerList
+ internal enum PeerType
{
+ Connecting,
+ Connected,
+ Available,
+ UploadQueue,
+ DownloadQueue
+ }
+
+ internal class PeerList
+ {
+ #region Private Member Variables
+
+ private List<PeerConnectionID> availablePeers;
+ private List<PeerConnectionID> connectedPeers;
+ private List<PeerConnectionID> connectingTo;
+ private List<PeerConnectionID> downloadQueue;
+ private List<PeerConnectionID> uploadQueue;
+
+ #endregion Private Member Variables
+
+ #region Internal Properties
+
/// <summary>
/// The list of peers that are available to be connected to
/// </summary>
- internal Peers AvailablePeers
+ internal List<PeerConnectionID> AvailablePeers
{
get { return this.availablePeers; }
- set { this.availablePeers = value; }
}
- private Peers availablePeers;
-
/// <summary>
/// The list of peers that we are currently connected to
/// </summary>
- internal Peers ConnectedPeers
+ internal List<PeerConnectionID> ConnectedPeers
{
get { return this.connectedPeers; }
- set { this.connectedPeers = value; }
}
- private Peers connectedPeers;
-
/// <summary>
/// The list of peers that we are currently trying to connect to
/// </summary>
- internal Peers ConnectingToPeers
+ internal List<PeerConnectionID> ConnectingToPeers
{
get { return this.connectingTo; }
- set { this.connectingTo = value; }
}
- private Peers connectingTo;
- internal enum PeerType {Connecting, Connected, Available}
+ /// <summary>
+ /// The list of peers which have data queued up to send
+ /// </summary>
+ internal List<PeerConnectionID> UploadQueue
+ {
+ get { return this.uploadQueue; }
+ }
+ /// <summary>
+ /// The list of peers which have data queued up to download
+ /// </summary>
+ internal List<PeerConnectionID> DownloadQueue
+ {
+ get { return this.downloadQueue; }
+ }
+ #endregion
+
+ #region Internal Methods
+
internal void AddPeer(PeerConnectionID id, PeerType type)
{
- if (this.availablePeers.Contains(id) ||
this.connectingTo.Contains(id) || this.connectedPeers.Contains(id))
- {
- string s = "";
- }
switch (type)
{
case (PeerType.Connected):
@@ -60,9 +86,93 @@
case (PeerType.Available):
this.availablePeers.Add(id);
break;
+
+ case (PeerType.DownloadQueue):
+ this.downloadQueue.Add(id);
+ break;
+
+ case (PeerType.UploadQueue):
+ this.uploadQueue.Remove(id);
+ break;
+
+ default:
+ throw new NotSupportedException();
}
}
+ internal void ClearAll()
+ {
+ this.availablePeers.Clear();
+ this.connectedPeers.Clear();
+ this.connectingTo.Clear();
+ this.downloadQueue.Clear();
+ this.uploadQueue.Clear();
+ }
+
+ internal PeerConnectionID Dequeue(PeerType type)
+ {
+ PeerConnectionID id;
+ switch (type)
+ {
+ case (PeerType.Connected):
+ id = this.connectedPeers[0];
+ this.connectedPeers.RemoveAt(0);
+ return id;
+
+ case (PeerType.Connecting):
+ id = this.connectingTo[0];
+ this.connectingTo.RemoveAt(0);
+ return id;
+
+ case (PeerType.Available):
+ id = this.availablePeers[0];
+ this.availablePeers.RemoveAt(0);
+ return id;
+
+ case (PeerType.DownloadQueue):
+ id = this.downloadQueue[0];
+ this.downloadQueue.RemoveAt(0);
+ return id;
+
+ case (PeerType.UploadQueue):
+ id = this.uploadQueue[0];
+ this.uploadQueue.RemoveAt(0);
+ return id;
+
+ default:
+ throw new NotSupportedException();
+ }
+ }
+
+ internal void Enqueue(PeerConnectionID id, PeerType type)
+ {
+ switch (type)
+ {
+ case (PeerType.Connected):
+ this.connectedPeers.Add(id);
+ return;
+
+ case (PeerType.Connecting):
+ this.connectingTo.Add(id);
+ return;
+
+ case (PeerType.Available):
+ this.availablePeers.Add(id);
+ return;
+
+ case (PeerType.DownloadQueue):
+ this.downloadQueue.Add(id);
+ return;
+
+ case (PeerType.UploadQueue):
+ this.uploadQueue.Add(id);
+ return;
+
+ default:
+ throw new NotSupportedException();
+ }
+ }
+
internal void RemovePeer(PeerConnectionID id, PeerType type)
{
switch (type)
@@ -78,17 +188,29 @@
case (PeerType.Available):
this.availablePeers.Remove(id);
break;
- }
- if (this.availablePeers.Contains(id) ||
this.connectingTo.Contains(id) || this.connectedPeers.Contains(id))
- {
- string s = "";
+ case(PeerType.DownloadQueue):
+ this.downloadQueue.Remove(id);
+ break;
+
+ case(PeerType.UploadQueue):
+ this.uploadQueue.Remove(id);
+ break;
+
+ default:
+ throw new NotSupportedException();
}
}
+ #endregion
public PeerList()
{
+ this.availablePeers = new List<PeerConnectionID>();
+ this.connectedPeers = new List<PeerConnectionID>();
+ this.connectingTo = new List<PeerConnectionID>();
+ this.downloadQueue = new List<PeerConnectionID>();
+ this.uploadQueue = new List<PeerConnectionID>();
}
}
}
Deleted: trunk/bitsharp/src/MonoTorrent.Client/Peers/Peers.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Peers/Peers.cs 2007-02-11
17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/Peers/Peers.cs 2007-02-11
17:12:46 UTC (rev 72625)
@@ -1,142 +0,0 @@
-//
-// Peers.cs
-//
-// Authors:
-// Alan McGovern [EMAIL PROTECTED]
-//
-// Copyright (C) 2006 Alan McGovern
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-
-using System.Collections.Generic;
-
-using MonoTorrent.Common;
-
-namespace MonoTorrent.Client
-{
- /// <summary>
- /// Represents a list of Peers that can be downloaded from
- /// </summary>
- internal class Peers
- {
- #region Member Variables
- private List<PeerConnectionID> peerList;
- #endregion
-
-
- #region Constructors
- /// <summary>
- /// Creates a new Peer list
- /// </summary>
- public Peers()
- : this(16)
- {
- }
-
-
- /// <summary>
- /// Creates a new Peer list
- /// </summary>
- /// <param name="capacity">The initial capacity of the list</param>
- public Peers(int capacity)
- {
- this.peerList = new List<PeerConnectionID>(capacity);
- }
- #endregion
-
-
- #region Methods
- /// <summary>
- /// Returns the PeerConnectionID at the specified index
- /// </summary>
- /// <param name="index">The index of the PeerConnectionID to
return</param>
- /// <returns></returns>
- public PeerConnectionID this[int index]
- {
- get { return this.peerList[index]; }
- set { this.peerList[index] = value; }
- }
-
-
- /// <summary>
- /// Adds a peer to the PeerConnectionID
- /// </summary>
- /// <param name="peer">The peer to add</param>
- public void Add(PeerConnectionID peer)
- {
- this.peerList.Add(peer);
- }
-
-
- /// <summary>
- /// Removes the PeerConnectionID at the specified index
- /// </summary>
- /// <param name="index">The index to remove the PeerConnectionID
at</param>
- public void Remove(int index)
- {
- this.peerList.RemoveAt(index);
- }
-
-
- /// <summary>
- /// Removes the supplied PeerConnectionID from the list
- /// </summary>
- /// <param name="id">The PeerConnectionID to remove</param>
- /// <returns>True if the PeerConnectionID was removed</returns>
- public bool Remove(PeerConnectionID id)
- {
- return this.peerList.Remove(id);
- }
-
-
- /// <summary>
- /// Returns the number of Peers in the list
- /// </summary>
- public int Count
- {
- get { return this.peerList.Count; }
- }
-
-
- /// <summary>
- /// Checks if the specified PeerConnectionID is in the list
- /// </summary>
- /// <param name="peer">The PeerConnectionID to search for</param>
- /// <returns>True if the PeerConnectionID was found, false
otherwise</returns>
- public bool Contains(PeerConnectionID peer)
- {
- return (this.peerList.Contains(peer));
- }
-
-
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public List<PeerConnectionID>.Enumerator GetEnumerator()
- {
- return this.peerList.GetEnumerator();
- }
- #endregion
- }
-}
\ No newline at end of file
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -74,7 +74,7 @@
}
}
- public RequestMessage PickPiece(PeerConnectionID id, Peers otherPeers)
+ public RequestMessage PickPiece(PeerConnectionID id,
List<PeerConnectionID> otherPeers)
{
lock (this.requestsLocker)
{
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -38,7 +38,7 @@
interface IPiecePicker
{
bool IsInteresting(PeerConnectionID id);
- RequestMessage PickPiece(PeerConnectionID id, Peers otherPeers);
+ RequestMessage PickPiece(PeerConnectionID id, List<PeerConnectionID>
otherPeers);
BitField MyBitField { get; }
int CurrentRequestCount();
void ReceivedRejectRequest(PeerConnectionID id, RejectRequestMessage
message);
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
2007-02-11 17:09:34 UTC (rev 72624)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
2007-02-11 17:12:46 UTC (rev 72625)
@@ -110,7 +110,7 @@
/// <param name="id">The id of the peer to request a piece off
of</param>
/// <param name="otherPeers">The other peers that are also downloading
the same torrent</param>
/// <returns></returns>
- public RequestMessage PickPiece(PeerConnectionID id, Peers otherPeers)
+ public RequestMessage PickPiece(PeerConnectionID id,
List<PeerConnectionID> otherPeers)
{
int requestIndex = 0;
RequestMessage message = null;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches