当前位置: 首页>>代码示例>>C#>>正文


C# IByteBuffer.SkipBytes方法代码示例

本文整理汇总了C#中IByteBuffer.SkipBytes方法的典型用法代码示例。如果您正苦于以下问题:C# IByteBuffer.SkipBytes方法的具体用法?C# IByteBuffer.SkipBytes怎么用?C# IByteBuffer.SkipBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IByteBuffer的用法示例。


在下文中一共展示了IByteBuffer.SkipBytes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Decode

        /// <summary>
        ///     Create a frame out of the <see cref="IByteBuffer" /> and return it.
        /// </summary>
        /// <param name="context">
        ///     The <see cref="IChannelHandlerContext" /> which this <see cref="ByteToMessageDecoder" /> belongs
        ///     to.
        /// </param>
        /// <param name="input">The <see cref="IByteBuffer" /> from which to read data.</param>
        /// <returns>The <see cref="IByteBuffer" /> which represents the frame or <c>null</c> if no frame could be created.</returns>
        protected object Decode(IChannelHandlerContext context, IByteBuffer input)
        {
            if (this.discardingTooLongFrame)
            {
                long bytesToDiscard = this.bytesToDiscard;
                int localBytesToDiscard = (int)Math.Min(bytesToDiscard, input.ReadableBytes);
                input.SkipBytes(localBytesToDiscard);
                bytesToDiscard -= localBytesToDiscard;
                this.bytesToDiscard = bytesToDiscard;

                this.FailIfNecessary(false);
            }

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

            int actualLengthFieldOffset = input.ReaderIndex + this.lengthFieldOffset;
            long frameLength = this.GetUnadjustedFrameLength(input, actualLengthFieldOffset, this.lengthFieldLength, this.byteOrder);

            if (frameLength < 0)
            {
                input.SkipBytes(this.lengthFieldEndOffset);
                throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength);
            }

            frameLength += this.lengthAdjustment + this.lengthFieldEndOffset;

            if (frameLength < this.lengthFieldEndOffset)
            {
                input.SkipBytes(this.lengthFieldEndOffset);
                throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " +
                    "than lengthFieldEndOffset: " + this.lengthFieldEndOffset);
            }

            if (frameLength > this.maxFrameLength)
            {
                long discard = frameLength - input.ReadableBytes;
                this.tooLongFrameLength = frameLength;

                if (discard < 0)
                {
                    // buffer contains more bytes then the frameLength so we can discard all now
                    input.SkipBytes((int)frameLength);
                }
                else
                {
                    // Enter the discard mode and discard everything received so far.
                    this.discardingTooLongFrame = true;
                    this.bytesToDiscard = discard;
                    input.SkipBytes(input.ReadableBytes);
                }
                this.FailIfNecessary(true);
                return null;
            }

            // never overflows because it's less than maxFrameLength
            int frameLengthInt = (int)frameLength;
            if (input.ReadableBytes < frameLengthInt)
            {
                return null;
            }

            if (this.initialBytesToStrip > frameLengthInt)
            {
                input.SkipBytes(frameLengthInt);
                throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " +
                    "than initialBytesToStrip: " + this.initialBytesToStrip);
            }
            input.SkipBytes(this.initialBytesToStrip);

            // extract frame
            int readerIndex = input.ReaderIndex;
            int actualFrameLength = frameLengthInt - this.initialBytesToStrip;
            IByteBuffer frame = this.ExtractFrame(context, input, readerIndex, actualFrameLength);
            input.SetReaderIndex(readerIndex + actualFrameLength);
            return frame;
        }
开发者ID:RabbitTeam,项目名称:DotNetty,代码行数:88,代码来源:LengthFieldBasedFrameDecoder.cs

