Exemplo n.º 1
0
        internal RedisResponse Parse()
        {
            var line = ParseLine(out char marker);

            if (marker.Equals('-'))
            {
                throw new Exception(line);
            }

            var buffer = new RedisResponseBuffer();

            switch (marker)
            {
            case '$':
                ParseBulkReply(buffer, line);
                break;

            case '*':
                ParseMultiBulkReply(buffer, line);
                break;

            case ':':
            case '+':
                buffer.Push(line);
                break;

            default:
                throw new InvalidDataException($"Unknown Response Marker received. Response Marker \"{marker}\" is not supported.");
            }

            return(buffer.ToResponse());
        }
Exemplo n.º 2
0
        private void ParseBulkReply(RedisResponseBuffer buffer, string line)
        {
            if (!int.TryParse(line, out int length))
            {
                throw new InvalidDataException($"Expected Bulk Reply to indicate stream length. Received \"{line}\"");
            }

            if (length.Equals(-1))
            {
                buffer.PushEmpty();
            }
            else
            {
                buffer.Push(ParseLine(length));
            }
        }
Exemplo n.º 3
0
        private void ParseMultiBulkReply(RedisResponseBuffer buffer, string line)
        {
            if (!int.TryParse(line, out int count))
            {
                throw new InvalidDataException($"Expected Multi Bulk Reply to indicate result count. Received \"{line}\"");
            }

            for (int index = 0; index < count; index++)
            {
                var multiline = ParseLine(out char marker);
                if (!marker.Equals(':') && !marker.Equals('$') && !marker.Equals('*'))
                {
                    buffer.PushEmpty(index);
                    continue;
                }

                if (marker.Equals(':'))
                {
                    buffer.Push(index, multiline);
                    continue;
                }

                if (marker.Equals('*'))
                {
                    ParseMultiBulkReply(buffer, multiline);
                    continue;
                }

                if (!int.TryParse(multiline, out int length) || length.Equals(-1))
                {
                    buffer.PushEmpty(index);
                }
                else
                {
                    buffer.Push(index, ParseLine(length));
                }
            }
        }