private const int BlockRequestSize = 16384; // 16 kB public IEnumerable <BlockRequest> BlocksToRequest(IReadOnlyList <Piece> incompletePieces, Bitfield availability, IReadOnlyCollection <PeerConnection> peers, IBlockRequests blockRequests) { var toRequest = new List <BlockRequest>(); int maxToRequest = MaxOutstandingRequests - blockRequests.RequestedBlocks.Count; foreach (var piece in incompletePieces) { if (toRequest.Count >= maxToRequest) { break; } if (!availability.IsPieceAvailable(piece.Index)) { continue; } var block = NextBlockForPiece(piece, blockRequests); if (block != null) { toRequest.Add(block); } } return(toRequest); }
/// <summary> /// Determines whether Bitfield a has pieces not available in Bitfield b. /// </summary> /// <param name="a">Bitfield to check for more pieces in.</param> /// <param name="b">Bitfield to compare against.</param> /// <returns>True if a has more pieces </returns> public static bool NotSubset(Bitfield a, Bitfield b) { for (int i = 0; i < a.Length; i++) { if (a.IsPieceAvailable(i) && !b.IsPieceAvailable(i)) { return(true); } } return(false); }