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


C# IByteBuffer.ReadSlice方法代码示例

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


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

示例1: 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

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