Author: alanmc
Date: 2006-11-26 15:38:29 -0500 (Sun, 26 Nov 2006)
New Revision: 68486

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
   trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.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
   trunk/bitsharp/src/MonoTorrent.Common/Enums.cs
Log:
If the peer sends 3 or more bad pieces, the peer is disconnected. The hashfails 
are recorded as part of the Peer class as opposed to the PeerConnection class 
so that even that if the peer reconnects later, i can disconnect him again. 
Gets reset when the client is restarted.

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs 
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs 
2006-11-26 20:38:29 UTC (rev 68486)
@@ -641,6 +641,8 @@
                         {   // The '-4' is because of the messagelength int 
which has already been counted in a different method
                             id.TorrentManager.DataBytesDownloaded -= 
(message.ByteLength - 4);
                             id.TorrentManager.ProtocolBytesDownloaded += 
(message.ByteLength - 4);
+                            if (id.Peer.HashFails == 3)
+                                cleanUp = true;
                         }
 
                         id.Peer.Connection.LastMessageReceived = DateTime.Now;

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2006-11-26 20:38:29 UTC (rev 68486)
@@ -123,10 +123,10 @@
         }
 
 
-        internal void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, 
int dataOffset, int writeIndex, int blockLength, PieceMessage message)
+        internal PieceEvent ReceivedPieceMessage(PeerConnectionID id, byte[] 
buffer, int dataOffset, int writeIndex, int blockLength, PieceMessage message)
         {
             lock (this.piecePicker)
-                this.piecePicker.ReceivedPieceMessage(id, buffer, dataOffset, 
writeIndex, blockLength, message);
+                return this.piecePicker.ReceivedPieceMessage(id, buffer, 
dataOffset, writeIndex, blockLength, message);
         }
         #endregion
     }

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs  
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs  
2006-11-26 20:38:29 UTC (rev 68486)
@@ -30,6 +30,7 @@
 
 using System;
 using System.Net;
+using MonoTorrent.Common;
 
 namespace MonoTorrent.Client.PeerMessages
 {
@@ -152,7 +153,9 @@
         internal void Handle(PeerConnectionID id)
         {
             int writeIndex = this.StartOffset + this.PieceIndex * 
this.fileManager.PieceLength;
-            id.TorrentManager.PieceManager.ReceivedPieceMessage(id, 
id.Peer.Connection.recieveBuffer, this.dataOffset, writeIndex, 
this.blockLength, this);
+            PieceEvent pevent = 
id.TorrentManager.PieceManager.ReceivedPieceMessage(id, 
id.Peer.Connection.recieveBuffer, this.dataOffset, writeIndex, 
this.blockLength, this);
+            if (pevent == PieceEvent.HashFailed)
+                id.Peer.HashFails++;
             //id.TorrentManager.PieceManager.ReceivedPiece(id, this);
         }
 

Modified: trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs 2006-11-26 20:28:56 UTC 
(rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/Peers/Peer.cs 2006-11-26 20:38:29 UTC 
(rev 68486)
@@ -52,6 +52,17 @@
 
 
         /// <summary>
+        /// Returns the number of times the peer has sent us a piece which 
failed a hashcheck
+        /// </summary>
+        public int HashFails
+        {
+            get { return this.hashFails; }
+            internal set { this.hashFails = value; }
+        }
+        private int hashFails;
+
+
+        /// <summary>
         /// The ID of the peer
         /// </summary>
         public string PeerId

Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs 
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs 
2006-11-26 20:38:29 UTC (rev 68486)
@@ -97,7 +97,7 @@
         }
 
 
-        public void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, 
int dataOffset, long writeIndex, int blockLength, PieceMessage message)
+        public PieceEvent ReceivedPieceMessage(PeerConnectionID id, byte[] 
buffer, int dataOffset, long writeIndex, int blockLength, PieceMessage message)
         {
             lock (this.requestsLocker)
             {
@@ -108,25 +108,25 @@
                         p = this.pieces[i];
 
                 if (p == null)
-                    return;
+                    return PieceEvent.BlockNotRequested;
 
                 for (int i = 0; i < p.Blocks.Length; i++)
                     if (p[i].StartOffset == message.StartOffset)
                         b = p[i];
 
                 if (b == null)
-                    return;
+                    return PieceEvent.BlockNotRequested;
 
                 if (message.BlockLength != b.RequestLength)
                     throw new Exception("Request length should match block 
length");
 
-                if(!b.Received)
+                if (!b.Received)
                     id.TorrentManager.FileManager.Write(buffer, dataOffset, 
writeIndex, blockLength);
                 b.Received = true;
                 id.Peer.Connection.AmRequestingPiecesCount--;
-                
+
                 if (!p.AllBlocksReceived)
-                    return;
+                    return PieceEvent.BlockWrittenToDisk;
 
                 bool result = 
ToolBox.ByteMatch(id.TorrentManager.Torrent.Pieces[p.Index], 
id.TorrentManager.FileManager.GetHash(p.Index));
                 this.myBitfield[message.PieceIndex] = result;
@@ -150,6 +150,8 @@
                         p.Blocks[i].Received = false;
                     }
                 }
+
+                return result ? PieceEvent.HashPassed : PieceEvent.HashFailed;
             }
         }
         #endregion

Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs  
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs  
2006-11-26 20:38:29 UTC (rev 68486)
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Text;
 using MonoTorrent.Client.PeerMessages;
