Exemplo n.º 1
0
 public static int IndexOf(this IByteBuffer buf, byte value)
 {
     if (buf is IByteBuffer2 buffer2)
     {
         return(buffer2.IndexOf(value));
     }
     return(buf.IndexOf(buf.ReaderIndex, buf.WriterIndex, value));
 }
Exemplo n.º 2
0
        public int IndexOf(int fromIndex, int toIndex, byte value)
        {
            if (0u >= (uint)(fromIndex - toIndex))
            {
                return(SharedConstants.IndexNotFound);
            }

            if ((uint)Math.Max(fromIndex, toIndex) > (uint)_buffer.WriterIndex)
            {
                ThrowReplay();
            }

            return(_buffer.IndexOf(fromIndex, toIndex, value));
        }
Exemplo n.º 3
0
        public static int BytesBefore(this IByteBuffer buf, int index, int length, byte value)
        {
            if (buf is IByteBuffer2 buffer2)
            {
                return(buffer2.BytesBefore(index, length, value));
            }
            int endIndex = buf.IndexOf(index, index + length, value);

            if ((uint)endIndex > SharedConstants.TooBigOrNegative)
            {
                return(SharedConstants.IndexNotFound);
            }
            return(endIndex - index);
        }
        /// <summary>
        /// Finds the index of a CR LF sequence (EOL). The index points to LF.
        /// Returns -1 if there is no EOL. </summary>
        /// <param name="startIndex"> starting index (inclusive) </param>
        /// <param name="endIndex"> ending index (exclusive) </param>
        private int findEol(IByteBuffer buf, int startIndex, int endIndex)
        {
            int eolIndex = -1;

            if (startIndex >= endIndex)
            {
                return(eolIndex);
            }
            int crIndex = buf.IndexOf(startIndex, endIndex, CR);

            if (crIndex != -1 && crIndex != endIndex - 1 && buf.GetByte(crIndex + 1) == LF)
            {
                eolIndex = crIndex + 1;
            }
            return(eolIndex);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the index in the buffer of the end of line found.
        /// Returns <c>-1</c> if no end of line was found in the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        int FindEndOfLine(IByteBuffer buffer)
        {
            const byte LineFeed = (byte)'\n';
            int        i        = buffer.IndexOf(buffer.ReaderIndex + offset, buffer.WriterIndex, LineFeed);

            if (i >= 0)
            {
                offset = 0;
                if (i > 0 && buffer.GetByte(i - 1) == '\r')
                {
                    i--;
                }
            }
            else
            {
                offset = buffer.ReadableBytes;
            }
            return(i);
        }
Exemplo n.º 6
0
        WebSocketFrame DecodeTextFrame(IChannelHandlerContext ctx, IByteBuffer buffer)
        {
            int ridx     = buffer.ReaderIndex;
            int rbytes   = ActualReadableBytes;
            int delimPos = buffer.IndexOf(ridx, ridx + rbytes, 0xFF);

            if ((uint)delimPos > SharedConstants.TooBigOrNegative) // == -1
            {
                // Frame delimiter (0xFF) not found
                if (rbytes > _maxFrameSize)
                {
                    // Frame length exceeded the maximum
                    ThrowHelper.ThrowTooLongFrameException_WebSocket00FrameDecoder();
                }
                else
                {
                    // Wait until more data is received
                    return(null);
                }
            }

            int frameSize = delimPos - ridx;

            if (frameSize > _maxFrameSize)
            {
                ThrowHelper.ThrowTooLongFrameException_WebSocket00FrameDecoder();
            }

            IByteBuffer binaryData = ReadBytes(ctx.Allocator, buffer, frameSize);

            _ = buffer.SkipBytes(1);

            var endIndex   = binaryData.WriterIndex;
            int ffDelimPos = binaryData.IndexOf(binaryData.ReaderIndex, endIndex, 0xFF);

            if ((uint)endIndex >= (uint)ffDelimPos) // ffDelimPos >= 0
            {
                _ = binaryData.Release();
                ThrowHelper.ThrowArgumentException_TextFrame();
            }

            return(new TextWebSocketFrame(binaryData));
        }
        WebSocketFrame DecodeTextFrame(IChannelHandlerContext ctx, IByteBuffer buffer)
        {
            int ridx     = buffer.ReaderIndex;
            int rbytes   = this.ActualReadableBytes;
            int delimPos = buffer.IndexOf(ridx, ridx + rbytes, 0xFF);

            if (delimPos == -1)
            {
                // Frame delimiter (0xFF) not found
                if (rbytes > this.maxFrameSize)
                {
                    // Frame length exceeded the maximum
                    throw new TooLongFrameException(nameof(WebSocket00FrameDecoder));
                }
                else
                {
                    // Wait until more data is received
                    return(null);
                }
            }

            int frameSize = delimPos - ridx;

            if (frameSize > this.maxFrameSize)
            {
                throw new TooLongFrameException(nameof(WebSocket00FrameDecoder));
            }

            IByteBuffer binaryData = ReadBytes(ctx.Allocator, buffer, frameSize);

            buffer.SkipBytes(1);

            int ffDelimPos = binaryData.IndexOf(binaryData.ReaderIndex, binaryData.WriterIndex, 0xFF);

            if (ffDelimPos >= 0)
            {
                binaryData.Release();
                throw new ArgumentException("a text frame should not contain 0xFF.");
            }

            return(new TextWebSocketFrame(binaryData));
        }
Exemplo n.º 8
0
        static IByteBuffer ReadLine(IByteBuffer input)
        {
            if (!input.IsReadable(RedisConstants.EndOfLineLength))
            {
                return(null);
            }

            //int lfIndex = input.ForEachByte(ByteProcessor.FindLF);
            const byte LineFeed = (byte)'\n';
            int        lfIndex  = input.IndexOf(LineFeed);

            if (lfIndex < 0)
            {
                return(null);
            }

            IByteBuffer data = input.ReadSlice(lfIndex - input.ReaderIndex - 1); // `-1` is for CR

            ReadEndOfLine(input);                                                // validate CR LF
            return(data);
        }
Exemplo n.º 9
0
 public static int IndexOf(IByteBuffer needle, IByteBuffer haystack)
 {
     return(haystack.IndexOf(needle));
 }