Exemple #1
0
        /// <summary>
        /// Copies data from the stream to the buffer.
        /// If the stream does not contain specified number of bytes, an exception will be thrown.
        /// </summary>
        /// <param name="inputStream">The source stream to copy data from.</param>
        /// <param name="buffer">The buffer to copy data from.</param>
        /// <param name="offset">The zero-based byte offset in a buffer at which to begin storing the data read from the current stream.</param>
        /// <param name="sizeToRead">The size of the chunk being copied.</param>
        public static void ReadDataFromStream(Stream inputStream, byte[] buffer, int offset, int sizeToRead)
        {
            if (sizeToRead <= 0)
            {
                return;
            }

            int readPortion = 0;
            int readSize    = inputStream.Read(buffer, offset, sizeToRead);

            for ( ; readSize < sizeToRead; readSize += readPortion)
            {
                readPortion = inputStream.Read(buffer, offset + readSize, sizeToRead - readSize);
                if (readPortion <= 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }
            }
        }
        /// <summary>
        /// Synchronously reads the next network packet if it is available.
        /// </summary>
        /// <param name="deriveHeader">Indicates whether it is necessary to take header from the provided connection.</param>
        private void ReadNextPortion(bool deriveHeader)
        {
            int bytesRead    = 0;
            int lengthToRead = 0;

            if (!deriveHeader)
            {
                // try to read the remaining part of the packet
                if (this._currentPacketBytesRead < this._currentPacketSize)
                {
                    // read the next part of the packet
                    lengthToRead = Math.Min(this._currentPacketSize - this._currentPacketBytesRead, this._readBuffer.Length);

                    this._validLength = this.ReadFromSocket(this._readBuffer, 0, lengthToRead);
                    if (this._validLength == 0)
                    {
                        throw GenuineExceptions.Get_Receive_Portion();
                    }

                    // Fixed in 2.5.9.7
                    //this._tcpSocketInfo.ITransportContext.ConnectionManager.IncreaseBytesReceived(this._validLength);
                    this._currentPacketBytesRead += this._validLength;
                    this._currentPosition         = 0;

                    if (this._currentPacketBytesRead == this._currentPacketSize && this._messageRead)
                    {
                        this.ReadingCompleted();
                    }
                    return;
                }

                // the underlying stream ends
                if (this._messageRead)
                {
                    return;
                }

                // prepare for reading a header
                this._currentPosition = 0;
            }

            lengthToRead = TcpConnectionManager.HEADER_SIZE;

            if (deriveHeader)
            {
                if (this._tcpSocketInfo.ReceivingBufferCurrentPosition > 0)
                {
                    Buffer.BlockCopy(this._tcpSocketInfo.ReceivingHeaderBuffer, 0, this._readBuffer, 0, this._tcpSocketInfo.ReceivingBufferCurrentPosition);
                }
                this._currentPosition = this._tcpSocketInfo.ReceivingBufferCurrentPosition;
            }

            // read the header
            while (this._currentPosition < lengthToRead)
            {
                bytesRead = this.ReadFromSocket(this._readBuffer, this._currentPosition, lengthToRead - this._currentPosition);

                if (bytesRead == 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }

                // Fixed in 2.5.9.7
                //this._tcpSocketInfo.ITransportContext.ConnectionManager.IncreaseBytesReceived(bytesRead);
                this._currentPosition += bytesRead;
            }

            // parse the header
            if (this._readBuffer[0] != MessageCoder.COMMAND_MAGIC_CODE)
            {
                throw GenuineExceptions.Get_Receive_IncorrectData();
            }
            this._currentPacketSize      = MessageCoder.ReadInt32(this._readBuffer, 1);
            this._messageRead            = this._readBuffer[5] != 0;
            this._currentPacketBytesRead = 0;

            // and read the part of the packet
            if (this._currentPacketBytesRead < this._currentPacketSize)
            {
                // read the next part of the packet
                lengthToRead = Math.Min(this._currentPacketSize - this._currentPacketBytesRead, this._readBuffer.Length);

                this._validLength = this.ReadFromSocket(this._readBuffer, 0, lengthToRead);
                if (this._validLength == 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }

                // Fixed in 2.5.9.7
                //this._tcpSocketInfo.ITransportContext.ConnectionManager.IncreaseBytesReceived(this._validLength);
                this._currentPacketBytesRead += this._validLength;
                this._currentPosition         = 0;
            }

            if (this._currentPacketBytesRead == this._currentPacketSize && this._messageRead)
            {
                this.ReadingCompleted();
            }
        }
Exemple #3
0
        /// <summary>
        /// Synchronously reads the next network packet if it is available.
        /// </summary>
        private void ReadNextPortion()
        {
            int bytesRead    = 0;
            int lengthToRead = 0;

            // try to read the remaining part of the packet
            if (this._currentPacketBytesRead < this._currentPacketSize)
            {
                // read the next part of the packet
                lengthToRead = Math.Min(this._currentPacketSize - this._currentPacketBytesRead, this._readBuffer.Length);

                this._validLength = this._underlyingStream.Read(this._readBuffer, 0, lengthToRead);
                if (this._validLength == 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }

                this.ITransportContext.ConnectionManager.IncreaseBytesReceived(this._validLength);
                this._currentPacketBytesRead += this._validLength;
                this._currentPosition         = 0;
                return;
            }

            // the underlying stream ends
            if (this._messageRead)
            {
                return;
            }

            // prepare for reading a header
            lengthToRead          = MessageCoder.LABEL_HEADER_SIZE;
            this._currentPosition = 0;

            // read the header
            while (this._currentPosition < lengthToRead)
            {
                bytesRead = this._underlyingStream.Read(this._readBuffer, this._currentPosition, lengthToRead - this._currentPosition);

                if (bytesRead == 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }

                this.ITransportContext.ConnectionManager.IncreaseBytesReceived(bytesRead);
                this._currentPosition += bytesRead;
            }

            // parse the header
            if (this._readBuffer[0] != MessageCoder.COMMAND_MAGIC_CODE)
            {
                throw GenuineExceptions.Get_Receive_IncorrectData();
            }
            this._currentPacketSize      = MessageCoder.ReadInt32(this._readBuffer, 1);
            this._messageRead            = this._readBuffer[5] != 0;
            this._currentPacketBytesRead = 0;

            // and read the part of the packet
            if (this._currentPacketBytesRead < this._currentPacketSize)
            {
                // read the next part of the packet
                lengthToRead = Math.Min(this._currentPacketSize - this._currentPacketBytesRead, this._readBuffer.Length);

//				GenuineUtility.ReadDataFromStream(this._underlyingStream, this._readBuffer, 0, lengthToRead);
//				this._validLength = lengthToRead;

                this._validLength = this._underlyingStream.Read(this._readBuffer, 0, lengthToRead);
                if (this._validLength == 0)
                {
                    throw GenuineExceptions.Get_Receive_Portion();
                }

                this.ITransportContext.ConnectionManager.IncreaseBytesReceived(this._validLength);
                this._currentPacketBytesRead += this._validLength;
                this._currentPosition         = 0;
            }
        }