Author: alanmc
Date: 2007-06-20 19:20:08 -0400 (Wed, 20 Jun 2007)
New Revision: 80384

Modified:
   trunk/bitsharp/src/MonoTorrent/Client/BufferedFileWrite.cs
   trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs
   trunk/bitsharp/src/MonoTorrent/Client/Managers/PieceManager.cs
   trunk/bitsharp/src/MonoTorrent/Client/PiecePicking/StandardPicker.cs
Log:
Fixed a bug where pieces were being left in the unhashedpieces list

Modified: trunk/bitsharp/src/MonoTorrent/Client/BufferedFileWrite.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/BufferedFileWrite.cs  2007-06-20 
23:15:32 UTC (rev 80383)
+++ trunk/bitsharp/src/MonoTorrent/Client/BufferedFileWrite.cs  2007-06-20 
23:20:08 UTC (rev 80384)
@@ -15,22 +15,19 @@
         public IPeerMessageInternal Message;
         public Piece Piece;
         public BitField BitField;
-        public IntCollection UnhashedPieces;
 
                #endregion Fields
 
 
                #region Constructors
 
-        public BufferedFileWrite(PeerId id, ArraySegment<byte> buffer, 
IPeerMessageInternal message, Piece piece, BitField bitfield,
-            IntCollection unhashedPieces)
+        public BufferedFileWrite(PeerId id, ArraySegment<byte> buffer, 
IPeerMessageInternal message, Piece piece, BitField bitfield)
         {
             this.Id = id;
             this.Buffer = buffer;
             this.Message = message;
             this.Piece = piece;
             this.BitField = bitfield;
-            this.UnhashedPieces = unhashedPieces;
         }
         
                #endregion Constructors

Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs       
2007-06-20 23:15:32 UTC (rev 80383)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs       
2007-06-20 23:20:08 UTC (rev 80384)
@@ -410,8 +410,8 @@
             // Hashcheck the piece as we now have all the blocks.
             bool result = 
id.TorrentManager.Torrent.Pieces.IsValid(id.TorrentManager.FileManager.GetHash(piece.Index),
 piece.Index);
             id.TorrentManager.Bitfield[message.PieceIndex] = result;
-            lock (bufferedFileIO.UnhashedPieces)
-                bufferedFileIO.UnhashedPieces.Remove(piece.Index);
+            lock (id.TorrentManager.PieceManager.UnhashedPieces)
+                id.TorrentManager.PieceManager.UnhashedPieces[piece.Index] = 
false;
 
             id.TorrentManager.HashedPiece(new 
PieceHashedEventArgs(piece.Index, result));
 
@@ -442,7 +442,7 @@
         /// <param name="recieveBuffer">The array containing the block</param>
         /// <param name="message">The PieceMessage</param>
         /// <param name="piece">The piece that the block to be written is part 
of</param>
-        internal void QueueWrite(PeerId id, ArraySegment<byte> recieveBuffer, 
PieceMessage message, Piece piece, IntCollection unhashedPieces)
+        internal void QueueWrite(PeerId id, ArraySegment<byte> recieveBuffer, 
PieceMessage message, Piece piece)
         {
             lock (this.queueLock)
             {
@@ -453,7 +453,7 @@
                 ClientEngine.BufferManager.GetBuffer(ref buffer, 
recieveBuffer.Count);
                 Buffer.BlockCopy(recieveBuffer.Array, recieveBuffer.Offset, 
buffer.Array, buffer.Offset, recieveBuffer.Count);
 
-                bufferedWrites.Enqueue(new BufferedFileWrite(id, buffer, 
message, piece, id.TorrentManager.Bitfield, unhashedPieces));
+                bufferedWrites.Enqueue(new BufferedFileWrite(id, buffer, 
message, piece, id.TorrentManager.Bitfield));
                 SetHandleState(true);
             }
         }
@@ -470,7 +470,7 @@
         {
             lock (this.queueLock)
             {
-                this.bufferedReads.Enqueue(new BufferedFileWrite(id, 
recieveBuffer, message, piece, id.TorrentManager.Bitfield, null));
+                this.bufferedReads.Enqueue(new BufferedFileWrite(id, 
recieveBuffer, message, piece, id.TorrentManager.Bitfield));
                 SetHandleState(true);
             }
         }

Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/PieceManager.cs      
2007-06-20 23:15:32 UTC (rev 80383)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/PieceManager.cs      
2007-06-20 23:20:08 UTC (rev 80384)
@@ -176,6 +176,11 @@
             this.piecePicker.Reset();
         }
 
+        internal BitField UnhashedPieces
+        {
+            get { return ((StandardPicker)this.piecePicker).UnhashedPieces; }
+        }
+
         #endregion
 
 

Modified: trunk/bitsharp/src/MonoTorrent/Client/PiecePicking/StandardPicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/PiecePicking/StandardPicker.cs        
2007-06-20 23:15:32 UTC (rev 80383)
+++ trunk/bitsharp/src/MonoTorrent/Client/PiecePicking/StandardPicker.cs        
2007-06-20 23:20:08 UTC (rev 80384)
@@ -41,12 +41,12 @@
         private int[] priorities;
 
         private BitField bufferBitfield;
-        private BitField previousBitfield;
+        private List<BitField> previousBitfields;
         private BitField isInterestingBuffer;
