ReadNextHeader() public method

Read packet header
public ReadNextHeader ( ) : bool
return bool
示例#1
0
        /// <summary>
        /// Inflates the stream of data in this message
        /// </summary>
        /// <returns></returns>
        private bool _InflateDataStream(TDSStream stream)
        {
            // Indicates that end-of-message marker was reached
            bool isEndOfMessageReached = false;

            // Continue inflating packets as long as there's data in the stream or we've not reached the end of the message
            while (!isEndOfMessageReached)
            {
                // Chunk of data still available in the current packet
                int packetDataLeft = 0;

                // Check if we have a packet header
                if (stream.IncomingPacketHeader != null)
                {
                    // Calculate the chunk of remaining data
                    packetDataLeft = stream.IncomingPacketHeader.Length - stream.IncomingPacketPosition;
                }

                // Check if we have data to work with
                if (packetDataLeft == 0)
                {
                    // Position stream on the next packet header and read it
                    if (!stream.ReadNextHeader())
                    {
                        // We couldn't inflate the next packet header
                        break;
                    }

                    // Record packet status
                    PacketStatuses.Add(stream.IncomingPacketHeader.Status);

                    // Use the packet header to establish message type
                    MessageType = stream.IncomingPacketHeader.Type;

                    // Recalculate the chunk of remaining data
                    packetDataLeft = stream.IncomingPacketHeader.Length - stream.IncomingPacketPosition;
                }

                // Allocate a buffer to read the data into the buffer
                byte[] packetDataBuffer = new byte[packetDataLeft];

                // Read the data into the buffer
                int packetDataRead = stream.Read(packetDataBuffer, 0, packetDataLeft);

                // Check if we have a data stream
                if (_dataStream == null)
                {
                    // Allocate a new data stream
                    _dataStream = new MemoryStream();
                }

                // Write the data into the message stream
                _dataStream.Write(packetDataBuffer, 0, packetDataRead);

                // Check if the whole packet was read
                if (packetDataRead < packetDataLeft)
                {
                    // We don't have enough data to inflate the whole message
                    break;
                }

                // Check if inflation succeded
                isEndOfMessageReached = (stream.IncomingPacketHeader.Status & TDSPacketStatus.EndOfMessage) != 0;
            }

            // Tell caller whether we reached the end of message
            return(isEndOfMessageReached);
        }