Ejemplo n.º 1
0
        public override bool ValidatePiece(IPieceRequester peer, int pieceIndex, int startOffset, int length, out Piece piece)
        {
            Comparer.Index = pieceIndex;
            int pIndex = requests.BinarySearch(null, Comparer);

            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 || !peer.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);
            }
            peer.AmRequestingPiecesCount--;
            piece.Blocks[blockIndex].Received = true;

            if (piece.AllBlocksReceived)
            {
                requests.RemoveAt(pIndex);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public override bool ValidatePiece(IPieceRequester peer, int pieceIndex, int startOffset, int length, out Piece piece)
        {
            int pIndex = requests.BinarySearch(IndexComparer, pieceIndex);

            if (pIndex < 0)
            {
                piece = null;

                logger.InfoFormatted("Piece validation failed: {0} - {1}. No piece.", pieceIndex, startOffset);
                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 || !peer.Equals(piece.Blocks[blockIndex].RequestedOff))
            {
                logger.InfoFormatted("Piece validation failed: {0} - {1}. No block ", pieceIndex, startOffset);
                return(false);
            }
            if (piece.Blocks[blockIndex].Received)
            {
                logger.InfoFormatted("Piece validation failed: {0} - {1}. Already received.", pieceIndex, startOffset);
                return(false);
            }
            if (!piece.Blocks[blockIndex].Requested)
            {
                logger.InfoFormatted("Piece validation failed: {0} - {1}. Not requested.", pieceIndex, startOffset);
                return(false);
            }
            peer.AmRequestingPiecesCount--;
            piece.Blocks[blockIndex].Received = true;

            if (piece.AllBlocksReceived)
            {
                requests.RemoveAt(pIndex);
            }
            return(true);
        }