internal void AddPieceRequests(PeerId id) { int maxRequests = id.MaxPendingRequests; if (id.AmRequestingPiecesCount >= maxRequests) { return; } int count = 1; if (id.Connection is HttpConnection) { if (id.AmRequestingPiecesCount > 0) { return; } // How many whole pieces fit into 2MB count = (2 * 1024 * 1024) / Manager.Torrent.PieceLength; // Make sure we have at least one whole piece count = Math.Max(count, 1); count *= Manager.Torrent.PieceLength / Piece.BlockSize; } if (!id.IsChoking || id.SupportsFastPeer) { while (id.AmRequestingPiecesCount < maxRequests) { PieceRequest request = Picker.ContinueExistingRequest(id); if (request != null) { id.Enqueue(new RequestMessage(request.PieceIndex, request.StartOffset, request.RequestLength)); } else { break; } } } if (!id.IsChoking || (id.SupportsFastPeer && id.IsAllowedFastPieces.Count > 0)) { while (id.AmRequestingPiecesCount < maxRequests) { List <PeerId> otherPeers = Manager.Peers.ConnectedPeers ?? new List <PeerId> (); IList <PieceRequest> request = Picker.PickPiece(id, id.BitField, otherPeers, count); if (request != null && request.Count > 0) { id.Enqueue(new RequestBundle(request)); } else { break; } } } }
internal void AddPieceRequests(PeerId id) { PeerMessage msg = null; int maxRequests = id.MaxPendingRequests; if (id.AmRequestingPiecesCount >= maxRequests) { return; } int count = 1; if (id.Connection is HttpConnection) { // How many whole pieces fit into 2MB count = (2 * 1024 * 1024) / id.TorrentManager.Torrent.PieceLength; // Make sure we have at least one whole piece count = Math.Max(count, 1); count *= id.TorrentManager.Torrent.PieceLength / Piece.BlockSize; } if (!id.IsChoking || id.SupportsFastPeer) { while (id.AmRequestingPiecesCount < maxRequests) { msg = Picker.ContinueExistingRequest(id); if (msg != null) { id.Enqueue(msg); } else { break; } } } if (!id.IsChoking || (id.SupportsFastPeer && id.IsAllowedFastPieces.Count > 0)) { while (id.AmRequestingPiecesCount < maxRequests) { msg = Picker.PickPiece(id, id.TorrentManager.Peers.ConnectedPeers, count); if (msg != null) { id.Enqueue(msg); } else { break; } } } }
void AddRequests(IPeerWithMessaging peer, IReadOnlyList <IPeerWithMessaging> allPeers, BitField bitfield, int startPieceIndex, int endPieceIndex, int maxDuplicates, int?preferredMaxRequests = null) { if (!peer.CanRequestMorePieces) { return; } int preferredRequestAmount = peer.PreferredRequestAmount(TorrentData.PieceLength); var maxRequests = Math.Min(preferredMaxRequests ?? 3, peer.MaxPendingRequests); if (peer.AmRequestingPiecesCount >= maxRequests) { return; } // FIXME: Add a test to ensure we do not unintentionally request blocks off peers which are choking us. // This used to say if (!peer.IsChoing || peer.SupportsFastPeer), and with the recent changes we might // not actually guarantee that 'ContinueExistingRequest' or 'ContinueAnyExistingRequest' properly takes // into account that a peer which is choking us can *only* resume a 'fast piece' in the 'AmAllowedfastPiece' list. if (!peer.IsChoking) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueAnyExistingRequest(peer, startPieceIndex, endPieceIndex, maxDuplicates); if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } // If the peer supports fast peer and they are choking us, they'll still send pieces in the allowed fast set. if (peer.SupportsFastPeer && peer.IsChoking) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueExistingRequest(peer, startPieceIndex, endPieceIndex); if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } // Should/could we simplify things for IPiecePicker implementations by guaranteeing IPiecePicker.PickPiece calls will // only be made to pieces which *can* be requested? Why not! // FIXME add a test for this. if (!peer.IsChoking || (peer.SupportsFastPeer && peer.IsAllowedFastPieces.Count > 0)) { while (peer.AmRequestingPiecesCount < maxRequests) { IList <BlockInfo> request = PriorityPick(peer, peer.BitField, allPeers, preferredRequestAmount, 0, TorrentData.PieceCount() - 1); if (request != null && request.Count > 0) { peer.EnqueueRequests(request); } else { break; } } } if (!peer.IsChoking && peer.AmRequestingPiecesCount == 0) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueAnyExistingRequest(peer, HighPriorityPieceIndex, bitfield.Length - 1, 1); // If this peer is a seeder and we are unable to request any new blocks, then we should enter // endgame mode. Every block has been requested at least once at this point. if (request == null && (InEndgameMode || peer.IsSeeder)) { request = Picker.ContinueAnyExistingRequest(peer, 0, TorrentData.PieceCount() - 1, 2); // FIXME: What if the picker is choosing to not allocate pieces? Then it's not endgame mode. // This should be deterministic, not a heuristic? InEndgameMode |= request != null && (bitfield.Length - bitfield.TrueCount) < 10; } if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } }
void AddRequests(IPeerWithMessaging peer, IReadOnlyList <IPeerWithMessaging> allPeers, int startPieceIndex, int endPieceIndex, int maxDuplicates, int?preferredMaxRequests = null) { if (!peer.CanRequestMorePieces) { return; } int preferredRequestAmount = peer.PreferredRequestAmount(TorrentData.PieceLength); var maxRequests = Math.Min(preferredMaxRequests ?? 3, peer.MaxPendingRequests); if (peer.AmRequestingPiecesCount >= maxRequests) { return; } // FIXME: Add a test to ensure we do not unintentionally request blocks off peers which are choking us. // This used to say if (!peer.IsChoing || peer.SupportsFastPeer), and with the recent changes we might // not actually guarantee that 'ContinueExistingRequest' or 'ContinueAnyExistingRequest' properly takes // into account that a peer which is choking us can *only* resume a 'fast piece' in the 'AmAllowedfastPiece' list. if (!peer.IsChoking) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueAnyExistingRequest(peer, startPieceIndex, endPieceIndex, maxDuplicates); if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } // If the peer supports fast peer and they are choking us, they'll still send pieces in the allowed fast set. if (peer.SupportsFastPeer && peer.IsChoking) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueExistingRequest(peer, startPieceIndex, endPieceIndex); if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } // Should/could we simplify things for IPiecePicker implementations by guaranteeing IPiecePicker.PickPiece calls will // only be made to pieces which *can* be requested? Why not! // FIXME add a test for this. if (!peer.IsChoking || (peer.SupportsFastPeer && peer.IsAllowedFastPieces.Count > 0)) { MutableBitField filtered = null; while (peer.AmRequestingPiecesCount < maxRequests) { filtered ??= GenerateAlreadyHaves().Not().And(peer.BitField); IList <BlockInfo> request = PriorityPick(peer, filtered, allPeers, preferredRequestAmount, 0, TorrentData.PieceCount() - 1); if (request != null && request.Count > 0) { peer.EnqueueRequests(request); } else { break; } } } if (!peer.IsChoking && peer.AmRequestingPiecesCount == 0) { while (peer.AmRequestingPiecesCount < maxRequests) { BlockInfo?request = Picker.ContinueAnyExistingRequest(peer, HighPriorityPieceIndex, TorrentData.PieceCount() - 1, 1); if (request != null) { peer.EnqueueRequest(request.Value); } else { break; } } } }