示例#1
0
        /// <summary>
        /// ExecuteBulkReplies
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="valueFactory"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">valueFactory is null.</exception>
        private Task <T> ExecuteBulkReplies <T>(RedisRequest request, Func <byte[], T> valueFactory, object asyncState)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException("valueFactory");
            }

            return(this.Execute <T>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as BulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Payload == null || mbReeply.Payload.Length == 0)
                    {
                        source.TrySetResult(default(T));
                        return;
                    }

                    try { source.TrySetResult(valueFactory(mbReeply.Payload)); }
                    catch (Exception ex)
                    {
                        source.TrySetException(ex);
                    }
                    return;
                }

                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }

                source.TrySetException(new RedisException("Failed to resolve the bulkReplies"));
            }, asyncState));
        }
示例#2
0
        /// <summary>
        /// ExecuteMultiBulkReplies
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        private Task <byte[][]> ExecuteMultiBulkReplies(RedisRequest request, object asyncState)
        {
            return(this.Execute <byte[][]>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as MultiBulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Replies == null)
                    {
                        source.TrySetResult(new byte[0][]);
                        return;
                    }

                    byte[][] arrBytes = new byte[mbReeply.Replies.Length][];
                    for (int i = 0, l = mbReeply.Replies.Length; i < l; i++)
                    {
                        var objBulk = mbReeply.Replies[i] as BulkReplies;
                        if (objBulk != null)
                        {
                            arrBytes[i] = objBulk.Payload;
                        }
                    }
                    source.TrySetResult(arrBytes);
                    return;
                }
                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }
                source.TrySetException(new RedisException("Failed to resolve the multiBulkReplies."));
            }, asyncState));
        }
示例#3
0
        /// <summary>
        /// ExecuteMultiBulkReplies
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="valueFactory"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">valueFactory is null.</exception>
        private Task <T[]> ExecuteMultiBulkReplies <T>(RedisRequest request, Func <byte[], T> valueFactory, object asyncState)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException("valueFactory");
            }

            return(this.Execute <T[]>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as MultiBulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Replies == null)
                    {
                        source.TrySetResult(null);
                        return;
                    }

                    var arrResults = new T[mbReeply.Replies.Length];
                    for (int i = 0, l = mbReeply.Replies.Length; i < l; i++)
                    {
                        var objBulk = mbReeply.Replies[i] as BulkReplies;
                        if (objBulk == null)
                        {
                            continue;
                        }

                        if (objBulk.Payload == null)
                        {
                            arrResults[i] = default(T);
                            continue;
                        }

                        try { arrResults[i] = valueFactory(objBulk.Payload); }
                        catch (Exception ex)
                        {
                            source.TrySetException(ex);
                            return;
                        }
                    }
                    source.TrySetResult(arrResults);
                    return;
                }
                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }
                source.TrySetException(new RedisException(
                                           "Failed to resolve the multiBulkReplies, expected is multiBulkReplies, but was " + response.Reply.GetType().Name));
            }, asyncState));
        }
示例#4
0
 /// <summary>
 /// ExecuteBulkReplies
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <byte[]> ExecuteBulkReplies(RedisRequest request, object asyncState)
 {
     return(this.Execute <byte[]>(request.ToPayload(), (source, response) =>
     {
         var mbReeply = response.Reply as BulkReplies;
         if (mbReeply != null)
         {
             source.TrySetResult(mbReeply.Payload);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the bulkReplies"));
     }, asyncState));
 }
示例#5
0
 /// <summary>
 /// ExecuteStatusReply
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <string> ExecuteStatusReply(RedisRequest request, object asyncState)
 {
     return(this.Execute <string>(request.ToPayload(), (source, response) =>
     {
         var statusReply = response.Reply as StatusReply;
         if (statusReply != null)
         {
             source.TrySetResult(statusReply.Status);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the statusReply"));
     }, asyncState));
 }
示例#6
0
 /// <summary>
 /// ExecuteIntegerReply2
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <bool> ExecuteIntegerReply2(RedisRequest request, object asyncState)
 {
     return(this.Execute <bool>(request.ToPayload(), (source, response) =>
     {
         var intReply = response.Reply as IntegerReply;
         if (intReply != null)
         {
             source.TrySetResult(intReply.Value == 1);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the integerReply"));
     }, asyncState));
 }
示例#7
0
 /// <summary>
 /// ExecuteIntegerReply
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <int> ExecuteIntegerReply(RedisRequest request, object asyncState)
 {
     return(this.Execute <int>(request.ToPayload(), (source, response) =>
     {
         var intReply = response.Reply as IntegerReply;
         if (intReply != null)
         {
             source.TrySetResult(intReply.Value);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException(
                                    "Failed to resolve the integerReply, expected is integerReply, but was " + response.Reply.GetType().Name));
     }, asyncState));
 }
示例#8
0
        /// <summary>
        /// unsubscribe pattern
        /// </summary>
        /// <param name="patterns"></param>
        private void UnPatternSubscribeInternal(string[] patterns)
        {
            if (patterns == null || patterns.Length == 0)
            {
                return;
            }

            var connection = this._currentConnection;

            if (connection == null)
            {
                return;
            }

            var r = new RedisRequest(patterns.Length + 1).AddArgument("PUNSUBSCRIBE");

            foreach (var pattern in patterns)
            {
                r.AddArgument(pattern);
            }

            connection.BeginSend(new Packet(r.ToPayload()));
        }
示例#9
0
        /// <summary>
        /// unsubscribe channel
        /// </summary>
        /// <param name="channels"></param>
        private void UnSubscribeInternal(string[] channels)
        {
            if (channels == null || channels.Length == 0)
            {
                return;
            }

            var connection = this._currentConnection;

            if (connection == null)
            {
                return;
            }

            var r = new RedisRequest(channels.Length + 1).AddArgument("UNSUBSCRIBE");

            foreach (var channel in channels)
            {
                r.AddArgument(channel);
            }

            connection.BeginSend(new Packet(r.ToPayload()));
        }
        /// <summary>
        /// unsubscribe pattern
        /// </summary>
        /// <param name="patterns"></param>
        private void UnPatternSubscribeInternal(string[] patterns)
        {
            if (patterns == null || patterns.Length == 0) return;

            var connection = this._currentConnection;
            if (connection == null) return;

            var r = new RedisRequest(patterns.Length + 1).AddArgument("PUNSUBSCRIBE");
            foreach (var pattern in patterns) r.AddArgument(pattern);

            connection.BeginSend(new Packet(r.ToPayload()));
        }
        /// <summary>
        /// unsubscribe channel
        /// </summary>
        /// <param name="channels"></param>
        private void UnSubscribeInternal(string[] channels)
        {
            if (channels == null || channels.Length == 0) return;

            var connection = this._currentConnection;
            if (connection == null) return;

            var r = new RedisRequest(channels.Length + 1).AddArgument("UNSUBSCRIBE");
            foreach (var channel in channels) r.AddArgument(channel);

            connection.BeginSend(new Packet(r.ToPayload()));
        }