Author: alanmc
Date: 2008-02-19 16:56:46 -0500 (Tue, 19 Feb 2008)
New Revision: 96198

Modified:
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs
Log:
Don't expose the peers list publicly, allow the user to get a copy of it 
though. Fixes possible threading issue

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs  
2008-02-19 21:56:39 UTC (rev 96197)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs  
2008-02-19 21:56:46 UTC (rev 96198)
@@ -106,17 +106,7 @@
             get { return downloaded.Number; }
         }
 
-
         /// <summary>
-        /// The list of all peers being monitored by this manager
-        /// </summary>
-        public ICollection<Peer> Peers
-        {
-            get { return peers.Values; }
-        }
-
-
-        /// <summary>
         /// The torrent being tracked
         /// </summary>
         public ITrackable Trackable
@@ -162,10 +152,17 @@
 
             Debug.WriteLine(string.Format("Adding: {0}", peer.ClientAddress));
             peers.Add(peer.ClientAddress.Address, peer);
-            buffer.Clear();
+            lock (buffer)
+                buffer.Clear();
             UpdateCounts();
         }
 
+        public List<Peer> GetPeers()
+        {
+            lock (buffer)
+                return new List<Peer>(buffer);
+        }
+
         /// <summary>
         /// Retrieves a semi-random list of peers which can be used to fulfill 
an Announce request
         /// </summary>
@@ -188,8 +185,11 @@
 
             int start = random.Next(0, peers.Count);
 
-            if (buffer.Count != peers.Values.Count)
-                buffer = new List<Peer>(peers.Values);
+            lock (buffer)
+            {
+                if (buffer.Count != peers.Values.Count)
+                    buffer = new List<Peer>(peers.Values);
+            }
             List<Peer> p = buffer;
 
             while (total > 0)
@@ -214,11 +214,11 @@
 
         internal void ClearZombiePeers(DateTime cutoff)
         {
-            IPAddress[] keys = new IPAddress[peers.Keys.Count];
-            peers.Keys.CopyTo(keys, 0);
-            foreach (IPAddress ip in keys)
-                if (peers[ip].LastAnnounceTime < cutoff)
-                    peers.Remove(ip);
+            lock (buffer)
+            {
+                buffer.ForEach(delegate(Peer p) { if (p.LastAnnounceTime < 
cutoff) peers.Remove(p.ClientAddress.Address); });
+                buffer.Clear();
+            }
         }
 
 
@@ -228,12 +228,13 @@
         /// <param name="peer">The peer to remove</param>
         internal void Remove(Peer peer)
         {
-            if(peer == null)
+            if (peer == null)
                 throw new ArgumentNullException("peer");
 
             Debug.WriteLine(string.Format("Removing: {0}", 
peer.ClientAddress));
             peers.Remove(peer.ClientAddress.Address);
-            buffer.Clear();
+            lock (buffer)
+                buffer.Clear();
             UpdateCounts();
         }
 
@@ -242,7 +243,7 @@
             int complete = 0;
             int incomplete = 0;
 
-            foreach (Peer p in peers.Values)
+            foreach (Peer p in buffer)
             {
                 if (p.HasCompleted)
                     complete++;

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

Reply via email to