private void Process(TcpClient client)
 {
     using (client)
     {
         var parser = new Parser();
         try
         {
             using (var stream = client.GetStream())
             {
                 var redisStreamReader = new RedisStreamReader(stream, _token, 2000000);
                 while (!_token.IsCancellationRequested)
                 {
                     var command = parser.Parse(redisStreamReader);
                     if (null != command)
                     {
                         var result = _engine.Process(command);
                         parser.ConvertToString(result, stream);
                         stream.Flush();
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
 }
Exemple #2
0
        public RedisToken Parse(RedisStreamReader reader)
        {
            var rawType = reader.ReadByte();

            if (!rawType.HasValue)
            {
                return(null);
            }
            var type = (char)rawType.Value;

            switch (type)
            {
            case '*':
            {
                var length = int.Parse(reader.ReadLine());
                var token  = new ArrayRedisToken(length);
                for (int i = 0; i < length; i++)
                {
                    token.Add(Parse(reader));
                }
                return(token);
            }

            case '+':
            {
                return(new SimpleStringRedisToken
                    {
                        Data = reader.ReadLine()
                    });
            }

            case ':':
            {
                return(new IntegerRedisToken
                    {
                        Data = int.Parse(reader.ReadLine())
                    });
            }

            case '$':
            {
                var length  = int.Parse(reader.ReadLine());
                var content = reader.ReadBytes(length);
                reader.ReadEndLine();
                return(new BulkStringRedisToken()
                    {
                        Content = content
                    });
            }

            default:
                throw new NotImplementedException();
            }
        }