+using MonoTorrent.Common;
 
 namespace MonoTorrent.Client
 {
@@ -13,6 +14,6 @@
         int CurrentRequestCount();
         void ReceivedRejectRequest(PeerConnectionID id, RejectRequestMessage 
message);
         void RemoveRequests(PeerConnectionID id);
-        void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, int 
dataOffset, long writeIndex, int blockLength, PieceMessage message);
+        PieceEvent ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, 
int dataOffset, long writeIndex, int blockLength, PieceMessage message);
     }
 }

Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs        
2006-11-26 20:28:56 UTC (rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs        
2006-11-26 20:38:29 UTC (rev 68486)
@@ -250,12 +250,12 @@
         /// <param name="writeIndex"></param>
         /// <param name="p"></param>
 
-        public void ReceivedPieceMessage(PeerConnectionID id, byte[] 
recieveBuffer, int offset, long writeIndex, int p, PieceMessage message)
+        public PieceEvent ReceivedPieceMessage(PeerConnectionID id, byte[] 
recieveBuffer, int offset, long writeIndex, int p, PieceMessage message)
         {
             lock (this.requests)
             {
                 if (!this.requests.ContainsKey(id))
-                    return;
+                    return PieceEvent.BlockNotRequested;
 #warning I should close the connection, but at the moment i'm in a bug fixing 
mood
 
                 Piece piece = null;
@@ -272,7 +272,7 @@
 
                 if (piece == null)
                 {
-                    return;
+                    return PieceEvent.BlockNotRequested;
 #warning Handle this properly. Does this mean that i should close off the 
peers connection because they sent me a piece i didn't request?
                 }
 
@@ -295,7 +295,7 @@
 
 
                 if (!piece.AllBlocksReceived)
-                    return;
+                    return PieceEvent.BlockWrittenToDisk;
 
                 bool result = 
ToolBox.ByteMatch(id.TorrentManager.Torrent.Pieces[piece.Index], 
id.TorrentManager.FileManager.GetHash(piece.Index));
                 this.myBitfield[message.PieceIndex] = result;
@@ -312,6 +312,8 @@
 
                 if (pieces.Count == 0)
                     this.requests.Remove(id);
+
+                return result ? PieceEvent.HashPassed : PieceEvent.HashFailed;
             }
         }
 

Modified: trunk/bitsharp/src/MonoTorrent.Common/Enums.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Common/Enums.cs      2006-11-26 20:28:56 UTC 
(rev 68485)
+++ trunk/bitsharp/src/MonoTorrent.Common/Enums.cs      2006-11-26 20:38:29 UTC 
(rev 68486)
@@ -102,14 +102,9 @@
 
     public enum PieceEvent
     {
-        BlockSent,
-        BlockReceived,
+        BlockNotRequested,
         BlockWrittenToDisk,
-        PieceSent,
-        PieceReceived,
-        PieceHashFailed,
-        PieceHashSucceeded,
-        PieceBuffered,          // These two
-        PieceWrittenToDisk      // aren't used yet
+        HashPassed,
+        HashFailed
     }
-}
\ No newline at end of file
+}

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to