Author: alanmc
Date: 2006-11-01 21:43:16 -0500 (Wed, 01 Nov 2006)
New Revision: 67253

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
Log:
Completely untested, but it should be fine ;) Fix to make sure that 
TorrentStateChanges will always be fired when the state changes

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs    
2006-11-02 02:35:09 UTC (rev 67252)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs    
2006-11-02 02:43:16 UTC (rev 67253)
@@ -343,12 +343,7 @@
             {
                 if (!this.hashChecked && !(this.state == TorrentState.Hashing))
                 {
-                    args = new TorrentStateChangedEventArgs(this.state, 
TorrentState.Hashing);
-                    this.state = TorrentState.Hashing;
-
-                    if (this.OnTorrentStateChanged != null)
-                        OnTorrentStateChanged(this, args);
-
+                    UpdateState(TorrentState.Hashing);
                     ThreadPool.QueueUserWorkItem(new WaitCallback(HashCheck), 
this);
                     return;
                 }
@@ -364,21 +359,9 @@
                 throw new TorrentException("Torrent is already running");
 
             if (this.Progress() == 100.0)
-            {
-                args = new TorrentStateChangedEventArgs(this.state, 
TorrentState.Seeding);
-                this.state = TorrentState.Seeding;
-
-                if (this.OnTorrentStateChanged != null)
-                    this.OnTorrentStateChanged(this, args);
-            }
-
+                UpdateState(TorrentState.Seeding);
             else
-            {
-                args = new TorrentStateChangedEventArgs(this.state, 
TorrentState.Downloading);
-                this.state = TorrentState.Downloading;
-                if (this.OnTorrentStateChanged != null)
-                    this.OnTorrentStateChanged(this, args);
-            }
+                UpdateState(TorrentState.Downloading);
 
             this.trackerManager.SendUpdate(0, 0, (long)((1.0 - this.Progress() 
/ 100.0) * this.torrent.Size), TorrentEvent.Started); // Tell server we're 
starting
         }
@@ -390,14 +373,10 @@
         internal WaitHandle Stop()
         {
             WaitHandle handle;
-            TorrentStateChangedEventArgs args;
 
-            args = new TorrentStateChangedEventArgs(this.state, 
TorrentState.Stopped);
-            this.state = TorrentState.Stopped;
-            if (this.OnTorrentStateChanged != null)
-                this.OnTorrentStateChanged(this, args);
-
+            UpdateState(TorrentState.Stopped);
             this.fileManager.FlushAll();
+
             handle = this.trackerManager.SendUpdate(this.dataBytesDownloaded, 
this.dataBytesUploaded, (long)((1.0 - this.Progress() / 100.0) * 
this.torrent.Size), TorrentEvent.Stopped);
             lock (this.listLock)
             {
@@ -411,9 +390,6 @@
             }
 
             this.SaveFastResume();
-
-            //this.downloadQueue.Clear();
-            //this.uploadQueue.Clear();
             this.connectedPeers = new Peers();
             this.available = new Peers();
             this.connectingTo = new Peers();
@@ -439,26 +415,16 @@
             TorrentStateChangedEventArgs args;
             lock (this.listLock)
             {
-                args = new TorrentStateChangedEventArgs(this.state, 
TorrentState.Paused);
-                this.state = TorrentState.Paused;
-                if (this.OnTorrentStateChanged != null)
-                    this.OnTorrentStateChanged(this, args);
+                UpdateState(TorrentState.Paused);
 
-#warning Is there a deadlock possibility here?
                 for (int i = 0; i < this.connectingTo.Count; i++)
                     lock (this.connectingTo[i])
                         
ClientEngine.ConnectionManager.CleanupSocket(this.connectingTo[i]);
-                
-                for(int i =0; i <this.connectedPeers.Count; i++)
+
+                for (int i = 0; i < this.connectedPeers.Count; i++)
                     lock (this.connectedPeers[i])
                         
ClientEngine.ConnectionManager.CleanupSocket(this.connectedPeers[i]);
 
-                //lock (this.listLock)
-                //    this.downloadQueue.Clear();
-
-                //lock (this.listLock)
-                //    this.uploadQueue.Clear();
-
                 this.SaveFastResume();
             }
         }
@@ -574,20 +540,25 @@
                     }
                 }
 
-                if(counter%100 ==0)
-                // If the last connection succeeded, then update at the 
regular interval
-                if (this.trackerManager.UpdateSucceeded)
+                if (counter % 100 == 0)
                 {
-                    if (DateTime.Now > 
(this.trackerManager.LastUpdated.AddSeconds(this.trackerManager.CurrentTracker.UpdateInterval)))
+                    // If the last connection succeeded, then update at the 
regular interval
+                    if (this.trackerManager.UpdateSucceeded)
                     {
+                        if (DateTime.Now > 
(this.trackerManager.LastUpdated.AddSeconds(this.trackerManager.CurrentTracker.UpdateInterval)))
+                        {
+                            
this.trackerManager.SendUpdate(this.dataBytesDownloaded, 
this.dataBytesUploaded, (long)((1.0 - this.Progress() / 100.0) * 
this.torrent.Size), TorrentEvent.None);
+                        }
+                    }
+                    // Otherwise update at the min interval
+                    else if (DateTime.Now > 
(this.trackerManager.LastUpdated.AddSeconds(this.trackerManager.CurrentTracker.MinUpdateInterval)))
+                    {
                         
this.trackerManager.SendUpdate(this.dataBytesDownloaded, 
this.dataBytesUploaded, (long)((1.0 - this.Progress() / 100.0) * 
this.torrent.Size), TorrentEvent.None);
                     }
                 }
-                // Otherwise update at the min interval
-                else if (DateTime.Now > 
(this.trackerManager.LastUpdated.AddSeconds(this.trackerManager.CurrentTracker.MinUpdateInterval)))
-                {
-                    this.trackerManager.SendUpdate(this.dataBytesDownloaded, 
this.dataBytesUploaded, (long)((1.0 - this.Progress() / 100.0) * 
this.torrent.Size), TorrentEvent.None);
-                }
+
+                if (this.Progress() == 100.0)
+                    UpdateState(TorrentState.Seeding);
             }
         }
 
@@ -615,6 +586,7 @@
             // list as a lock as already been acquired.
             foreach (PeerConnectionID id in this.connectedPeers)
             {
+#warning This shouldn't be necessary anymore
                 lock (this.listLock)
                 {
                     if (Monitor.TryEnter(id, 5))    // The peer who we 
recieved the piece off is already locked on
@@ -814,7 +786,17 @@
 
         #endregion
 
+        private void UpdateState(TorrentState newState)
+        {
+            if (this.state == newState)
+                return;
 
+            TorrentStateChangedEventArgs e = new 
TorrentStateChangedEventArgs(this.state, newState);
+            this.state = newState;
+
+            if (this.OnTorrentStateChanged != null)
+                this.OnTorrentStateChanged(this, e);
+        }
         #region Misc
         /// <summary>
         /// Returns the number of Seeds we are currently connected to

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

Reply via email to