Esempio n. 1
0
        /// <exception cref="System.IO.IOException"/>
        private void ReadNextPacket()
        {
            //Read packet headers.
            packetReceiver.ReceiveNextPacket(@in);
            PacketHeader curHeader = packetReceiver.GetHeader();

            curDataSlice = packetReceiver.GetDataSlice();
            System.Diagnostics.Debug.Assert(curDataSlice.Capacity() == curHeader.GetDataLen()
                                            );
            if (Log.IsTraceEnabled())
            {
                Log.Trace("DFSClient readNextPacket got header " + curHeader);
            }
            // Sanity check the lengths
            if (!curHeader.SanityCheck(lastSeqNo))
            {
                throw new IOException("BlockReader: error in packet header " + curHeader);
            }
            if (curHeader.GetDataLen() > 0)
            {
                int chunks       = 1 + (curHeader.GetDataLen() - 1) / bytesPerChecksum;
                int checksumsLen = chunks * checksumSize;
                System.Diagnostics.Debug.Assert(packetReceiver.GetChecksumSlice().Capacity() == checksumsLen
                                                , "checksum slice capacity=" + packetReceiver.GetChecksumSlice().Capacity() + " checksumsLen="
                                                + checksumsLen);
                lastSeqNo = curHeader.GetSeqno();
                if (verifyChecksum && curDataSlice.Remaining() > 0)
                {
                    // N.B.: the checksum error offset reported here is actually
                    // relative to the start of the block, not the start of the file.
                    // This is slightly misleading, but preserves the behavior from
                    // the older BlockReader.
                    checksum.VerifyChunkedSums(curDataSlice, packetReceiver.GetChecksumSlice(), filename
                                               , curHeader.GetOffsetInBlock());
                }
                bytesNeededToFinish -= curHeader.GetDataLen();
            }
            // First packet will include some data prior to the first byte
            // the user requested. Skip it.
            if (curHeader.GetOffsetInBlock() < startOffset)
            {
                int newPos = (int)(startOffset - curHeader.GetOffsetInBlock());
                curDataSlice.Position(newPos);
            }
            // If we've now satisfied the whole client read, read one last packet
            // header, which should be empty
            if (bytesNeededToFinish <= 0)
            {
                ReadTrailingEmptyPacket();
                if (verifyChecksum)
                {
                    SendReadResult(DataTransferProtos.Status.ChecksumOk);
                }
                else
                {
                    SendReadResult(DataTransferProtos.Status.Success);
                }
            }
        }