Ejemplo n.º 1
0
        public static BlockInfo?PickPiece(this IPiecePicker picker, IPeer peer, BitField available, IReadOnlyList <IPeer> otherPeers)
        {
            Span <BlockInfo> buffer = stackalloc BlockInfo[1];
            var result = picker.PickPiece(peer, available, otherPeers, 0, available.Length - 1, buffer);

            return(result == 1 ? (BlockInfo?)buffer[0] : null);
        }
Ejemplo n.º 2
0
        public static BlockInfo?PickPiece(this IPiecePicker picker, IPeer peer, BitField available)
        {
            Span <BlockInfo> buffer = stackalloc BlockInfo[1];
            var picked = picker.PickPiece(peer, available, Array.Empty <IPeer> (), 0, available.Length - 1, buffer);

            return(picked == 1 ? (BlockInfo?)buffer[0] : null);
        }
Ejemplo n.º 3
0
 public DownloadPiecesStage(IApplicationProtocol <PeerConnection> application,
                            IMainLoop mainLoop,
                            IPiecePicker piecePicker)
 {
     this.application = application;
     this.mainLoop    = mainLoop;
     this.piecePicker = piecePicker;
 }
Ejemplo n.º 4
0
        public static IPiecePicker Wrap(IPiecePicker picker, IEnumerable <BitField> ignoringBitfields)
        {
            var result = picker;

            foreach (var bf in ignoringBitfields)
            {
                result = new IgnoringPicker(bf, result);
            }
            return(result);
        }
        public void DoesntHaveSuggestedPiece()
        {
            peer.IsChoking        = false;
            peer.SupportsFastPeer = true;
            peer.SuggestedPieces.AddRange(new[] { 1, 2, 3, 4 });
            peer.BitField.SetAll(true);
            picker = new StandardPicker();
            picker.Initialise(torrentData);
            var bundle = picker.PickPiece(peer, new BitField(peer.BitField.Length), peers, 1, 0, peer.BitField.Length - 1);

            Assert.IsNull(bundle);
        }
Ejemplo n.º 6
0
 public NormalDownloader(IStorageWrapper storageWrapper, IPiecePicker piecePicker, int backLog, double maxRatePeriod,
                         int piecesNumber, Measure downloadMeasure, double snubTime, DataDelegate measureFunction)
 {
     this.storageWrapper  = storageWrapper;
     this.piecePicker     = piecePicker;
     this.backLog         = backLog;
     this.maxRatePeriod   = maxRatePeriod;
     this.downloadMeasure = downloadMeasure;
     this.piecesNumber    = piecesNumber;
     this.snubTime        = snubTime;
     this.measureFunction = measureFunction;
     this.downloads       = new List <SingleDownload>();
 }
Ejemplo n.º 7
0
        public void Initialise(ITorrentData torrentData, IReadOnlyList <BitField> ignoringBitfields)
        {
            TorrentData = torrentData;

            IPiecePicker picker = new StandardPicker();

            picker = new RandomisedPicker(picker);
            picker = new RarestFirstPicker(picker);
            picker = new PriorityPicker(picker);

            Picker = IgnoringPicker.Wrap(picker, ignoringBitfields);
            Picker.Initialise(torrentData);
        }
