Author: alanmc
Date: 2008-01-03 10:48:16 -0500 (Thu, 03 Jan 2008)
New Revision: 92183

Modified:
   trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs
   trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
Log:
Feature: All code which could result in a blocking operation has been removed 
from TorrentManager.Start(). Code dealing with hashchecking a torrent has been 
simplified and consolidated into one method.

Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs       
2008-01-03 15:42:02 UTC (rev 92182)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/FileManager.cs       
2008-01-03 15:48:16 UTC (rev 92183)
@@ -56,7 +56,6 @@
         private string baseDirectory;                           // The base 
directory into which all the files will be put
         private long fileSize;                                  // The 
combined length of all the files
         private SHA1Managed hasher;                             // The SHA1 
hasher used to calculate the hash of a piece
-        private bool initialHashRequired;                       // Used to 
indicate whether we need to hashcheck the files or not
         private string savePath;                                // The path 
where the base directory will be put
         private TorrentManager manager;
         private TorrentFile[] files;
@@ -83,15 +82,6 @@
         }
 
         /// <summary>
-        /// True if we need to hash the files (i.e. some were preexisting)
-        /// </summary>
-        internal bool InitialHashRequired
-        {
-            get { return this.initialHashRequired; }
-            set { this.initialHashRequired = value; }
-        }
-
-        /// <summary>
         /// The length of a piece in bytes
         /// </summary>
         internal int PieceLength
@@ -139,26 +129,17 @@
         internal FileManager(TorrentManager manager, TorrentFile[] files, int 
pieceLength, string savePath, string baseDirectory)
         {
             this.baseDirectory = baseDirectory;
-            //this.streamsLock = new ReaderWriterLock();
-            //this.queueLock = new object();
-            //this.bufferedReads = new Queue<BufferedIO>();
-            //this.bufferedWrites = new Queue<BufferedIO>();
             this.hasher = new SHA1Managed();
-            this.initialHashRequired = false;
-            //this.ioActive = true;
             this.manager = manager;
             this.savePath = savePath;
-            //this.threadWait = new ManualResetEvent(false);
             this.files = files;
             this.pieceLength = pieceLength;
 
             foreach (TorrentFile file in files)
                 fileSize += file.Length;
-
-            initialHashRequired = CheckFilesExist();
         }
 
-        private bool CheckFilesExist()
+        internal bool CheckFilesExist()
         {
             foreach (TorrentFile file in files)
                 if(File.Exists(GenerateFilePath(file, BaseDirectory, 
savePath)))
@@ -281,11 +262,11 @@
                 // We need to delete the old fast resume data so in the event 
of a crash we don't 
                 // accidently reload it and think we've downloaded less data 
than we actually have
                 File.Delete(fastResumePath);
-                manager.loadedFastResume = true;
                 return true;
             }
             catch
             {
+                manager.PieceManager.MyBitField.SetAll(false);
                 return false;
             }
         }

Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs    
2008-01-03 15:42:02 UTC (rev 92182)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs    
2008-01-03 15:48:16 UTC (rev 92183)
@@ -81,7 +81,6 @@
         private bool hashChecked;               // True if the manager has 
been hash checked
         private int hashFails;                  // The total number of pieces 
receieved which failed the hashcheck
         internal object listLock;               // The object we use to 
syncronize list access
-        internal bool loadedFastResume;         // Used to fire the 
"PieceHashed" event if fast resume data was loaded
         private ConnectionMonitor monitor;      // Calculates download/upload 
speed
         private PeerManager peers;              // Stores all the peers we 
know of in a list
         private PieceManager pieceManager;      // Tracks all the piece 
requests we've made and decides what pieces we can request off each peer
@@ -397,37 +396,19 @@
                     return;
                 }
 