示例2: Decode

        /// <summary>Create a frame out of the <see cref="DotNetty.Buffers.IByteBuffer" /> and return it</summary>
        /// <param name="ctx">
        ///     the <see cref="DotNetty.Transport.Channels.IChannelHandlerContext" /> which this
        ///     <see cref="DotNetty.Codecs.ByteToMessageDecoder" /> belongs to
        /// </param>
        /// <param name="buffer">the <see cref="DotNetty.Buffers.IByteBuffer" /> from which to read data</param>
        /// <returns>
        ///     the <see cref="DotNetty.Buffers.IByteBuffer" /> which represent the frame or null if no frame could be
        ///     created.
        /// </returns>
        protected object Decode(IChannelHandlerContext ctx, IByteBuffer buffer)
        {
            if (this.lineBasedDecoder != null)
            {
                return this.lineBasedDecoder.Decode(ctx, buffer);
            }

            // Try all delimiters and choose the delimiter which yields the shortest frame.
            int minFrameLength = int.MaxValue;
            IByteBuffer minDelim = null;
            foreach (IByteBuffer delim in this.delimiters)
            {
                int frameLength = IndexOf(buffer, delim);
                if (frameLength >= 0 && frameLength < minFrameLength)
                {
                    minFrameLength = frameLength;
                    minDelim = delim;
                }
            }

            if (minDelim != null)
            {
                int minDelimLength = minDelim.Capacity;
                IByteBuffer frame;

                if (this.discardingTooLongFrame)
                {
                    // We've just finished discarding a very large frame.
                    // Go back to the initial state.
                    this.discardingTooLongFrame = false;
                    buffer.SkipBytes(minFrameLength + minDelimLength);

                    int tooLongFrameLength = this.tooLongFrameLength;
                    this.tooLongFrameLength = 0;
                    if (!this.failFast)
                    {
                        this.Fail(tooLongFrameLength);
                    }
                    return null;
                }

                if (minFrameLength > this.maxFrameLength)
                {
                    // Discard read frame.
                    buffer.SkipBytes(minFrameLength + minDelimLength);
                    this.Fail(minFrameLength);
                    return null;
                }

                if (this.stripDelimiter)
                {
                    frame = buffer.ReadSlice(minFrameLength);
                    buffer.SkipBytes(minDelimLength);
                }
                else
                {
                    frame = buffer.ReadSlice(minFrameLength + minDelimLength);
                }

                return frame.Retain();
            }
            else
            {
                if (!this.discardingTooLongFrame)
                {
                    if (buffer.ReadableBytes > this.maxFrameLength)
                    {
                        // Discard the content of the buffer until a delimiter is found.
                        this.tooLongFrameLength = buffer.ReadableBytes;
                        buffer.SkipBytes(buffer.ReadableBytes);
                        this.discardingTooLongFrame = true;
                        if (this.failFast)
                        {
                            this.Fail(this.tooLongFrameLength);
                        }
                    }
                }
                else
                {
                    // Still discarding the buffer since a delimiter is not found.
                    this.tooLongFrameLength += buffer.ReadableBytes;
                    buffer.SkipBytes(buffer.ReadableBytes);
                }
                return null;
            }
        }
开发者ID:RabbitTeam,项目名称:DotNetty,代码行数:96,代码来源:DelimiterBasedFrameDecoder.cs

示例3: Decode

        protected internal override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
        {
            if (this.state == StCorrupted)
            {
                input.SkipBytes(input.ReadableBytes);
                return;
            }

            // index of next byte to process.
            int idx = this.idx;
            int wrtIdx = input.WriterIndex;

            if (wrtIdx > this.maxObjectLength)
            {
                // buffer size exceeded maxObjectLength; discarding the complete buffer.
                input.SkipBytes(input.ReadableBytes);
                this.Reset();
                throw new TooLongFrameException($"Object length exceeds {this.maxObjectLength}: {wrtIdx} bytes discarded");
            }

            for ( /* use current idx */; idx < wrtIdx; idx++)
            {
                byte c = input.GetByte(idx);

                if (this.state == StDecodingNormal)
                {
                    this.DecodeByte(c, input, idx);

                    // All opening braces/brackets have been closed. That's enough to conclude
                    // that the JSON object/array is complete.
                    if (this.openBraces == 0)
                    {
                        IByteBuffer json = this.ExtractObject(context, input, input.ReaderIndex, idx + 1 - input.ReaderIndex);
                        if (json != null)
                        {
                            output.Add(json);
                        }

                        // The JSON object/array was extracted => discard the bytes from
                        // the input buffer.
                        input.SetReaderIndex(idx + 1);

                        // Reset the object state to get ready for the next JSON object/text
                        // coming along the byte stream.
                        this.Reset();
                    }
                }
                else if (this.state == StDecodingArrayStream)
                {
                    this.DecodeByte(c, input, idx);

                    if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']'))
                    {
                        // skip leading spaces. No range check is needed and the loop will terminate
                        // because the byte at position idx is not a whitespace.
                        for (int i = input.ReaderIndex; char.IsWhiteSpace(Convert.ToChar(input.GetByte(i))); i++)
                        {
                            input.SkipBytes(1);
                        }

                        // skip trailing spaces.
                        int idxNoSpaces = idx - 1;
                        while (idxNoSpaces >= input.ReaderIndex && char.IsWhiteSpace(Convert.ToChar(input.GetByte(idxNoSpaces))))
                        {
                            idxNoSpaces--;
                        }

                        IByteBuffer json = this.ExtractObject(context, input, input.ReaderIndex, idxNoSpaces + 1 - input.ReaderIndex);
                        if (json != null)
                        {
                            output.Add(json);
                        }

                        input.SetReaderIndex(idx + 1);

                        if (c == ']')
                        {
                            this.Reset();
                        }
                    }
                }
                else if (c == '{' || c == '[') // JSON object/array detected. Accumulate bytes until all braces/brackets are closed
                {
                    this.InitDecoding(c);

                    if (this.state == StDecodingArrayStream)
                    {
                        //Discard the array bracket
                        input.SkipBytes(1);
                    }
                }
                else if (char.IsWhiteSpace(Convert.ToChar(c))) //Discard leading spaces in from of a JSON object/array
                {
                    input.SkipBytes(1);
                }
                else
                {
                    this.state = StCorrupted;
                    throw new CorruptedFrameException($"Invalid JSON received at byte position {idx}");
                }
