Пример #1
0
        public void MultipleAnnounce()
        {
            var announceCount = 0;
            var random = new Random();
            var handle = new ManualResetEvent(false);

            for (var i = 0; i < 20; i++)
            {
                var infoHash = new InfoHash(new byte[20]);
                random.NextBytes(infoHash.Hash);
                var tier = new TrackerTier(new[] {_uri.ToString()});
                tier.Trackers[0].AnnounceComplete += (sender, args) =>
                                                         {
                                                             if (++announceCount == 20)
                                                                 handle.Set();
                                                         };
                var id = new TrackerConnectionID(tier.Trackers[0], false, TorrentEvent.Started,
                                                 new ManualResetEvent(false));
                var parameters = new AnnounceParameters(0, 0, 0, TorrentEvent.Started,
                                                        infoHash, false, new string('1', 20),
                                                        string.Empty, 1411);
                tier.Trackers[0].Announce(parameters, id);
            }

            Assert.IsTrue(handle.WaitOne(5000, true), "Some of the responses weren't received");
        }
        protected TrackerResponseEventArgs(Tracker tracker, object state, bool successful)
        {
            if (tracker == null)
                throw new ArgumentNullException("tracker");
            var trackerConnectionID = state as TrackerConnectionID;
            if (trackerConnectionID == null)
                throw new ArgumentException("The state object must be the same object as in the call to Announce", "state");

            _id = trackerConnectionID;
            Successful = successful;
            Tracker = tracker;
        }
Пример #3
0
        private WaitHandle Announce(Tracker tracker, TorrentEvent clientEvent, bool trySubsequent,
                                    ManualResetEvent waitHandle)
        {
            var engine = _manager.Engine;

            // If the engine is null, we have been unregistered
            if (engine == null)
            {
                waitHandle.Set();
                return(waitHandle);
            }

            _updateSucceeded = true;
            _lastUpdated     = DateTime.Now;

            var e = engine.Settings.AllowedEncryption;
            var requireEncryption  = !Toolbox.HasEncryption(e, EncryptionTypes.PlainText);
            var supportsEncryption = Toolbox.HasEncryption(e, EncryptionTypes.RC4Full) ||
                                     Toolbox.HasEncryption(e, EncryptionTypes.RC4Header);

            requireEncryption  = requireEncryption && ClientEngine.SupportsEncryption;
            supportsEncryption = supportsEncryption && ClientEngine.SupportsEncryption;

            var reportedAddress = engine.Settings.ReportedAddress;
            var ip   = reportedAddress == null ? null : reportedAddress.Address.ToString();
            var port = reportedAddress == null ? engine.Listener.Endpoint.Port : reportedAddress.Port;

            // FIXME: In metadata mode we need to pretend we need to download data otherwise
            // tracker optimisations might result in no peers being sent back.
            long bytesLeft = 1000;

            if (_manager.HasMetadata)
            {
                bytesLeft = (long)((1 - _manager.Bitfield.PercentComplete / 100.0) * _manager.Torrent.Size);
            }
            var parameters = new AnnounceParameters(_manager.Monitor.DataBytesDownloaded,
                                                    _manager.Monitor.DataBytesUploaded,
                                                    bytesLeft,
                                                    clientEvent, _infoHash, requireEncryption, _manager.Engine.PeerId,
                                                    ip, port)
            {
                SupportsEncryption = supportsEncryption
            };
            var id = new TrackerConnectionID(tracker, trySubsequent, clientEvent, waitHandle);

            tracker.Announce(parameters, id);
            return(waitHandle);
        }
Пример #4
0
        private WaitHandle Scrape(Tracker tracker, bool trySubsequent)
        {
            if (tracker == null)
            {
                throw new ArgumentNullException("tracker");
            }

            if (!tracker.CanScrape)
            {
                throw new TorrentException("This tracker does not support scraping");
            }

            var id = new TrackerConnectionID(tracker, trySubsequent, TorrentEvent.None, new ManualResetEvent(false));

            tracker.Scrape(new ScrapeParameters(_infoHash), id);
            return(id.WaitHandle);
        }
Пример #5
0
        protected TrackerResponseEventArgs(Tracker tracker, object state, bool successful)
        {
            if (tracker == null)
            {
                throw new ArgumentNullException("tracker");
            }
            var trackerConnectionID = state as TrackerConnectionID;

            if (trackerConnectionID == null)
            {
                throw new ArgumentException("The state object must be the same object as in the call to Announce", "state");
            }

            _id        = trackerConnectionID;
            Successful = successful;
            Tracker    = tracker;
        }
Пример #6
0
        public void AnnounceTest()
        {
            var t = (HTTPTracker) TrackerFactory.Create(new Uri(Prefix));
            var id = new TrackerConnectionID(t, false, TorrentEvent.Started, new ManualResetEvent(false));

            AnnounceResponseEventArgs p = null;
            t.AnnounceComplete += (o, e) =>
                                      {
                                          p = e;
                                          id.WaitHandle.Set();
                                      };
            var pars = new AnnounceParameters
                           {
                               PeerId = "id",
                               InfoHash = new InfoHash(new byte[20])
                           };

            t.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#1");
            Assert.IsTrue(p.Successful);
            Assert.AreEqual(_keys[0], t.Key, "#2");
        }
Пример #7
0
 public void AddPeer(Peer p)
 {
     TrackerConnectionID id = new TrackerConnectionID(this, false, TorrentEvent.None, null);
     AnnounceResponseEventArgs e = new AnnounceResponseEventArgs(this, null, true);
     e.Peers.Add(p);
     e.Successful = true;
     RaiseAnnounceComplete(e);
 }
