Exemplo n.º 1
0
        public void CanHandleNonTrivialCommands(string command)
        {
            var cmd = new CommandBytes(command);

            Assert.Equal(command.Length, cmd.Length);
            Assert.Equal(command.ToUpperInvariant(), cmd.ToString());

            Assert.Equal(31, CommandBytes.MaxLength);
        }
Exemplo n.º 2
0
 public RespCommand(RedisCommandAttribute attrib, MethodInfo method, RespServer server)
 {
     _operation   = (RespOperation)Delegate.CreateDelegate(typeof(RespOperation), server, method);
     Command      = (string.IsNullOrWhiteSpace(attrib.Command) ? method.Name : attrib.Command).Trim().ToLowerInvariant();
     CommandBytes = new CommandBytes(Command);
     SubCommand   = attrib.SubCommand?.Trim()?.ToLowerInvariant();
     Arity        = attrib.Arity;
     MaxArgs      = attrib.MaxArgs;
     LockFree     = attrib.LockFree;
     _subcommands = null;
 }
Exemplo n.º 3
0
        internal bool TryGetCommandBytes(int i, out CommandBytes command)
        {
            var payload = _inner[i].Payload;

            if (payload.Length > CommandBytes.MaxLength)
            {
                command = default;
                return(false);
            }

            command = payload.IsEmpty ? default : new CommandBytes(payload);
                      return(true);
        }
Exemplo n.º 4
0
        public void CheckCommandContents()
        {
            for (int len = 0; len <= CommandBytes.MaxLength; len++)
            {
                var          s = new string('A', len);
                CommandBytes b = s;
                Assert.Equal(len, b.Length);

                var t = b.ToString();
                Assert.Equal(s, t);

                CommandBytes b2 = t;
                Assert.Equal(b, b2);

                Assert.Equal(len == 0, ReferenceEquals(s, t));
            }
        }
Exemplo n.º 5
0
        private byte CalculateChecksum()
        {
            const int checksumValue     = 173;
            var       filteredDataBytes = DataBytes;

            if (filteredDataBytes.Any(b => b == 0x07))
            { // For checksum calculation only one 0x07 byte can be included
                filteredDataBytes = filteredDataBytes.Where(b => b != 0x07)
                                    .Append <byte>(0x07)
                                    .ToArray();
            }

            var allBytes = CommandBytes.Append(DataLength).Concat(filteredDataBytes);

            var checksum = allBytes.Aggregate(checksumValue, (acc, val) => acc + val);

            return((byte)(checksum & 0xFF));
        }
Exemplo n.º 6
0
    static int Main()
    {
        var lookup = new Dictionary <CommandBytes, string>();

        void Add(string val)
        {
            var cb = new CommandBytes(val);

            // prove we didn't screw up
            if (cb.ToString() != val)
            {
                throw new InvalidOperationException("oops!");
            }
            lookup.Add(cb, val);
        }

        Add("client");
        Add("cluster");
        Add("command");
        Add("config");
        Add("dbsize");
        Add("decr");
        Add("del");
        Add("echo");
        Add("exists");
        Add("flushall");
        Add("flushdb");
        Add("get");
        Add("incr");
        Add("incrby");
        Add("info");
        Add("keys");
        Add("llen");
        Add("lpop");
        Add("lpush");
        Add("lrange");
        Add("memory");
        Add("mget");
        Add("mset");
        Add("ping");
        Add("quit");
        Add("role");
        Add("rpop");
        Add("rpush");
        Add("sadd");
        Add("scard");
        Add("select");
        Add("set");
        Add("shutdown");
        Add("sismember");
        Add("spop");
        Add("srem");
        Add("strlen");
        Add("subscribe");
        Add("time");
        Add("unlink");
        Add("unsubscribe");

        bool HuntFor(string lookFor)
        {
            Console.WriteLine($"Looking for: '{lookFor}'");
            var  hunt   = new CommandBytes(lookFor);
            bool result = lookup.TryGetValue(hunt, out var found);

            if (result)
            {
                Console.WriteLine($"Found via TryGetValue: '{found}'");
            }
            else
            {
                Console.WriteLine("**NOT FOUND** via TryGetValue");
            }

            Console.WriteLine("looking manually");
            foreach (var pair in lookup)
            {
                if (pair.Value == lookFor)
                {
                    Console.WriteLine($"Found manually: '{pair.Value}'");
                    var key = pair.Key;
                    void Compare <T>(string caption, Func <CommandBytes, T> func)
                    {
                        T x = func(hunt), y = func(key);

                        Console.WriteLine($"{caption}: {EqualityComparer<T>.Default.Equals(x, y)}, '{x}' vs '{y}'");
                    }

                    Compare("GetHashCode", _ => _.GetHashCode());
                    Compare("ToString", _ => _.ToString());
                    Compare("Length", _ => _.Length);
                    Compare("ToInnerString", _ => _.ToInnerString());
                    Console.WriteLine($"Equals: {key.Equals(hunt)}, {hunt.Equals(key)}");
                    var eq = EqualityComparer <CommandBytes> .Default;

                    Console.WriteLine($"EqualityComparer: {eq.Equals(key, hunt)}, {eq.Equals(hunt, key)}");
                    Compare("eq GetHashCode", _ => eq.GetHashCode(_));
                }
            }
            Console.WriteLine();

            return(result);
        }

        bool result1 = HuntFor("ping");
        bool result2 = HuntFor("subscribe");

        return((result1 && result2) ? 100 : -1);
    }