protected internal virtual RedisRaw ExpectArray(RedisCommand command, bool throwException = true)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            ValidateNotDisposed();

            using (var connection = Connect(command.DbIndex, command.Role))
            {
                return(command.ExpectArray(connection, throwException));
            }
        }
        private bool Exec(IList <RedisRequest> requests, RedisSocketContext context)
        {
            if (requests != null)
            {
                var exec = new RedisCommand(DbIndex, RedisCommandList.Exec);

                var execResult = exec.ExpectArray(context);
                if (!ReferenceEquals(execResult, null))
                {
                    return(ProcessResult(requests, execResult.Value));
                }
            }
            return(false);
        }
        private NodeRoleAndSiblings GetSiblingSentinelsOfSentinel(string masterName, IRedisConnection connection)
        {
            if (masterName.IsEmpty())
            {
                throw new ArgumentNullException("masterName");
            }

            try
            {
                using (var command = new RedisCommand(-1, RedisCommandList.Sentinel, RedisCommandType.SendAndReceive,
                                                      RedisCommandList.Sentinels, masterName.ToBytes()))
                {
                    var raw = command.ExpectArray(connection, false);
                    if (!ReferenceEquals(raw, null))
                    {
                        var rawValue = raw.Value;
                        if (!ReferenceEquals(rawValue, null) && rawValue.Type == RedisRawObjectType.Array)
                        {
                            var sentinelInfos = RedisSentinelNodeInfo.ParseInfoResponse(rawValue);
                            if (sentinelInfos != null)
                            {
                                var length = sentinelInfos.Length;
                                if (length > 0)
                                {
                                    var siblingEndPoints = new List <RedisEndPoint>(length);
                                    for (var i = 0; i < length; i++)
                                    {
                                        var sentinelInfo = sentinelInfos[i];

                                        if (!Disposed && sentinelInfo != null && sentinelInfo.Port.HasValue && !sentinelInfo.IPAddress.IsEmpty())
                                        {
                                            siblingEndPoints.Add(new RedisEndPoint(sentinelInfo.IPAddress, (int)sentinelInfo.Port.Value));
                                        }
                                    }

                                    if (!Disposed)
                                    {
                                        return(new NodeRoleAndSiblings(RedisRole.Sentinel, siblingEndPoints.ToArray()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            { }
            return(null);
        }
Exemple #4
0
        protected internal override RedisRaw ExpectArray(RedisCommand command, bool throwException = true)
        {
            using (var result = TryToExecuteAsync <RedisRaw>(command, RedisCommandExpect.Array, (string)null))
            {
                if (result.Handled)
                {
                    return(result.Result);
                }

                using (var connection = (result.UsingConnection() ?? Connect(command.DbIndex, command.Role)))
                {
                    return(command.ExpectArray(connection, throwException));
                }
            }
        }
Exemple #5
0
        protected virtual RedisRole DiscoverRole(RedisSocket socket)
        {
            if (!Disposed)
            {
                var role = RedisRole.Undefined;
                try
                {
                    using (var cmd = new RedisCommand(-1, RedisCommandList.Role, RedisCommandType.SendAndReceive))
                    {
                        var raw = cmd.ExpectArray(new RedisSocketContext(socket, Settings), true);
                        if (!ReferenceEquals(raw, null) && raw.IsCompleted)
                        {
                            var info = RedisRoleInfo.Parse(raw.Value);
                            if (!ReferenceEquals(info, null))
                            {
                                role = info.Role;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    var exception = e;
                    while (exception != null)
                    {
                        if (exception is SocketException)
                        {
                            return(RedisRole.Undefined);
                        }

                        var re = e as RedisException;
                        if (re != null && (re.ErrorCode == RedisErrorCode.ConnectionError ||
                                           re.ErrorCode == RedisErrorCode.SocketError))
                        {
                            return(RedisRole.Undefined);
                        }

                        exception = exception.InnerException;
                    }
                }

                if (role == RedisRole.Undefined)
                {
                    try
                    {
                        var serverInfo = GetServerInfo();
                        if (serverInfo != null)
                        {
                            var serverSection = serverInfo.Server;
                            if (serverSection != null)
                            {
                                role = (serverSection.RedisMode ?? String.Empty).Trim().ToRedisRole();
                            }

                            if (role == RedisRole.Undefined)
                            {
                                var replicationSection = serverInfo.Replication;
                                if (replicationSection != null)
                                {
                                    role = (replicationSection.Role ?? String.Empty).Trim().ToRedisRole();
                                }

                                if (role == RedisRole.Undefined)
                                {
                                    var sentinelSection = serverInfo.Sentinel;
                                    if (sentinelSection != null && sentinelSection.SentinelMasters.HasValue)
                                    {
                                        role = RedisRole.Sentinel;
                                    }

                                    if (role == RedisRole.Undefined)
                                    {
                                        role = RedisRole.Master;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    { }
                }

                socket.Role = role;
                return(role);
            }
            return(RedisRole.Undefined);
        }
        protected internal override T Expect <T>(RedisCommand command, RedisCommandExpect expectation, string okIf = null)
        {
            using (var connection = Connect())
            {
                switch (expectation)
                {
                case RedisCommandExpect.Response:
                    return((T)(object)command.Execute(connection, ThrowOnError));

                case RedisCommandExpect.Array:
                    return((T)(object)command.ExpectArray(connection, ThrowOnError));

                case RedisCommandExpect.BulkString:
                    return((T)(object)command.ExpectBulkString(connection, ThrowOnError));

                case RedisCommandExpect.BulkStringBytes:
                    return((T)(object)command.ExpectBulkStringBytes(connection, ThrowOnError));

                case RedisCommandExpect.Double:
                    return((T)(object)command.ExpectDouble(connection, ThrowOnError));

                case RedisCommandExpect.GreaterThanZero:
                    return((T)(object)command.ExpectInteger(connection, ThrowOnError));

                case RedisCommandExpect.Integer:
                    return((T)(object)command.ExpectInteger(connection, ThrowOnError));

                case RedisCommandExpect.MultiDataBytes:
                    return((T)(object)command.ExpectMultiDataBytes(connection, ThrowOnError));

                case RedisCommandExpect.MultiDataStrings:
                    return((T)(object)command.ExpectMultiDataStrings(connection, ThrowOnError));

                case RedisCommandExpect.Nothing:
                    return((T)(object)command.ExpectNothing(connection, ThrowOnError));

                case RedisCommandExpect.NullableDouble:
                    return((T)(object)command.ExpectNullableDouble(connection, ThrowOnError));

                case RedisCommandExpect.NullableInteger:
                    return((T)(object)command.ExpectNullableInteger(connection, ThrowOnError));

                case RedisCommandExpect.OK:
                    return((T)(object)command.ExpectOK(connection, ThrowOnError));

                case RedisCommandExpect.One:
                    return((T)(object)command.ExpectOne(connection, ThrowOnError));

                case RedisCommandExpect.SimpleString:
                    if (!okIf.IsEmpty())
                    {
                        return((T)(object)command.ExpectSimpleString(connection, okIf, ThrowOnError));
                    }
                    return((T)(object)command.ExpectSimpleString(connection, ThrowOnError));

                case RedisCommandExpect.SimpleStringBytes:
                    if (!okIf.IsEmpty())
                    {
                        return((T)(object)command.ExpectSimpleStringBytes(connection, okIf.ToBytes(), ThrowOnError));
                    }
                    return((T)(object)command.ExpectSimpleStringBytes(connection, ThrowOnError));

                default:
                    throw new RedisException(String.Format("Undefined expectation type, {0}", expectation.ToString("F")), RedisErrorCode.NotSupported);
                }
            }
        }