//.........这里部分代码省略.........
开发者ID:RabbitTeam,项目名称:DotNetty,代码行数:101,代码来源:JsonObjectDecoder.cs

示例4: SkipNullBytes

        /// <summary>
        /// Skips the null bytes.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
        private static bool SkipNullBytes(IByteBuffer input)
        {
            while (input.IsReadable())
            {
                if (input.GetByte(input.ReaderIndex) != 0xFF) return true;
                input.SkipBytes(1);
            }

            return false;
        }
开发者ID:bradsjm,项目名称:DotEmwin,代码行数:15,代码来源:ByteBlasterProtocolDecoder.cs

示例5: Decode

        /// <summary>
        ///     Create a frame out of the {@link ByteBuf} and return it.
        /// </summary>
        /// <param name="ctx">the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to</param>
        /// <param name="buffer">the {@link ByteBuf} from which to read data</param>
        protected internal object Decode(IChannelHandlerContext ctx, IByteBuffer buffer)
        {
            int eol = this.FindEndOfLine(buffer);
            if (!this.discarding)
            {
                if (eol >= 0)
                {
                    IByteBuffer frame;
                    int length = eol - buffer.ReaderIndex;
                    int delimLength = buffer.GetByte(eol) == '\r' ? 2 : 1;

                    if (length > this.maxLength)
                    {
                        buffer.SetReaderIndex(eol + delimLength);
                        this.Fail(ctx, length);
                        return null;
                    }

                    if (this.stripDelimiter)
                    {
                        frame = buffer.ReadSlice(length);
                        buffer.SkipBytes(delimLength);
                    }
                    else
                    {
                        frame = buffer.ReadSlice(length + delimLength);
                    }

                    return frame.Retain();
                }
                else
                {
                    int length = buffer.ReadableBytes;
                    if (length > this.maxLength)
                    {
                        this.discardedBytes = length;
                        buffer.SetReaderIndex(buffer.WriterIndex);
                        this.discarding = true;
                        if (this.failFast)
                        {
                            this.Fail(ctx, "over " + this.discardedBytes);
                        }
                    }
                    return null;
                }
            }
            else
            {
                if (eol >= 0)
                {
                    int length = this.discardedBytes + eol - buffer.ReaderIndex;
                    int delimLength = buffer.GetByte(eol) == '\r' ? 2 : 1;
                    buffer.SetReaderIndex(eol + delimLength);
                    this.discardedBytes = 0;
                    this.discarding = false;
                    if (!this.failFast)
                    {
                        this.Fail(ctx, length);
                    }
                }
                else
                {
                    this.discardedBytes += buffer.ReadableBytes;
                    buffer.SetReaderIndex(buffer.WriterIndex);
                }
                return null;
            }
        }
开发者ID:RabbitTeam,项目名称:DotNetty,代码行数:73,代码来源:LineBasedFrameDecoder.cs


注:本文中的IByteBuffer.SkipBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。