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


C# IByteBuffer.SetReaderIndex方法代码示例

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


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

示例1: WriteBytes

 public virtual IByteBuffer WriteBytes(IByteBuffer src, int length)
 {
     if (length > src.ReadableBytes)
     {
         throw new IndexOutOfRangeException(string.Format("length({0}) exceeds src.readableBytes({1}) where src is: {2}", length, src.ReadableBytes, src));
     }
     this.WriteBytes(src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return this;
 }
开发者ID:dalong123,项目名称:DotNetty,代码行数:10,代码来源:AbstractByteBuffer.cs

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

示例3: SetBytes

 public virtual IByteBuffer SetBytes(int index, IByteBuffer src, int length)
 {
     this.CheckIndex(index, length);
     if (src == null)
     {
         throw new NullReferenceException("src cannot be null");
     }
     if (length > src.ReadableBytes)
     {
         throw new IndexOutOfRangeException(string.Format(
             "length({0}) exceeds src.readableBytes({1}) where src is: {2}", length, src.ReadableBytes, src));
     }
     this.SetBytes(index, src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return this;
 }
开发者ID:dalong123,项目名称:DotNetty,代码行数:16,代码来源:AbstractByteBuffer.cs

示例4: DoWriteBytes

        protected override int DoWriteBytes(IByteBuffer buf)
        {
            if (!buf.HasArray)
            {
                throw new NotImplementedException("Only IByteBuffer implementations backed by array are supported.");
            }

            SocketError errorCode;
            int sent = this.Socket.Send(buf.Array, buf.ArrayOffset + buf.ReaderIndex, buf.ReadableBytes, SocketFlags.None, out errorCode);

            if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock)
            {
                throw new SocketException((int)errorCode);
            }

            if (sent > 0)
            {
                buf.SetReaderIndex(buf.ReaderIndex + sent);
            }

            return sent;
        }
开发者ID:nayato,项目名称:DotNetty,代码行数:22,代码来源:TcpSocketChannel.cs

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

示例6: WriteBytes

 public virtual IByteBuffer WriteBytes(IByteBuffer src, int length)
 {
     if (length > src.ReadableBytes)
     {
         throw new IndexOutOfRangeException($"length({length}) exceeds src.readableBytes({src.ReadableBytes}) where src is: {src}");
     }
     this.WriteBytes(src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return this;
 }
开发者ID:RabbitTeam,项目名称:DotNetty,代码行数:10,代码来源:AbstractByteBuffer.cs

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