Example #1
0
        void LoadPieces(BitField b)
        {
            int length = b.Length;

            for (int i = b.FirstTrue(0, length); i != -1; i = b.FirstTrue(i + 1, length))
            {
                if (!pieces.Exists(delegate(Piece p) { return(p.Index == i); }))
                {
                    pieces.Add(new Piece(i, TorrentData.PieceLength, TorrentData.Size));
                }
            }
        }
Example #2
0
        void LoadPieces(PeerId id, BitField b)
        {
            int length = b.Length;

            for (int i = b.FirstTrue(0, length); i != -1; i = b.FirstTrue(i + 1, length))
            {
                if (!pieces.Exists(delegate(Piece p) { return(p.Index == i); }))
                {
                    pieces.Add(new Piece(i, id.TorrentManager.Torrent.PieceLength, id.TorrentManager.Torrent.Size));
                }
            }
        }
Example #3
0
        private void LoadPieces(PeerId id, BitField b)
        {
            var length = b.Length;

            for (var i = b.FirstTrue(0, length); i != -1; i = b.FirstTrue(i + 1, length))
            {
                if (!_pieces.Exists(p => p.Index == i))
                {
                    _pieces.Add(new Piece(i, id.TorrentManager.Torrent.PieceLength, id.TorrentManager.Torrent.Size));
                }
            }
        }
Example #4
0
        public void FirstTrue2()
        {
            var b = new BitField(1025);

            b[1024] = true;
            Assert.AreEqual(1024, b.FirstTrue(0, 1025));
        }
Example #5
0
        public void FirstTrue_2()
        {
            BitField b = new BitField(1025);

            b[1024] = true;
            Assert.Equal(1024, b.FirstTrue(0, 1025));
        }
Example #6
0
 public void FirstTrue()
 {
     Assert.AreEqual(0, _bf.FirstTrue(0, _bf.Length));
     Assert.AreEqual(0, _bf.FirstTrue(0, 0));
     Assert.AreEqual(-1, _bf.FirstTrue(_bf.Length, _bf.Length));
     Assert.AreEqual(11, _bf.FirstTrue(_bf.Length - 1, _bf.Length - 1));
     Assert.AreEqual(11, _bf.FirstTrue(_bf.Length - 1, _bf.Length));
     Assert.AreEqual(11, _bf.FirstTrue(9, _bf.Length));
 }
Example #7
0
 public void FirstTrue()
 {
     Assert.Equal(0, bf.FirstTrue(0, bf.Length));
     Assert.Equal(0, bf.FirstTrue(0, 0));
     Assert.Equal(-1, bf.FirstTrue(bf.Length, bf.Length));
     Assert.Equal(11, bf.FirstTrue(bf.Length - 1, bf.Length - 1));
     Assert.Equal(11, bf.FirstTrue(bf.Length - 1, bf.Length));
     Assert.Equal(11, bf.FirstTrue(9, bf.Length));
 }
Example #8
0
        private int CanRequest(BitField bitfield, int pieceStartIndex, int pieceEndIndex, ref int pieceCount)
        {
            int largestStart = 0;
            int largestEnd   = 0;

            while ((pieceStartIndex = bitfield.FirstTrue(pieceStartIndex, pieceEndIndex)) != -1)
            {
                int end = bitfield.FirstFalse(pieceStartIndex, pieceEndIndex);
                if (end == -1)
                {
                    end = Math.Min(pieceStartIndex + pieceCount, bitfield.Length);
                }

                for (int i = pieceStartIndex; i < end; i++)
                {
                    if (AlreadyRequested(i))
                    {
                        end = i;
                    }
                }

                if ((end - pieceStartIndex) >= pieceCount)
                {
                    return(pieceStartIndex);
                }

                if ((largestEnd - largestStart) < (end - pieceStartIndex))
                {
                    largestStart = pieceStartIndex;
                    largestEnd   = end;
                }

                pieceStartIndex = Math.Max(pieceStartIndex + 1, end);
            }

            pieceCount = largestEnd - largestStart;
            return(pieceCount == 0 ? -1 : largestStart);
        }
Example #9
0
 public void FirstTrue()
 => BitField_L.FirstTrue();
Example #10
0
 void LoadPieces(PeerId id, BitField b)
 {
     int length = b.Length;
     for (int i = b.FirstTrue(0, length); i != -1; i = b.FirstTrue(i + 1, length))
         if (!pieces.Exists(delegate(Piece p) { return p.Index == i; }))
             pieces.Add(new Piece(i, id.TorrentManager.Torrent.PieceLength, id.TorrentManager.Torrent.Size));
 }
