public virtual void Write(RedisAsyncTask asyncTask, bool flush = true)
 {
     m_ReceiveWaitingQ.Enqueue(asyncTask);
     try
     {
         if (Write(asyncTask.Command, flush) && flush)
         {
             BeginReceive();
         }
     }
     catch (Exception e)
     {
         try
         {
             if (e is SocketException)
             {
                 CancelAllReceiveQ();
                 throw;
             }
         }
         finally
         {
             asyncTask.TrySetException(e);
         }
     }
 }
예제 #2
0
        protected override void DoAfterCompleteContext(RedisBufferContext context, RedisAsyncTask asyncTask)
        {
            if (context.ResultType == RedisRawObjectType.SimpleString)
            {
                var result = context.Result as RedisString;

                if (!ReferenceEquals(result, null) &&
                    result != RedisConstants.OK)
                {
                    var message = ToMonitorMessage(result);
                    if (message != null && !message.IsEmpty)
                    {
                        var callback = m_Callback;
                        if (callback != null)
                        {
                            callback(message);
                        }
                        return;
                    }
                }
            }

            if (asyncTask != null)
            {
                asyncTask.TrySetCompleted(context.Result);
            }
        }
예제 #3
0
        protected override void DoAfterCompleteContext(RedisBufferContext context, RedisAsyncTask asyncTask)
        {
            if (context.ResultType == RedisRawObjectType.Array)
            {
                var result = context.Result as RedisArray;

                if (!ReferenceEquals(result, null))
                {
                    var message = ToPubSubMessage(result);

                    if (message != null && !message.IsEmpty)
                    {
                        if (asyncTask != null &&
                            !(message.Type == RedisPubSubMessageType.Message ||
                              message.Type == RedisPubSubMessageType.PMessage))
                        {
                            asyncTask.TrySetCompleted(context.Result);
                        }

                        var callback = m_Callback;
                        if (callback != null)
                        {
                            callback(message);
                        }
                        return;
                    }
                }
            }

            if (asyncTask != null)
            {
                asyncTask.TrySetCompleted(context.Result);
            }
        }
 protected virtual void DoAfterCompleteContext(RedisBufferContext context, RedisAsyncTask asyncTask)
 {
     if (asyncTask != null)
     {
         asyncTask.TrySetCompleted(context.Result);
     }
 }
예제 #5
0
        protected Task <RedisResult> RunAsyncTask(RedisCommand command)
        {
            var tcs       = new TaskCompletionSource <RedisResult>();
            var asyncTask = new RedisAsyncTask(command, tcs);

            if (!UseBackgroundThread)
            {
                m_SendWaitingQ.Enqueue(asyncTask, command.Priority == RedisCommandPriority.High);
            }
            else
            {
                lock (m_SendWaitingQ)
                {
                    m_SendWaitingQ.Enqueue(asyncTask, command.Priority == RedisCommandPriority.High);
                    Monitor.PulseAll(m_SendWaitingQ);
                }
            }

            if (UseBackgroundThread)
            {
                InitBackgroundThread();
            }
            else
            {
                TrySendQ(false);
            }

            return(tcs.Task);
        }
 protected virtual void DoBeforeCompleteContext(RedisBufferContext context, out bool @continue, out RedisAsyncTask asyncTask)
 {
     @continue = m_ReceiveWaitingQ.TryDequeue(out asyncTask);
 }
 protected override void DoBeforeCompleteContext(RedisBufferContext context, out bool @continue, out RedisAsyncTask asyncTask)
 {
     @continue = true;
     asyncTask = m_ReceiveWaitingQ.Dequeue();
 }
예제 #8
0
        protected RedisResult[] RunSyncTask(RedisCommand[] commands)
        {
            ValidateNotDisposed();

            var count      = commands.Length;
            var asyncTasks = new Queue <RedisAsyncTask>(count);

            lock (m_SendWaitingQ.SyncLock)
            {
                for (var i = 0; i < count; i++)
                {
                    var command = commands[i];

                    var asyncTask = new RedisAsyncTask(command);
                    asyncTasks.Enqueue(asyncTask);

                    m_SendWaitingQ.Enqueue(asyncTask, command.Priority == RedisCommandPriority.High);
                }

                if (UseBackgroundThread)
                {
                    lock (m_SendWaitingQ)
                    {
                        Monitor.PulseAll(m_SendWaitingQ);
                    }
                }
            }

            if (UseBackgroundThread)
            {
                InitBackgroundThread();
            }
            else
            {
                TrySendQ(false);
            }

            var results = new List <RedisResult>(count);

            while (asyncTasks.Count > 0)
            {
                var asyncTask = asyncTasks.Dequeue();
                if (!asyncTask.IsCompleted)
                {
                    var spinCount      = 0;
                    var spinStartTime  = Environment.TickCount;
                    var notWinPlatform = !RedisCommon.IsWinPlatform;

                    do
                    {
                        if (Environment.TickCount - spinStartTime >= m_ReceiveTimeout)
                        {
                            throw new RedisFatalException("Request Timeout", RedisErrorCode.SocketError);
                        }

                        var socket = m_Socket;
                        if ((socket != null) && socket.ReceiveCallRequired)
                        {
                            socket.BeginReceive();
                            asyncTask.Wait(1);
                        }
                    }while (asyncTask.Wait(notWinPlatform || ++spinCount % 50 != 0 ? s_WaitTimeout : spinCount / 10));
                }

                var result = asyncTask.Result;
                if (result == null && asyncTask.IsCanceled)
                {
                    throw new RedisFatalException(asyncTask.Status.ToString("F"));
                }

                if (result is RedisError)
                {
                    throw new RedisFatalException(((RedisError)result).Value);
                }

                results.Add(result);
            }

            return(results.ToArray());
        }
예제 #9
0
        protected RedisResult RunSyncTask(RedisCommand command)
        {
            ValidateNotDisposed();

            var asyncTask = new RedisAsyncTask(command);

            if (!UseBackgroundThread)
            {
                m_SendWaitingQ.Enqueue(asyncTask, command.Priority == RedisCommandPriority.High);
            }
            else
            {
                lock (m_SendWaitingQ)
                {
                    m_SendWaitingQ.Enqueue(asyncTask, command.Priority == RedisCommandPriority.High);
                    Monitor.PulseAll(m_SendWaitingQ);
                }
            }

            if (UseBackgroundThread)
            {
                InitBackgroundThread();
            }
            else
            {
                TrySendQ(false);
            }

            if (command.CommandType == RedisCommandType.SendNotReceive)
            {
                return(RedisVoid.Default);
            }

            if (!asyncTask.IsCompleted)
            {
                var spinCount      = 0;
                var spinStartTime  = Environment.TickCount;
                var notWinPlatform = !RedisCommon.IsWinPlatform;

                do
                {
                    if (Environment.TickCount - spinStartTime >= m_ReceiveTimeout)
                    {
                        throw new RedisFatalException("Request Timeout", RedisErrorCode.SocketError);
                    }

                    var socket = m_Socket;
                    if ((socket != null) && socket.ReceiveCallRequired)
                    {
                        socket.BeginReceive();
                        asyncTask.Wait(1);
                    }
                }while (asyncTask.Wait(notWinPlatform || ++spinCount % 50 != 0 ? s_WaitTimeout : spinCount / 10));
            }

            var result = asyncTask.Result;

            if (result == null && asyncTask.IsCanceled)
            {
                throw new RedisFatalException(asyncTask.Status.ToString("F"));
            }

            if (result is RedisError)
            {
                throw new RedisFatalException(((RedisError)result).Value);
            }

            return(result);
        }