+        private BitField unhashedPieces;   // Store the index of finished 
pieces which are not hashed. These count as "AlreadyHaveOrRequested"
 
 
         private TorrentFile[] torrentFiles;
-        private IntCollection unhashedPieces;   // Store the index of finished 
pieces which are not hashed. These count as "AlreadyHaveOrRequested"
         private Dictionary<PeerId, PieceCollection> requests;
         internal Dictionary<PeerId, PieceCollection> Requests
         {
@@ -71,6 +71,12 @@
             }
             return result;
         }
+
+        internal BitField UnhashedPieces
+        {
+            get { return this.unhashedPieces; }
+        }
+
         #endregion
 
 
@@ -91,9 +97,10 @@
 
             this.myBitfield = bitField;
             this.bufferBitfield = new BitField(myBitfield.Length);
-            this.previousBitfield = new BitField(myBitfield.Length);
+            this.previousBitfields = new List<BitField>();
+            this.previousBitfields.Add(new BitField(myBitfield.Length);
             this.torrentFiles = torrentFiles;
-            this.unhashedPieces = new IntCollection(8);
+            this.unhashedPieces = new BitField(myBitfield.Length);
             this.requests = new Dictionary<PeerId, PieceCollection>();
 
             // Order the priorities in decending order of priority. i.e. 
Immediate is first, and DoNotDownload is last
@@ -122,7 +129,7 @@
                         return true;
 
             lock (this.unhashedPieces)
-                return this.unhashedPieces.Contains(index);
+                return this.unhashedPieces[index];
         }
 
 
@@ -332,6 +339,7 @@
 
         private RequestMessage GetStandardRequest(PeerId id, PeerIdCollection 
otherPeers)
         {
+            int bitfieldIndex = 0;
             int checkIndex = 0;
             RequestMessage message = null;
             Priority highestPriorityFound = Priority.DoNotDownload;
@@ -342,7 +350,7 @@
             if (!id.Peer.IsSeeder)
                 this.bufferBitfield.AndFast(id.Peer.Connection.BitField);
 
-            Buffer.BlockCopy(this.bufferBitfield.Array, 0, 
this.previousBitfield.Array, 0, this.bufferBitfield.Array.Length * 4);
+            Buffer.BlockCopy(this.bufferBitfield.Array, 0, 
this.previousBitfields[bitfieldIndex].Array, 0, 
this.bufferBitfield.Array.Length * 4);
 
             // Out of the pieces the other peer has that we want, we look for 
the highest priority and store it here.
             // We then keep NANDing the bitfield until we no longer have 
pieces with that priority. Then we can pick
@@ -372,13 +380,13 @@
             // When picking the piece, we start at a random index and then 
scan forwards to select the first available piece.
             // If none is found, we scan from the start up until that random 
index. If nothing is found, the peer is actually
             // uninteresting.
-            int midPoint = random.Next(0, this.previousBitfield.Length);
-            int endIndex = this.previousBitfield.Length;
+            int midPoint = random.Next(0, this.myBitfield.Length);
+            int endIndex = this.myBitfield.Length;
             checkIndex = midPoint;
 
             // First we check all the places from midpoint -> end
             // Then if we dont find anything we check from 0 -> midpoint
-            while ((checkIndex = this.previousBitfield.FirstTrue(checkIndex, 
endIndex)) != -1)
+            while ((checkIndex = 
this.previousBitfields[bitfieldIndex].FirstTrue(checkIndex, endIndex)) != -1)
                 if (AlreadyHaveOrRequested(checkIndex))
                     checkIndex++;
                 else
@@ -387,7 +395,7 @@
             if (checkIndex == -1)
             {
                 checkIndex = 0;
-                while ((checkIndex = 
this.previousBitfield.FirstTrue(checkIndex, midPoint)) != -1)
+                while ((checkIndex = 
this.previousBitfields[bitfieldIndex].FirstTrue(checkIndex, midPoint)) != -1)
                     if (AlreadyHaveOrRequested(checkIndex))
                         checkIndex++;
                     else
@@ -543,13 +551,15 @@
                 piece.Blocks[blockIndex].Received = true;
                 id.Peer.Connection.AmRequestingPiecesCount--;
                 id.TorrentManager.PieceManager.RaiseBlockReceived(new 
BlockEventArgs(piece.Blocks[blockIndex], piece, id));
-                lock (this.unhashedPieces)
-                    if (!this.unhashedPieces.Contains(piece.Index))
-                        this.unhashedPieces.Add(piece.Index);
-                id.TorrentManager.FileManager.QueueWrite(id, recieveBuffer, 
message, piece, unhashedPieces);
+                id.TorrentManager.FileManager.QueueWrite(id, recieveBuffer, 
message, piece);
 
                 if (piece.AllBlocksReceived)
                 {
+#warning review usage of the unhashedpieces variable
+                    lock (this.unhashedPieces)
+                        if (!this.myBitfield[piece.Index])
+                            this.unhashedPieces[piece.Index] = true;
+
                     pieces.Remove(piece);
 
                     if (pieces.Count == 0)
@@ -620,7 +630,7 @@
 
         public override void Reset()
         {
-            this.unhashedPieces.Clear();
+            this.unhashedPieces.SetAll(false);
             this.requests.Clear();
         }
         #endregion

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

Reply via email to