Example #11
0
        void TryAdvertisePiece(ChokeData data)
        {
            // If we are seeding to this peer and we have a peer waiting to unchoke
            // don't advertise more data
            if (!data.Peer.AmChoking && PendingUnchoke)
            {
                return;
            }

            int advertised = advertisedPieces.FindAll(p => p.Peer == data.Peer).Count;
            int max        = MaxAdvertised;

            if (Manager.UploadingTo < Manager.Settings.UploadSlots)
            {
                max = MaxAdvertised;
            }
            else if (data.ShareRatio < 0.25)
            {
                max = 1;
            }
            else if (data.ShareRatio < 0.35)
            {
                max = 2;
            }
            else if (data.ShareRatio < 0.50)
            {
                max = 3;
            }
            else
            {
                max = MaxAdvertised;
            }

            if (advertised >= max)
            {
                return;
            }

            // List of pieces *not* in the swarm
            temp.From(bitfield).Not();

            // List of pieces that he wants that aren't in the swarm
            temp.NAnd(data.Peer.BitField);

            // Ignore all the pieces we've already started sharing
            foreach (SeededPiece p in advertisedPieces)
            {
                temp[p.Index] = false;
            }

            int index = 0;

            while (advertised < max)
            {
                // Get the index of the first piece we can send him
                index = temp.FirstTrue(index, temp.Length);
                // Looks like he's not interested in us...
                if (index == -1)
                {
                    return;
                }

                advertised++;
                data.TotalPieces++;
                data.CurrentPieces[index] = true;
                advertisedPieces.Add(new SeededPiece(data.Peer, index, Manager.Torrent.PieceLength / Piece.BlockSize));
                data.Peer.Enqueue(new HaveMessage(index));
                index++;
            }
        }
Example #12
0
 public void FirstTrue_2()
 {
     BitField b = new BitField(1025);
     b[1024] = true;
     Assert.AreEqual(1024, b.FirstTrue(0, 1025));
 }
Example #13
0
        int CanRequest(BitField bitfield, int pieceStartIndex, int pieceEndIndex, ref int pieceCount)
        {
            // This is the easiest case to consider - special case it
            if (pieceCount == 1)
            {
                while (pieceStartIndex <= pieceEndIndex && (pieceStartIndex = bitfield.FirstTrue(pieceStartIndex, pieceEndIndex)) != -1)
                {
                    var end = bitfield.FirstFalse(pieceStartIndex, pieceEndIndex);

                    // If end is a valid value, it's the first *false* piece. Subtract '1' from it
                    // to give us the last available piece we can request. If it's -1 then we can use
                    // 'pieceEndIndex' as the last available piece to request as all pieces are available.
                    var lastAvailable = end == -1 ? pieceEndIndex : end - 1;
                    for (int i = pieceStartIndex; i <= lastAvailable; i++)
                    {
                        if (!AlreadyRequested(i))
                        {
                            return(i);
                        }
                    }
                    pieceStartIndex = lastAvailable + 1;
                }
                return(-1);
            }

            int largestStart = 0;
            int largestEnd   = 0;

            while (pieceStartIndex <= pieceEndIndex && (pieceStartIndex = bitfield.FirstTrue(pieceStartIndex, pieceEndIndex)) != -1)
            {
                int end = bitfield.FirstFalse(pieceStartIndex, pieceEndIndex);
                if (end == -1)
                {
                    end = Math.Min(pieceStartIndex + pieceCount, bitfield.Length);
                }

                // Do not include 'end' as it's the first *false* piece.
                for (int i = pieceStartIndex; i < end; i++)
                {
                    if (AlreadyRequested(i))
                    {
                        end = i;
                    }
                }

                if ((end - pieceStartIndex) >= pieceCount)
                {
                    return(pieceStartIndex);
                }

                if ((largestEnd - largestStart) < (end - pieceStartIndex))
                {
                    largestStart = pieceStartIndex;
                    largestEnd   = end;
                }

                pieceStartIndex = Math.Max(pieceStartIndex + 1, end);
            }

            pieceCount = largestEnd - largestStart;
            return(pieceCount == 0 ? -1 : largestStart);
        }
 private void LoadPieces(PeerId id, BitField b)
 {
     var length = b.Length;
     for (var i = b.FirstTrue(0, length); i != -1; i = b.FirstTrue(i + 1, length))
         if (!_pieces.Exists(p => p.Index == i))
             _pieces.Add(new Piece(i, id.TorrentManager.Torrent.PieceLength, id.TorrentManager.Torrent.Size));
 }