public override void CancelRequest(PeerId peer, int piece, int startOffset, int length) { CancelWhere(b => b.StartOffset == startOffset && b.RequestLength == length && b.PieceIndex == piece && peer.Equals(b.RequestedOff)); }
public override RequestMessage ContinueExistingRequest(PeerId id) { foreach (var piece in Requests) { // For each piece that was assigned to this peer, try to request a block from it // A piece is 'assigned' to a peer if he is the first person to request a block from that piece if (piece.AllBlocksRequested || !id.Equals(piece.Blocks[0].RequestedOff)) { continue; } for (int i = 0; i < piece.BlockCount; i++) { if (piece.Blocks[i].Requested || piece.Blocks[i].Received) { continue; } piece.Blocks[i].Requested = true; return(piece.Blocks[i].CreateRequest(id)); } } // If we get here it means all the blocks in the pieces being downloaded by the peer are already requested return(null); }
public override void CancelRequest(PeerId peer, int piece, int startOffset, int length) { CancelWhere(delegate(Request r) { return(r.Block.PieceIndex == piece && r.Block.StartOffset == startOffset && r.Block.RequestLength == length && peer.Equals(r.Peer)); }, false); }
public override bool ValidatePiece(PeerId id, int pieceIndex, int startOffset, int length, out Piece piece) { //Comparer.index = pieceIndex; var pIndex = Requests.BinarySearch(null, new BinaryIndexComparer(pieceIndex)); if (pIndex < 0) { piece = null; Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset); Logger.Log(null, "No piece"); return(false); } piece = Requests[pIndex]; // Pick out the block that this piece message belongs to int blockIndex = Block.IndexOf(piece.Blocks, startOffset, length); if (blockIndex == -1 || !id.Equals(piece.Blocks[blockIndex].RequestedOff)) { Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset); Logger.Log(null, "no block"); return(false); } if (piece.Blocks[blockIndex].Received) { Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset); Logger.Log(null, "received"); return(false); } if (!piece.Blocks[blockIndex].Requested) { Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset); Logger.Log(null, "not requested"); return(false); } id.AmRequestingPiecesCount--; piece.Blocks[blockIndex].Received = true; if (piece.AllBlocksReceived) { Requests.RemoveAt(pIndex); } return(true); }
public override void CancelRequests(PeerId peer) { CancelWhere(b => peer.Equals(b.RequestedOff)); }