Beispiel #1
0
        internal int CheckCurrentStep(int readCount)
        {
            _messageBytesReadLength += readCount;
            _bufferStartIndex       += readCount;

            if (_currentFrameHeaderData == null)
            {
                if (_messageBytesReadLength < FrameFormat.FrameHeaderSize)
                {
                    return(0);
                }

                if (FrameFormat.CheckFrameHeader(_readBuffer) == false)
                {
                    return(-1);
                }

                var frameHeaderData = _tempFrameHeaderData;
                FrameFormat.ReadDataFromHeaderBuffer(_readBuffer, ref frameHeaderData);

                _headerExtentionLength = frameHeaderData.HeaderExtentionLength;
                _titleLength           = frameHeaderData.TitleLength;
                _contentLength         = frameHeaderData.ContentLength;
                _stateCode             = frameHeaderData.StateCode;
                _messageId             = frameHeaderData.MessageId;

                _currentFrameHeaderData = frameHeaderData;

                _headerExtentionBytes = _headerExtentionLength == 0 ? FrameFormat.EmptyBytes : new byte[_headerExtentionLength];
                _titleBytes           = _titleLength == 0 ? FrameFormat.EmptyBytes : new byte[_titleLength];
                _contentBytes         = _contentLength == 0 ? FrameFormat.EmptyBytes : new byte[_contentLength];

                if (GetBodyLength() == 0)
                {
                    return(_messageBytesReadLength == FrameFormat.FrameHeaderSize ? 1 : -1);
                }

                readCount = readCount - FrameFormat.FrameHeaderSize;
            }

            _unprocessCount += readCount;

            var messageLength = GetBodyLength() + FrameFormat.FrameHeaderSize;

            // message format error,return error code
            if (_messageBytesReadLength > messageLength)
            {
                return(-1);
            }

            // read datas and return success code
            if (_messageBytesReadLength == messageLength)
            {
                ReadBufferDatas(_unprocessCount);
                return(1);
            }

            // message is not complete,return continue-reading code
            // if buffer is full,move datas to cache and clear buffer
            if (_bufferStartIndex == _readBuffer.Length)
            {
                ReadBufferDatas(_unprocessCount);
                _unprocessCount   = 0;
                _bufferStartIndex = 0;
            }

            //continue to read
            return(0);
        }
        internal void CheckCurrentReceive(int readCount)
        {
            if (_step == 0)
            {
                _currentStepReadCount += readCount;

                if (FrameFormat.CheckFrameHeader(_currentStepReadBuffer, _currentStepReadCount) == false)
                {
                    throw new Exception("step:0, CheckHeader error");
                }

                var frameHeaderData = _tempFrameHeaderData;
                FrameFormat.ReadDataFromHeaderBuffer(_currentStepReadBuffer, ref frameHeaderData);

                _titleLength   = frameHeaderData.TitleLength;
                _contentLength = frameHeaderData.ContentLength;
                _stateCode     = frameHeaderData.StateCode;
                _messageId     = frameHeaderData.MessageId;

                if (_titleLength + _contentLength == 0)
                {
                    _title        = string.Empty;
                    _contentBytes = FrameFormat.EmptyBytes;

                    StepTo(2);
                    return;
                }

                StepTo(1);
                return;
            }

            if (_step == 1)
            {
                _currentStepReadCount += readCount;

                if (_currentStepReadCount < _titleLength + _contentLength)
                {
                    return;
                }

                if (_currentStepReadCount != _titleLength + _contentLength)
                {
                    throw new Exception("message body length error");
                }

                _title = string.Empty;

                if (_titleLength > 0)
                {
                    _title = FrameFormat.GetTitle(_currentStepReadBuffer, 0, _titleLength);
                }

                _contentBytes = new byte[_contentLength];

                if (_contentLength > 0)
                {
                    Array.Copy(_currentStepReadBuffer, _titleLength, _contentBytes, 0, _contentLength);
                }

                StepTo(2);
                return;
            }

            if (_step == 2)
            {
                throw new Exception();
            }
        }