Author: alanmc
Date: 2006-12-03 12:17:12 -0500 (Sun, 03 Dec 2006)
New Revision: 68910
Modified:
trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/BufferManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs
Log:
Rewrote BufferManager to make it harder to lose buffers accidently. Fix so that
pieceManager.IsInteresting is called on all peers when i recieve a complete
piece. KeepAlive messages now fully supported and being sent when needed.
Note: Some memory intensive logging code is active in this commit which doubles
memory usage.
Modified: trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-12-03
17:12:35 UTC (rev 68909)
+++ trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-12-03
17:17:12 UTC (rev 68910)
@@ -478,12 +478,11 @@
peer.Connection = new TCPConnection(peerSocket, 0, new
NoEncryption());
id = new PeerConnectionID(peer);
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 68;
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None, peerHandshakeReceived, id,
out id.ErrorCode);
- this.listener.BeginAccept();
}
catch (SocketException)
{
@@ -493,6 +492,11 @@
catch (ObjectDisposedException)
{
}
+ finally
+ {
+ if (!this.listener.Disposed)
+ this.listener.BeginAccept();
+ }
}
@@ -547,14 +551,14 @@
id.Peer.PeerId = handshake.PeerId;
id.Peer.Connection.SupportsFastPeer =
handshake.SupportsFastPeer;
id.TorrentManager = man;
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.recieveBuffer);
- id.Peer.Connection.recieveBuffer = null;
+
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
id.Peer.Connection.ClientApp = new
PeerID(handshake.PeerId);
handshake = new
HandshakeMessage(id.TorrentManager.Torrent.InfoHash, ClientEngine.peerId,
VersionInfo.ProtocolStringV100);
BitfieldMessage bf = new
BitfieldMessage(id.TorrentManager.PieceManager.MyBitField);
- id.Peer.Connection.sendBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.LargeMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.LargeMessageBuffer);
id.Peer.Connection.BytesSent = 0;
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);
@@ -571,7 +575,7 @@
}
catch (NullReferenceException)
{
-#warning Why is this happening?
+ CleanupSocket(id);
}
}
@@ -582,8 +586,15 @@
/// <param name="id"></param>
private void CleanupSocket(PeerConnectionID id)
{
- lock(id)
- id.Peer.Connection.Dispose();
+ lock (id)
+ {
+ if (id.Peer.Connection != null)
+ {
+ BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
+ BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
+ id.Peer.Connection.Dispose();
+ }
+ }
}
@@ -635,4 +646,4 @@
}
#endregion
}
-}
+}
\ No newline at end of file
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/BufferManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/BufferManager.cs
2006-12-03 17:12:35 UTC (rev 68909)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/BufferManager.cs
2006-12-03 17:17:12 UTC (rev 68910)
@@ -16,73 +16,83 @@
public const int SmallMessageBufferSize = 256;
public const int LargeMessageBufferSize = ((1 << 14) + 32);
+ public static readonly byte[] EmptyBuffer = new byte[0];
private Queue<byte[]> smallMessageBuffers;
private Queue<byte[]> largeMessageBuffers;
+
+ /// <summary>
+ /// The class that controls the allocating and deallocating of all
byte[] buffers used in the engine.
+ /// </summary>
public BufferManager()
{
this.largeMessageBuffers = new Queue<byte[]>();
this.smallMessageBuffers = new Queue<byte[]>();
}
- private int largebuffers = 0;
- private int smallbuffers = 0;
- public byte[] GetBuffer(BufferType type)
+
+ /// <summary>
+ /// Allocates an existing buffer from the pool
+ /// </summary>
+ /// <param name="buffer">The byte[]you want the buffer to be assigned
to</param>
+ /// <param name="type">The type of buffer that is needed</param>
+ public void GetBuffer(ref byte[] buffer, BufferType type)
{
+ // We check to see if the buffer already there is the empty
buffer. If it isn't, then we have
+ // a buffer leak somewhere and the buffers aren't being freed
properly.
+ if (buffer != EmptyBuffer)
+ throw new ArgumentException("The oldBuffer should have been
recovered before getting a new buffer");
+
+ // If we're getting a small buffer and there are none in the pool,
just return a new one.
+ // Otherwise return one from the pool.
if (type == BufferType.SmallMessageBuffer)
lock (this.smallMessageBuffers)
- {
if (this.smallMessageBuffers.Count == 0)
- {
- Debug.WriteLine((++smallbuffers).ToString() + " Small
Buffers");
- return new byte[SmallMessageBufferSize];
- }
+ buffer = new byte[SmallMessageBufferSize];
else
- {
- //return new byte[SmallMessageBufferSize];
- return this.smallMessageBuffers.Dequeue();
- }
- }
+ buffer = this.smallMessageBuffers.Dequeue();
+ // If we're getting a large buffer and there are none in the pool,
just return a new one.
+ // Otherwise return one from the pool.
else if (type == BufferType.LargeMessageBuffer)
lock (this.largeMessageBuffers)
- {
if (this.largeMessageBuffers.Count == 0)
- {
- Debug.WriteLine((++largebuffers).ToString() + " Large
Buffers");
- return new byte[LargeMessageBufferSize];
- }
+ buffer = new byte[LargeMessageBufferSize];
else
- {
- //return new byte[LargeMessageBufferSize];
- return this.largeMessageBuffers.Dequeue();
- }
- }
+ buffer = this.largeMessageBuffers.Dequeue();
else
- throw new Exception("Error: Illegal buffer supplied");
-#warning Don't throw a System.Exception. Rethink the whole exception thing to
allow for better handling of my own exceptions
+ throw new ArgumentException("Couldn't allocate the required
buffer", "type");
}
- public void FreeBuffer(byte[] buffer)
+
+ /// <summary>
+ /// Returns a buffer to the pool after it has finished being used.
+ /// </summary>
+ /// <param name="buffer">The buffer to add back into the pool</param>
+ /// <returns></returns>
+ public void FreeBuffer(ref byte[] buffer)
{
- //buffer = null;
- //return;
-
- if (buffer == null)
+ // If true, the buffer has already been freed, so we just return
+ if (buffer == EmptyBuffer)
return;
+ // If the buffer is a small buffer, add it into that smallbuffer
queue
if (buffer.Length == SmallMessageBufferSize)
lock (this.smallMessageBuffers)
this.smallMessageBuffers.Enqueue(buffer);
+ // If the buffer is a large buffer, add it into the largebuffer
queue
else if (buffer.Length == LargeMessageBufferSize)
lock (this.largeMessageBuffers)
this.largeMessageBuffers.Enqueue(buffer);
+ // All buffers should be allocated in this class, so if something
else is passed in that isn't the right size
+ // We just throw an exception as someone has done something wrong.
else
throw new Exception("That buffer wasn't created by this
manager");
-#warning Don't throw a System.Exception. Rethink the whole exception thing to
allow for better handling of my own exceptions
+
+ buffer = EmptyBuffer; // After recovering the buffer, we send the
"EmptyBuffer" back as a placeholder
}
}
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2006-12-03 17:12:35 UTC (rev 68909)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
2006-12-03 17:17:12 UTC (rev 68910)
@@ -148,10 +148,11 @@
manager.ConnectingTo.Add(id);
lock (id)
{
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "
****Connecting****");
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting****");
id.Peer.Connection = new TCPConnection(id.Peer.Location,
id.TorrentManager.Torrent.Pieces.Length, new NoEncryption());
id.Peer.Connection.ProcessingQueue = true;
id.Peer.Connection.LastMessageSent = DateTime.Now;
+ id.Peer.Connection.LastMessageReceived = DateTime.Now;
id.Peer.Connection.BeginConnect(peerEndCreateConnection, id);
}
}
@@ -166,7 +167,7 @@
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -177,7 +178,7 @@
return;
id.Peer.Connection.EndConnect(result);
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "
****Connected****");
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connected****");
id.TorrentManager.ConnectingTo.Remove(id);
id.TorrentManager.ConnectedPeers.Add(id);
@@ -193,13 +194,12 @@
//FIXME: Can this be optimised more? How many
allocations is this responsible for?
HandshakeMessage handshake = new
HandshakeMessage(id.TorrentManager.Torrent.InfoHash, ClientEngine.PeerId,
VersionInfo.ProtocolStringV100);
- id.Peer.Connection.sendBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesToSend = 0;
id.Peer.Connection.BytesToSend +=
handshake.Encode(id.Peer.Connection.sendBuffer, 0);
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer, 0,
id.Peer.Connection.BytesToSend, SocketFlags.None, peerHandshakeSent, id, out
id.ErrorCode);
System.Threading.Interlocked.Increment(ref
this.openConnections);
- System.Threading.Interlocked.Decrement(ref
this.halfOpenConnections);
}
}
}
@@ -212,12 +212,12 @@
lock (id)
{
id.Peer.FailedConnectionAttempts++;
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "
****Connecting Failed****");
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting Failed****");
if (id.Peer.Connection != null)
{
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
+
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
id.Peer.Connection.ProcessingQueue = false;
id.Peer.Connection.Dispose();
}
@@ -227,8 +227,6 @@
if (id.Peer.FailedConnectionAttempts < 2) // We
couldn't connect this time, so re-add to available
id.TorrentManager.Available.Add(id);
-
- System.Threading.Interlocked.Decrement(ref
this.halfOpenConnections);
}
}
}
@@ -238,9 +236,9 @@
{
lock (id)
{
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "
****Connecting Failed****");
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Connecting Failed****");
+
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
id.Peer.FailedConnectionAttempts++;
id.Peer.Connection.ProcessingQueue = false;
if (id.Peer.Connection != null)
@@ -252,12 +250,12 @@
if (id.Peer.FailedConnectionAttempts < 10) // We
couldn't connect this time, so re-add to available
id.TorrentManager.Available.Add(id);
- System.Threading.Interlocked.Decrement(ref
this.halfOpenConnections);
}
}
}
finally
{
+ System.Threading.Interlocked.Decrement(ref
this.halfOpenConnections);
if (cleanUp)
CleanupSocket(id);
}
@@ -273,7 +271,7 @@
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -303,10 +301,10 @@
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
id.Peer.Connection.BytesSent, id.Peer.Connection.BytesToSend -
id.Peer.Connection.BytesSent, SocketFlags.None, peerHandshakeSent, id, out
id.ErrorCode);
return;
}
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() +
"Handshake");
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() +
"Handshake");
+
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 68; //
FIXME: Will fail if protocol version changes. FIX THIS
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None, peerHandshakeReceieved,
id, out id.ErrorCode);
@@ -340,7 +338,7 @@
IPeerMessageInternal msg;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -371,7 +369,7 @@
return;
}
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() +
"\tHandshake");
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() +
"\tHandshake");
msg = new HandshakeMessage();
msg.Decode(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve);
HandshakeMessage handshake = msg as HandshakeMessage;
@@ -404,9 +402,8 @@
msg = new
BitfieldMessage(id.TorrentManager.PieceManager.MyBitField);
}
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.recieveBuffer);
- id.Peer.Connection.recieveBuffer = null;
- id.Peer.Connection.sendBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.LargeMessageBuffer);
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.LargeMessageBuffer);
id.Peer.Connection.BytesSent = 0;
id.Peer.Connection.BytesToSend =
msg.Encode(id.Peer.Connection.sendBuffer, 0);
@@ -439,7 +436,7 @@
{
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
- result.AsyncWaitHandle.Close();
+ result.AsyncWaitHandle.Close();
try
{
lock (id.TorrentManager.listLock)
@@ -469,10 +466,9 @@
id.Peer.Connection.BeginSend(id.Peer.Connection.sendBuffer,
id.Peer.Connection.BytesSent, id.Peer.Connection.BytesToSend -
id.Peer.Connection.BytesSent, SocketFlags.None, peerBitfieldSent, id, out
id.ErrorCode);
return;
}
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() +
"BitfieldMessage");
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() +
"BitfieldMessage");
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve = 4;
@@ -506,7 +502,7 @@
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -541,17 +537,16 @@
id.Peer.Connection.BytesReceived = 0;
id.Peer.Connection.BytesToRecieve =
System.Net.IPAddress.NetworkToHostOrder(networkOrderLength);
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.recieveBuffer);
- id.Peer.Connection.recieveBuffer = null;
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
if (id.Peer.Connection.BytesToRecieve >
BufferManager.SmallMessageBufferSize)
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.LargeMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.LargeMessageBuffer);
else
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
- id.Peer.Connection.LastMessageReceived = DateTime.Now;
if (id.Peer.Connection.BytesToRecieve == 0) // We
Received a KeepAlive
{
+ id.Peer.Connection.LastMessageReceived =
DateTime.Now;
id.Peer.Connection.BytesToRecieve = 4;
id.Peer.Connection.BeginReceive(id.Peer.Connection.recieveBuffer, 0,
id.Peer.Connection.BytesToRecieve, SocketFlags.None, peerMessageLengthReceived,
id, out id.ErrorCode);
}
@@ -596,7 +591,7 @@
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -640,7 +635,7 @@
if (this.OnPeerMessages != null)
this.OnPeerMessages(id, new
PeerMessageEventArgs((IPeerMessage)message, Direction.Incoming));
message.Handle(id);
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "\t" +
message.ToString());
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "\t" +
message.ToString());
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);
@@ -650,10 +645,10 @@
}
id.Peer.Connection.LastMessageReceived = DateTime.Now;
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.recieveBuffer);
- id.Peer.Connection.recieveBuffer = null;
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
+
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, peerMessageLengthReceived,
id, out id.ErrorCode);
@@ -697,7 +692,7 @@
bool cleanUp = false;
PeerConnectionID id = (PeerConnectionID)result.AsyncState;
result.AsyncWaitHandle.Close();
-
+
try
{
lock (id.TorrentManager.listLock)
@@ -741,8 +736,8 @@
}
if (this.OnPeerMessages != null)
this.OnPeerMessages(id, new
PeerMessageEventArgs((IPeerMessage)id.Peer.Connection.CurrentlySendingMessage,
Direction.Outgoing));
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
+
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
//Console.WriteLine("SENT " +
id.Peer.Connection.CurrentlySendingMessage.ToString());
id.Peer.Connection.LastMessageSent = DateTime.Now;
this.ProcessQueue(id);
@@ -783,15 +778,15 @@
if (msg is PieceMessage)
id.Peer.Connection.PiecesSent++;
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + " " +
msg.ToString());
+ id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString()
+ " " + msg.ToString());
id.Peer.Connection.ProcessingQueue = true;
try
{
if (msg.ByteLength > BufferManager.SmallMessageBufferSize)
- id.Peer.Connection.sendBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.LargeMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.LargeMessageBuffer);
else
- id.Peer.Connection.sendBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.sendBuffer, BufferType.SmallMessageBuffer);
id.Peer.Connection.BytesSent = 0;
id.Peer.Connection.BytesToSend =
msg.Encode(id.Peer.Connection.sendBuffer, 0);
@@ -878,6 +873,7 @@
{
lock (id)
{
+ Interlocked.Increment(ref this.openConnections);
bytesSent = id.Peer.Connection.EndSend(result, out
id.ErrorCode);
if (bytesSent == 0)
{
@@ -907,10 +903,9 @@
id.TorrentManager.Available.Remove(id);
id.TorrentManager.ConnectedPeers.Add(id);
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
- id.Peer.Connection.sendBuffer = null;
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
- id.Peer.Connection.recieveBuffer =
ClientEngine.BufferManager.GetBuffer(BufferType.SmallMessageBuffer);
+ ClientEngine.BufferManager.GetBuffer(ref
id.Peer.Connection.recieveBuffer, BufferType.SmallMessageBuffer);
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.peerMessageLengthReceived, id, out id.ErrorCode);
@@ -944,19 +939,16 @@
{
System.Threading.Interlocked.Decrement(ref
openConnections);
id.TorrentManager.PieceManager.RemoveRequests(id);
-
//id.Peer.MessageHistory.AppendLine(DateTime.Now.ToShortTimeString() + "
****Cleaning Up****");
+
id.Peer.MessageHistory.AppendLine(DateTime.Now.ToLongTimeString() + "
****Cleaning Up****");
if (id.Peer.Connection != null)
{
if (this.OnPeerConnectionChanged != null)
this.OnPeerConnectionChanged(id, new
PeerConnectionEventArgs(PeerConnectionEvent.Disconnected));
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.sendBuffer);
-
ClientEngine.BufferManager.FreeBuffer(id.Peer.Connection.recieveBuffer);
- id.Peer.Connection.sendBuffer = null;
- id.Peer.Connection.recieveBuffer = null;
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.sendBuffer);
+ ClientEngine.BufferManager.FreeBuffer(ref
id.Peer.Connection.recieveBuffer);
-
if (!id.Peer.Connection.AmChoking)
id.TorrentManager.UploadingTo--;
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2006-12-03 17:12:35 UTC (rev 68909)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2006-12-03 17:17:12 UTC (rev 68910)
@@ -413,8 +413,8 @@
if (counter % 40 == 0) // Call it every second...
ish
id.Peer.Connection.Monitor.TimePeriodPassed();
- // if (counter % 500 == 0)
- // DumpStats(id, counter);
+ if (counter % 500 == 0)
+ DumpStats(id, counter);
if (id.Peer.Connection.IsInterestingToMe &&
(!id.Peer.Connection.AmInterested))
{
@@ -471,10 +471,11 @@
id.Peer.Connection.EnQueue(msg);
id.Peer.Connection.AmRequestingPiecesCount++;
}
+ if((DateTime.Now - id.Peer.Connection.LastMessageSent
> new TimeSpan(0,0,90)))
+ id.Peer.Connection.EnQueue(new KeepAliveMessage());
- if ((DateTime.Now -
id.Peer.Connection.LastMessageReceived) > new TimeSpan(0, 0, 30) &&
id.Peer.Connection.AmRequestingPiecesCount > 0)
+ if ((DateTime.Now -
id.Peer.Connection.LastMessageReceived > new TimeSpan(0, 0, 120)))
{
- this.pieceManager.RemoveRequests(id);
ClientEngine.ConnectionManager.CleanupSocket(id);
continue;
}
@@ -515,15 +516,14 @@
(int)(this.UploadSpeed()));
}
}
-#warning Figure out how [Conditional("DEBUG")] works
- /* private void DumpStats(PeerConnectionID id, int counter)
+ private void DumpStats(PeerConnectionID id, int counter)
{
string path = Path.Combine(@"C:\Docs\" + counter.ToString(),
id.Peer.Location.GetHashCode() + ".txt");
if(!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (FileStream stream = File.Create(path))
stream.Write(System.Text.UTF8Encoding.UTF8.GetBytes(id.Peer.MessageHistory.ToString()),0,
System.Text.UTF8Encoding.UTF8.GetByteCount(id.Peer.MessageHistory.ToString()));
- }*/
+ }
/// <summary>
@@ -843,8 +843,11 @@
for (int i = 0; i < this.connectedPeers.Count; i++)
lock (this.connectedPeers[i])
if (this.connectedPeers[i].Peer.Connection != null)
+ {
+
this.connectedPeers[i].Peer.Connection.IsInterestingToMe =
this.pieceManager.IsInteresting(this.connectedPeers[i]);
if
(!this.connectedPeers[i].Peer.Connection.BitField[p])
this.connectedPeers[i].Peer.Connection.EnQueue(new HaveMessage(p));
+ }
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs 2006-12-03 17:12:35 UTC
(rev 68909)
+++ trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs 2006-12-03 17:17:12 UTC
(rev 68910)
@@ -138,6 +138,6 @@
}
#endregion
- //public StringBuilder MessageHistory = new StringBuilder(1024);
+ public StringBuilder MessageHistory = new StringBuilder(1024);
}
}
\ No newline at end of file
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches