Пример #1
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (_buffer.HasArray)
     {
         src.GetBytes(srcIndex, _buffer.InternalArray(), index, length);
     }
     else
     {
         src.GetBytes(srcIndex, this, index, length);
     }
     return(this);
 }
Пример #2
0
        public IByteBuf Write(IByteBuf buf, int length)
        {
            Ensure.IsNotNull(buf, $"Write(IByteBuf:{buf},length:{length}) buf不能为null");

            BlockCopy(buf.GetBytes(), buf.ReadIndex, length);

            return(this);
        }
Пример #3
0
        /// <summary>
        /// 摘要
        ///
        /// 将buf写入当前的Bytebuf缓冲区中
        ///
        ///
        /// 异常
        /// buf == null 抛出ArgumentNullException
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public IByteBuf Write(IByteBuf buf)
        {
            Ensure.IsNotNull(buf, $"Write(buf:{buf}) buf不能为空");

            //将buf的数据复制到当前的数据中
            BlockCopy(buf.GetBytes(), 0, buf.Readables());

            return(this);
        }
Пример #4
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (src.HasArray)
     {
         SetBytes(index, src.Array, src.ArrayOffset + srcIndex, length);
     }
     else
     {
         src.GetBytes(srcIndex, Array, index, length);
     }
     return(this);
 }
Пример #5
0
 public override IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length)
 {
     _buffer.GetBytes(index, destination, dstIndex, length);
     return(this);
 }
Пример #6
0
 public IByteBuf GetBytes(int index, IByteBuf destination)
 {
     _buf.GetBytes(index, destination);
     return(this);
 }
Пример #7
0
 public override IByteBuf GetBytes(int index, IByteBuf dst, int dstIndex, int length)
 {
     CheckIndex(index, length);
     _buffer.GetBytes(index + _adjustment, dst, dstIndex, length);
     return(this);
 }
