示例#1
0
        private bool TryEnableEndgame()
        {
            if (inEndgame)
            {
                return(false);
            }

            // We need to activate endgame mode when there are less than 20 requestable blocks
            // available. We OR the bitfields of all the files which are downloadable and then
            // NAND it with the torrents bitfield to get a list of pieces which remain to be downloaded.

            // Essentially we get a list of all the pieces we're allowed download, then get a list of
            // the pieces which we still need to get and AND them together.

            // Create the bitfield of pieces which are downloadable
            endgameSelector.SetAll(false);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Priority != Priority.DoNotDownload)
                {
                    endgameSelector.Or(files[i].GetSelector(bitfield.Length));
                }
            }

            // NAND it with the pieces we already have (i.e. AND it with the pieces we still need to receive)
            endgameSelector.NAnd(bitfield);

            // If the total number of blocks remaining is less than Threshold, activate Endgame mode.
            int          count  = 0;
            List <Piece> pieces = standard.ExportActiveRequests();

            for (int i = 0; i < pieces.Count; i++)
            {
                count += pieces[i].TotalReceived;
            }
            inEndgame = Math.Max(blocksPerPiece, (endgameSelector.TrueCount * blocksPerPiece)) - count < Threshold;
            if (inEndgame)
            {
                endgame.Initialise(bitfield, files, standard.ExportActiveRequests());
                // Set torrent's IsInEndGame flag
                torrentManager.isInEndGame = true;
            }
            return(inEndgame);
        }
示例#2
0
 public virtual void Setup()
 {
     // Yes, this is horrible. Deal with it.
     rig = TestRig.CreateMultiFile();
     peers = new List<PeerId>();
     picker = new IgnoringPicker(rig.Manager.Bitfield, new StandardPicker());
     picker.Initialise(rig.Manager.Bitfield, rig.Manager.Torrent.Files, new List<Piece>());
     peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < 20; i++)
     {
         PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager);
         p.SupportsFastPeer = true;
         peers.Add(p);
     }
 }
示例#3
0
        internal void ChangePicker(PiecePicker picker, BitField bitfield, TorrentFile[] files)
        {
            if (unhashedPieces.Length != bitfield.Length)
                unhashedPieces = new BitField(bitfield.Length);

            picker = new IgnoringPicker(bitfield, picker);
            picker = new IgnoringPicker(unhashedPieces, picker);
            IEnumerable<Piece> pieces = Picker == null ? new List<Piece>() : Picker.ExportActiveRequests();
            picker.Initialise(bitfield, files, pieces);
            this.picker = picker;
        }
示例#4
0
 public void DoesntHaveSuggestedPiece()
 {
     peer.IsChoking = false;
     peer.SupportsFastPeer = true;
     peer.SuggestedPieces.AddRange(new int[] { 1, 2, 3, 4 });
     peer.BitField.SetAll(true);
     picker = new StandardPicker();
     picker.Initialise(rig.Manager.Bitfield, rig.Torrent.Files, new List<Piece>());
     MessageBundle bundle = picker.PickPiece(peer, new Common.BitField(peer.BitField.Length), peers, 1, 0, peer.BitField.Length);
     Assert.IsNull(bundle);
 }
示例#5
0
 public virtual void Initialise(BitField bitfield, TorrentFile[] files, IEnumerable <Piece> requests)
 {
     CheckOverriden();
     picker.Initialise(bitfield, files, requests);
 }