コード例 #1
0
        /// <summary>
        /// Adds a new endpoint to the list
        /// </summary>
        public void Add(string hostAndPort)
        {
            var endpoint = Format.TryParseEndPoint(hostAndPort);

            if (endpoint == null)
            {
                throw new ArgumentException();
            }
            Add(endpoint);
        }
コード例 #2
0
        internal ClusterNode(ClusterConfiguration configuration, string raw, EndPoint origin)
        {
            // http://redis.io/commands/cluster-nodes
            this.configuration = configuration;
            this.Raw           = raw;
            var parts = raw.Split(StringSplits.Space);

            var flags = parts[2].Split(StringSplits.Comma);

            EndPoint = Format.TryParseEndPoint(parts[1]);
            if (flags.Contains("myself"))
            {
                IsMyself = true;
                if (EndPoint == null)
                {
                    // Unconfigured cluster nodes might report themselves as endpoint ":{port}",
                    // hence the origin fallback value to make sure that we can address them
                    EndPoint = origin;
                }
            }

            NodeId       = parts[0];
            IsSlave      = flags.Contains("slave");
            IsNoAddr     = flags.Contains("noaddr");
            ParentNodeId = string.IsNullOrWhiteSpace(parts[3]) ? null : parts[3];

            List <SlotRange> slots = null;

            for (int i = 8; i < parts.Length; i++)
            {
                SlotRange range;
                if (SlotRange.TryParse(parts[i], out range))
                {
                    if (slots == null)
                    {
                        slots = new List <SlotRange>(parts.Length - i);
                    }
                    slots.Add(range);
                }
            }
            this.Slots       = slots?.AsReadOnly() ?? NoSlots;
            this.IsConnected = parts[7] == "connected"; // Can be "connected" or "disconnected"
        }
コード例 #3
0
 /// <summary>
 /// Attempt to parse a string into an EndPoint
 /// </summary>
 public static EndPoint TryParse(string endpoint)
 {
     return(Format.TryParseEndPoint(endpoint));
 }
コード例 #4
0
        void MatchResult(RawResult result)
        {
            // check to see if it could be an out-of-band pubsub message
            if (connectionType == ConnectionType.Subscription && result.Type == ResultType.MultiBulk)
            {   // out of band message does not match to a queued message
                var items = result.GetItems();
                if (items.Length >= 3 && items[0].IsEqual(message))
                {
                    // special-case the configuration change broadcasts (we don't keep that in the usual pub/sub registry)
                    var configChanged = Multiplexer.ConfigurationChangedChannel;
                    if (configChanged != null && items[1].IsEqual(configChanged))
                    {
                        EndPoint blame = null;
                        try
                        {
                            if (!items[2].IsEqual(RedisLiterals.ByteWildcard))
                            {
                                blame = Format.TryParseEndPoint(items[2].GetString());
                            }
                        }
                        catch { /* no biggie */ }
                        Multiplexer.Trace("Configuration changed: " + Format.ToString(blame), physicalName);
                        Multiplexer.ReconfigureIfNeeded(blame, true, "broadcast");
                    }

                    // invoke the handlers
                    var channel = items[1].AsRedisChannel(ChannelPrefix, RedisChannel.PatternMode.Literal);
                    Multiplexer.Trace("MESSAGE: " + channel, physicalName);
                    if (!channel.IsNull)
                    {
                        Multiplexer.OnMessage(channel, channel, items[2].AsRedisValue());
                    }
                    return; // AND STOP PROCESSING!
                }
                else if (items.Length >= 4 && items[0].IsEqual(pmessage))
                {
                    var channel = items[2].AsRedisChannel(ChannelPrefix, RedisChannel.PatternMode.Literal);
                    Multiplexer.Trace("PMESSAGE: " + channel, physicalName);
                    if (!channel.IsNull)
                    {
                        var sub = items[1].AsRedisChannel(ChannelPrefix, RedisChannel.PatternMode.Pattern);
                        Multiplexer.OnMessage(sub, channel, items[3].AsRedisValue());
                    }
                    return; // AND STOP PROCESSING!
                }

                // if it didn't look like "[p]message", then we still need to process the pending queue
            }
            Multiplexer.Trace("Matching result...", physicalName);
            Message msg;

            lock (outstanding)
            {
                Multiplexer.Trace(outstanding.Count == 0, "Nothing to respond to!", physicalName);
                msg = outstanding.Dequeue();
            }

            Multiplexer.Trace("Response to: " + msg.ToString(), physicalName);
            if (msg.ComputeResult(this, result))
            {
                Bridge.CompleteSyncOrAsync(msg);
            }
        }
コード例 #5
0
        internal static ClientInfo[] Parse(string input)
        {
            if (input == null)
            {
                return(null);
            }

            var clients = new List <ClientInfo>();

            using (var reader = new StringReader(input))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var client = new ClientInfo();
                    client.Raw = line;
                    string[] tokens = line.Split(StringSplits.Space);
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        string tok = tokens[i];
                        int    idx = tok.IndexOf('=');
                        if (idx < 0)
                        {
                            continue;
                        }
                        string key = tok.Substring(0, idx), value = tok.Substring(idx + 1);

                        switch (key)
                        {
                        case "addr": client.Address = Format.TryParseEndPoint(value); break;

                        case "age": client.AgeSeconds = Format.ParseInt32(value); break;

                        case "idle": client.IdleSeconds = Format.ParseInt32(value); break;

                        case "db": client.Database = Format.ParseInt32(value); break;

                        case "name": client.Name = value; break;

                        case "sub": client.SubscriptionCount = Format.ParseInt32(value); break;

                        case "psub": client.PatternSubscriptionCount = Format.ParseInt32(value); break;

                        case "multi": client.TransactionCommandLength = Format.ParseInt32(value); break;

                        case "cmd": client.LastCommand = value; break;

                        case "flags":
                            client.FlagsRaw = value;
                            ClientFlags flags = ClientFlags.None;
                            AddFlag(ref flags, value, ClientFlags.SlaveMonitor, 'O');
                            AddFlag(ref flags, value, ClientFlags.Slave, 'S');
                            AddFlag(ref flags, value, ClientFlags.Master, 'M');
                            AddFlag(ref flags, value, ClientFlags.Transaction, 'x');
                            AddFlag(ref flags, value, ClientFlags.Blocked, 'b');
                            AddFlag(ref flags, value, ClientFlags.TransactionDoomed, 'd');
                            AddFlag(ref flags, value, ClientFlags.Closing, 'c');
                            AddFlag(ref flags, value, ClientFlags.Unblocked, 'u');
                            AddFlag(ref flags, value, ClientFlags.CloseASAP, 'A');
                            client.Flags = flags;
                            break;

                        case "id": client.Id = Format.ParseInt64(value); break;
                        }
                    }
                    clients.Add(client);
                }
            }

            return(clients.ToArray());
        }