Пример #8
0
 public void AddPeer(Peer p)
 {
     TrackerConnectionID id = new TrackerConnectionID(this, false, TorrentEvent.None, new ManualResetEvent(false));
     AnnounceResponseEventArgs e = new AnnounceResponseEventArgs(this, id, true);
     e.Peers.Add(p);
     RaiseAnnounceComplete(e);
     Assert.IsTrue(id.WaitHandle.WaitOne(1000, true), "#1 Tracker never raised the AnnounceComplete event");
 }
Пример #9
0
        private void OfflineScrapeTest()
        {
            var udpTracker = (UdpTracker) TrackerFactory.Create(new Uri("udp://127.0.0.1:57532/announce"));
            udpTracker.RetryDelay = TimeSpan.FromMilliseconds(500);
            var id = new TrackerConnectionID(udpTracker, false, TorrentEvent.Started, new ManualResetEvent(false));

            ScrapeResponseEventArgs p = null;
            udpTracker.ScrapeComplete += (o, e) =>
                                             {
                                                 if (e.Successful)
                                                     Console.ReadLine();
                                                 p = e;
                                                 id.WaitHandle.Set();
                                             };
            var pars = new ScrapeParameters(new InfoHash(new byte[20]));

            udpTracker.Scrape(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#1");
            Assert.IsFalse(p.Successful);
        }
Пример #10
0
        private void OfflineAnnounceTest()
        {
            var udpTracker = (UdpTracker) TrackerFactory.Create(new Uri("udp://127.0.0.1:57532/announce"));
            udpTracker.RetryDelay = TimeSpan.FromMilliseconds(500);
            var id = new TrackerConnectionID(udpTracker, false, TorrentEvent.Started, new ManualResetEvent(false));

            AnnounceResponseEventArgs p = null;
            udpTracker.AnnounceComplete += delegate(object o, AnnounceResponseEventArgs e)
                                      {
                                          p = e;
                                          id.WaitHandle.Set();
                                      };
            var pars = new AnnounceParameters
                           {
                               InfoHash = new InfoHash(new byte[20]),
                               PeerId = ""
                           };

            udpTracker.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#1");
            Assert.IsFalse(p.Successful);
        }
Пример #11
0
        public void ScrapeTest()
        {
            var udpTracker = (UdpTracker) TrackerFactory.Create(new Uri(Prefix));
            Assert.IsTrue(udpTracker.CanScrape, "#1");

            var id = new TrackerConnectionID(udpTracker, false, TorrentEvent.Started, new ManualResetEvent(false));

            ScrapeResponseEventArgs p = null;
            udpTracker.ScrapeComplete += delegate(object o, ScrapeResponseEventArgs e)
                                             {
                                                 p = e;
                                                 id.WaitHandle.Set();
                                             };
            var pars = new ScrapeParameters(new InfoHash(new byte[20]));

            udpTracker.Scrape(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#2");
            Assert.IsTrue(p.Successful, "#3");
            Assert.AreEqual(0, udpTracker.Complete, "#1");
            Assert.AreEqual(0, udpTracker.Incomplete, "#2");
            Assert.AreEqual(0, udpTracker.Downloaded, "#3");
        }
Пример #12
0
        public void AnnounceTest()
        {
            var udpTracker = (UdpTracker) TrackerFactory.Create(new Uri(Prefix));
            var id = new TrackerConnectionID(udpTracker, false, TorrentEvent.Started, new ManualResetEvent(false));

            AnnounceResponseEventArgs p = null;
            udpTracker.AnnounceComplete += delegate(object o, AnnounceResponseEventArgs e)
                                               {
                                                   p = e;
                                                   id.WaitHandle.Set();
                                               };
            var pars = new AnnounceParameters
                           {
                               InfoHash = new InfoHash(new byte[20]),
                               PeerId = ""
                           };

            udpTracker.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#1");
            Assert.IsTrue(p.Successful);
            //Assert.AreEqual(keys[0], t.Key, "#2");
        }
Пример #13
0
        public void KeyTest()
        {
            var pars = new AnnounceParameters
                           {
                               PeerId = "id",
                               InfoHash = new InfoHash(new byte[20])
                           };

            var t = TrackerFactory.Create(new Uri(Prefix + "?key=value"));
            var id = new TrackerConnectionID(t, false, TorrentEvent.Started, new ManualResetEvent(false));
            t.AnnounceComplete += delegate { id.WaitHandle.Set(); };
            t.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.AreEqual("value", _keys[0], "#1");
        }
Пример #14
0
        public void ScrapeTest()
        {
            var t = TrackerFactory.Create(new Uri(Prefix.Substring(0, Prefix.Length - 1)));
            Assert.IsTrue(t.CanScrape, "#1");
            var id = new TrackerConnectionID(t, false, TorrentEvent.Started, new ManualResetEvent(false));

            AnnounceResponseEventArgs p = null;
            t.AnnounceComplete += delegate(object o, AnnounceResponseEventArgs e)
                                      {
                                          p = e;
                                          id.WaitHandle.Set();
                                      };
            var pars = new AnnounceParameters {PeerId = "id", InfoHash = new InfoHash(new byte[20])};

            t.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#2");
            Assert.IsTrue(p.Successful, "#3");
            Assert.AreEqual(1, t.Complete, "#1");
            Assert.AreEqual(0, t.Incomplete, "#2");
            Assert.AreEqual(0, t.Downloaded, "#3");
        }