Ejemplo n.º 8
0
        public void DoesntHaveSuggestedPiece()
        {
            Span <BlockInfo> buffer = stackalloc BlockInfo[1];

            peer.IsChoking        = false;
            peer.SupportsFastPeer = true;
            peer.SuggestedPieces.AddRange(new[] { 1, 2, 3, 4 });
            peer.BitField.SetAll(true);
            picker = new StandardPicker();
            picker.Initialise(torrentData);
            var requested = picker.PickPiece(peer, new BitField(peer.BitField.Length), peers, 0, peer.BitField.Length - 1, buffer);

            Assert.AreEqual(0, requested);
        }
        public void Initialise(ITorrentData torrentData, IReadOnlyList <BitField> ignoringBitfields)
        {
            IgnorableBitfields = ignoringBitfields;
            TorrentData        = torrentData;

            Temp = new MutableBitField(TorrentData.PieceCount());

            IPiecePicker picker = new StandardPicker();

            picker = new RandomisedPicker(picker);
            picker = new RarestFirstPicker(picker);
            Picker = new PriorityPicker(picker);

            Picker.Initialise(torrentData);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 发送请求信息的队列
        /// </summary>
        /// <param name="pp">PiecePicker片断选择类</param>
        /// <returns>返回发送请求信息的队列</returns>
        private List <int> Pull(IPiecePicker pp)
        {
            request = new List <int>();
            while (true)
            {
                //调用PiecePicker选择片断
                int selectIndex = pp.Next(Want);

                //当selectIndex == -1时,表示已经没有排片断可供选择,所以结束选择
                if (selectIndex == -1)
                {
                    break;
                }

                //将所选择的片断索引号加入到request中
                request.Add(selectIndex);
            }
            return(request);
        }
        public void Setup()
        {
            int pieceCount  = 40;
            int pieceLength = 256 * 1024;

            bitfield    = new BitField(pieceCount);
            torrentData = new TestTorrentData {
                Files       = TorrentFileInfo.Create(pieceLength, ("File", pieceLength * pieceCount, "Full/Path/File")),
                PieceLength = pieceLength,
                Size        = pieceLength * pieceCount
            };
            peers = new List <PeerId> ();

            picker = new StandardPicker();
            picker.Initialise(torrentData);

            peer = PeerId.CreateNull(pieceCount);
            for (int i = 0; i < 20; i++)
            {
                PeerId p = PeerId.CreateNull(pieceCount);
                p.SupportsFastPeer = true;
                peers.Add(p);
            }
        }
Ejemplo n.º 12
0
 protected PiecePickerFilter(IPiecePicker picker)
 => Next = picker;
Ejemplo n.º 13
0
 public static BlockInfo?ContinueAnyExistingRequest(this IPiecePicker picker, IPeer peer, int startIndex, int endIndex)
 => picker.ContinueAnyExistingRequest(peer, startIndex, endIndex, 1);
Ejemplo n.º 14
0
 public static IList <BlockInfo> CancelRequests(this IPiecePicker picker, IPeer peer)
 {
     return(picker.CancelRequests(peer, 0, peer.BitField.Length - 1));
 }
Ejemplo n.º 15
0
 public RarestFirstPicker(IPiecePicker picker)
     : base(picker)
 {
     rarest = new Stack <MutableBitField> ();
     spares = new Stack <MutableBitField> ();
 }
Ejemplo n.º 16
0
 public PriorityPicker(IPiecePicker picker)
     : base(picker)
 {
     AllSamePriority = file => file.Priority == files[0].Priority;
 }
Ejemplo n.º 17
0
 public IgnoringPicker(BitField bitfield, IPiecePicker picker)
     : base(picker)
 {
     Bitfield = bitfield;
     Temp     = new BitField(bitfield.Length);
 }
Ejemplo n.º 18
0
 public DownloadPiecesStage(BitTorrentApplicationProtocol applicationProtocol, IMainLoop mainLoop, IPiecePicker piecePicker)
 {
     ApplicationProtocol = applicationProtocol;
     _mainLoop           = mainLoop;
     _piecePicker        = piecePicker;
 }
Ejemplo n.º 19
0
        public static BlockInfo?PickPiece(this IPiecePicker picker, IPeer peer, BitField available)
        {
            var result = picker.PickPiece(peer, available, Array.Empty <IPeer> (), 1, 0, available.Length - 1);

            return(result?.Single());
        }
Ejemplo n.º 20
0
 public static IList <BlockInfo> PickPiece(this IPiecePicker picker, IPeer peer, BitField available, IReadOnlyList <IPeer> otherPeers, int count)
 {
     return(picker.PickPiece(peer, available, otherPeers, count, 0, available.Length - 1));
 }
Ejemplo n.º 21
0
        public static BlockInfo?PickPiece(this IPiecePicker picker, IPeer peer, BitField available, IReadOnlyList <IPeer> otherPeers)
        {
            var result = picker.PickPiece(peer, available, otherPeers, 1, 0, available.Length - 1);

            return(result?.Single());
        }
Ejemplo n.º 22
0
 public static int PickPiece(this IPiecePicker picker, IPeer peer, BitField available, IReadOnlyList <IPeer> otherPeers, Span <BlockInfo> requests)
 {
     return(picker.PickPiece(peer, available, otherPeers, 0, available.Length - 1, requests));
 }
 public RandomisedPicker(IPiecePicker picker)
     : base(picker)
 {
     Random = new Random();
 }