-                // If the torrent needs to be hashed, hash it. If it's already 
in the process of being hashed
-                // just return
-                if (this.fileManager.InitialHashRequired)
+                // If the torrent has not been hashed, we start the hashing 
process then we wait for it to finish
+                // before attempting to start again
+                if (!hashChecked)
                 {
-                    if (!this.hashChecked && !(this.state == 
TorrentState.Hashing))
-                    {
+                    if (state != TorrentState.Hashing)
                         HashCheck(false, true);
-                        return;
-                    }
-
-                    else if (!this.hashChecked)
-                    {
-                        return;
-                    }
+                    return;
                 }
 
-                this.hashChecked = true;
 
                 if (this.state == TorrentState.Seeding || this.state == 
TorrentState.SuperSeeding || this.state == TorrentState.Downloading)
                     throw new TorrentException("Torrent is already running");
 
-                // If we loaded the fast resume data, we fire the piece hashed 
event as if we had read
-                //  the pieces from the harddrive.
-                if (this.loadedFastResume)
-                {
-                    for (int i = 0; i < this.bitfield.Length; i++)
-                        RaisePieceHashed(new PieceHashedEventArgs(this, i, 
this.bitfield[i]));
-
-                    this.loadedFastResume = false;
-                }
-
                 if (TrackerManager.CurrentTracker.CanScrape)
                     this.TrackerManager.Scrape();
 
@@ -536,10 +517,6 @@
             return count;
         }
 
-
-
-
-
         internal void PreLogicTick(int counter)
         {
             PeerIdInternal id;
@@ -581,8 +558,6 @@
             }
         }
 
-
-
         internal void PostLogicTick(int counter)
         {
             PeerIdInternal id;
@@ -793,34 +768,39 @@
                 enterCount++;
                 // Store the value for whether the streams are open or not
                 // If they are initially closed, we need to close them again 
after we hashcheck
-                bool streamsOpen = this.fileManager.StreamsOpen;
                 bool forceCheck = ((bool[])state)[0];
                 bool autoStart = ((bool[])state)[1];
 
+                // We only need to hashcheck if at least one file already 
exists on the disk
+                bool filesExist = fileManager.CheckFilesExist();
+
+                // We only load fast resume if one (or more) of the files 
exist on disk.
+                bool loadedFastResume = filesExist && 
FileManager.LoadFastResume(this);
+
                 // If we are performing a forced scan OR we aren't forcing a 
full scan but can't load the fast resume data
                 // perform a full scan.
-                if (forceCheck || (!forceCheck && 
!FileManager.LoadFastResume(this)))
+                if (filesExist && (forceCheck || !loadedFastResume))
                 {
                     for (int i = 0; i < this.torrent.Pieces.Count; i++)
                     {
-                        bool temp = 
this.torrent.Pieces.IsValid(this.fileManager.GetHash(i), i);
+                        bitfield[i] = 
this.torrent.Pieces.IsValid(this.fileManager.GetHash(i), i);
                         
System.Threading.Monitor.Exit(this.engine.asyncCompletionLock);
                         enterCount--;
-                        this.pieceManager.MyBitField[i] = temp;
-                        RaisePieceHashed(new PieceHashedEventArgs(this, i, 
temp));
+                        RaisePieceHashed(new PieceHashedEventArgs(this, i, 
bitfield[i]));
                         
System.Threading.Monitor.Enter(this.engine.asyncCompletionLock);
                         enterCount++;
+
+                        // This happens if the user cancels the hash by 
stopping the torrent.
                         if (State != TorrentState.Hashing)
-                        {
-                            this.bitfield.SetAll(false);
                             return;
-                        }
                     }
-
-                    SaveFastResume();
                 }
 
-                this.fileManager.InitialHashRequired = false;
+                // If we loaded fastresume data then we need to fire the piece 
hashed events
+                if (loadedFastResume)
+                    for (int i = 0; i < bitfield.Length; i++)
+                        RaisePieceHashed(new PieceHashedEventArgs(this, i, 
bitfield[i]));
+
                 this.hashChecked = true;
 
                 if (autoStart)

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

Reply via email to