Пример #8
0
        protected IByteBuf Decode(IConnection connection, IByteBuf input)
        {
            if (_discardingTooLongFrame)
            {
                var bytesToDiscard      = _bytesToDiscard;
                var localBytesToDiscard = (int)Math.Min(bytesToDiscard, input.ReadableBytes);
                input.SkipBytes(localBytesToDiscard);
                bytesToDiscard -= localBytesToDiscard;
                _bytesToDiscard = bytesToDiscard;
                FailIfNecessary(false);
            }

            if (input.ReadableBytes < _lengthFieldEndOffset)
            {
                return(null);
            }

            var actualLengthFieldOffset = input.ReaderIndex + _lengthFieldOffset;
            var frameLength             = GetUnadjustedFrameLength(input, actualLengthFieldOffset, _lengthFieldLength);

            if (frameLength < 0)
            {
                input.SkipBytes(_lengthFieldEndOffset);
                throw new CorruptedFrameException(string.Format("negative or zero pre-adjustment length field: " + frameLength));
            }

            frameLength += _lengthAdjustment + _lengthFieldEndOffset;

            if (frameLength < _lengthFieldEndOffset)
            {
                input.SkipBytes(_lengthFieldEndOffset);
                throw new CorruptedFrameException(string.Format("Adjusted frame length ({0}) is less than lengthFieldEndOffset: {1}", frameLength, _lengthFieldEndOffset));
            }

            if (frameLength > _maxFrameLength)
            {
                var discard = frameLength - input.ReadableBytes;
                _tooLongFrameLength = frameLength;

                if (discard < 0)
                {
                    // buffer contains more bytes than the frameLength so we can discard all now
                    input.SkipBytes((int)frameLength);
                    HeliosTrace.Instance.DecodeMalformedBytes((int)frameLength);
                }
                else
                {
                    //Enter discard mode and discard everything receive so far
                    _discardingTooLongFrame = true;
                    _bytesToDiscard         = discard;
                    HeliosTrace.Instance.DecodeMalformedBytes(input.ReadableBytes);
                    input.SkipBytes(input.ReadableBytes);
                }
                FailIfNecessary(true);
                return(null);
            }

            // never overflows because it's less than _maxFrameLength
            var frameLengthInt = (int)frameLength;

            if (input.ReadableBytes < frameLengthInt)
            {
                var unreadBytes = new byte[input.ReadableBytes];
                input.GetBytes(input.ReaderIndex, unreadBytes, 0, input.ReadableBytes);
                //need additional data from the network before we can finish decoding this message
                return(null);
            }

            if (_initialBytesToStrip > frameLengthInt)
            {
                input.SkipBytes(frameLengthInt);
                HeliosTrace.Instance.DecodeFailure();
                throw new CorruptedFrameException(string.Format("Adjusted frame lenght ({0}) is less than initialBytesToStrip: {1}", frameLength, _initialBytesToStrip));
            }
            input.SkipBytes(_initialBytesToStrip);

            //extract frame
            var readerIndex       = input.ReaderIndex;
            var actualFrameLength = frameLengthInt - _initialBytesToStrip;
            var frame             = ExtractFrame(connection, input, readerIndex, actualFrameLength);

            input.SetReaderIndex(readerIndex + actualFrameLength);
            return(frame);
        }
        protected IByteBuf Decode(IConnection connection, IByteBuf input)
        {
            if (_discardingTooLongFrame)
            {
                var bytesToDiscard = _bytesToDiscard;
                var localBytesToDiscard = (int) Math.Min(bytesToDiscard, input.ReadableBytes);
                input.SkipBytes(localBytesToDiscard);
                bytesToDiscard -= localBytesToDiscard;
                _bytesToDiscard = bytesToDiscard;
                FailIfNecessary(false);
            }

            if (input.ReadableBytes < _lengthFieldEndOffset) return null;

            var actualLengthFieldOffset = input.ReaderIndex + _lengthFieldOffset;
            var frameLength = GetUnadjustedFrameLength(input, actualLengthFieldOffset, _lengthFieldLength);

            if (frameLength < 0)
            {
                input.SkipBytes(_lengthFieldEndOffset);
                throw new CorruptedFrameException(string.Format("negative or zero pre-adjustment length field: " + frameLength));
            }

            frameLength += _lengthAdjustment + _lengthFieldEndOffset;

            if (frameLength < _lengthFieldEndOffset)
            {
                input.SkipBytes(_lengthFieldEndOffset);
                throw new CorruptedFrameException(string.Format("Adjusted frame length ({0}) is less than lengthFieldEndOffset: {1}", frameLength, _lengthFieldEndOffset));
            }

            if (frameLength > _maxFrameLength)
            {
                var discard = frameLength - input.ReadableBytes;
                _tooLongFrameLength = frameLength;

                if (discard < 0)
                {
                    // buffer contains more bytes than the frameLength so we can discard all now
                    input.SkipBytes((int) frameLength);
                    HeliosTrace.Instance.DecodeMalformedBytes((int)frameLength);
                }
                else
                {
                    //Enter discard mode and discard everything receive so far
                    _discardingTooLongFrame = true;
                    _bytesToDiscard = discard;
                    HeliosTrace.Instance.DecodeMalformedBytes(input.ReadableBytes);
                    input.SkipBytes(input.ReadableBytes);
                    
                }
                FailIfNecessary(true);
                return null;
            }

            // never overflows because it's less than _maxFrameLength
            var frameLengthInt = (int) frameLength;
            if (input.ReadableBytes < frameLengthInt)
            {
                
                var unreadBytes = new byte[input.ReadableBytes];
                input.GetBytes(input.ReaderIndex, unreadBytes, 0, input.ReadableBytes);
                //need additional data from the network before we can finish decoding this message
                return null;
            }

            if (_initialBytesToStrip > frameLengthInt)
            {
                input.SkipBytes(frameLengthInt);
                HeliosTrace.Instance.DecodeFailure();
                throw new CorruptedFrameException(string.Format("Adjusted frame lenght ({0}) is less than initialBytesToStrip: {1}", frameLength, _initialBytesToStrip));
            }
            input.SkipBytes(_initialBytesToStrip);

            //extract frame
            var readerIndex = input.ReaderIndex;
            var actualFrameLength = frameLengthInt - _initialBytesToStrip;
            var frame = ExtractFrame(connection, input, readerIndex, actualFrameLength);
            input.SetReaderIndex(readerIndex + actualFrameLength);
            return frame;
        }
Пример #10
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (src.HasArray)
     {
         SetBytes(index, src.Array, src.ArrayOffset + srcIndex, length);
     }
     else
     {
         src.GetBytes(srcIndex, Array, index, length);
     }
     return this;
 }
Пример #11
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (_buffer.HasArray)
     {
         src.GetBytes(srcIndex, _buffer.InternalArray(), index, length);
     }
     else
     {
         src.GetBytes(srcIndex, this, index, length);
     }
     return this;
 }