public override RedisRawResponse SendReceive(IRedisCommand cmd)
        {
            if (cmd == null)
            {
                throw new RedisFatalException(new ArgumentNullException("cmd"), RedisErrorCode.MissingParameter);
            }

            ValidateNotDisposed();
            ValidateRole(cmd.Role);

            var socket = Connect();

            if (socket == null)
            {
                SetLastError((long)SocketError.NotConnected);
                SetState((long)RedisConnectionState.Failed);

                throw new SocketException((int)SocketError.NotConnected);
            }

            try
            {
                cmd.WriteTo(socket);
                using (var reader = new RedisSingleResponseReader(Settings))
                    return(reader.Execute(socket));
            }
            catch (Exception e)
            {
                if (e.IsSocketError())
                {
                    FreeAndNilSocket();
                }
                throw;
            }
        }
        public override RedisRawResponse SendReceive(byte[] data, RedisRole commandRole)
        {
            ValidateNotDisposed();
            ValidateRole(commandRole);

            var socket = Connect();

            if (socket == null)
            {
                SetLastError((long)SocketError.NotConnected);
                SetState((long)RedisConnectionState.Failed);

                throw new RedisFatalException(new SocketException((int)SocketError.NotConnected), RedisErrorCode.ConnectionError);
            }

            var task = socket.SendAsync(data, 0, data.Length)
                       .ContinueWith <RedisRawResponse>((asyncTask) =>
            {
                if (asyncTask.IsFaulted && asyncTask.Exception.IsSocketError())
                {
                    FreeAndNilSocket();
                    return(null);
                }

                if (asyncTask.IsCompleted && asyncTask.Result > 0)
                {
                    using (var reader = new RedisSingleResponseReader(Settings))
                        return(reader.Execute(socket));
                }
                return(null);
            });

            return(task.Result);
        }
示例#3
0
        private bool Receive(IList <RedisRequest> requests, RedisSocketContext context)
        {
            if (requests != null)
            {
                var requestCount = requests.Count;
                if (requestCount > 0)
                {
                    var socket = context.Socket;
                    if (socket.IsConnected())
                    {
                        using (var reader = new RedisSingleResponseReader(context.Settings))
                        {
                            for (var i = 0; i < requestCount; i++)
                            {
                                try
                                {
                                    var request = requests[i];
                                    if (ReferenceEquals(request, null))
                                    {
                                        continue;
                                    }

                                    var execResult = reader.Execute(socket);
                                    if (ReferenceEquals(execResult, null))
                                    {
                                        throw new RedisFatalException("Corrupted redis response data", RedisErrorCode.CorruptResponse);
                                    }

                                    execResult.HandleError();

                                    var rawObj = RedisRawObject.ToObject(execResult);
                                    if (ReferenceEquals(rawObj, null))
                                    {
                                        throw new RedisFatalException("Corrupted redis response data", RedisErrorCode.CorruptResponse);
                                    }

                                    request.ProcessResult(rawObj);
                                }
                                catch (Exception e)
                                {
                                    SetException(requests, e, i);
                                    throw;
                                }
                            }
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#4
0
        private RedisRawResponse ExecuteInternal(RedisSocketContext context, bool throwException = true, bool sendNotReceive = false)
        {
            if (context == null)
            {
                if (throwException)
                {
                    throw new RedisFatalException(new ArgumentNullException("context"), RedisErrorCode.MissingParameter);
                }
                return(null);
            }

            var socket = context.Socket;

            if (socket == null)
            {
                if (throwException)
                {
                    throw new RedisFatalException(new ArgumentNullException("context.Socket"), RedisErrorCode.MissingParameter);
                }
                return(null);
            }

            RedisRawResponse response = RedisVoidResponse.Void;

            if (sendNotReceive || !m_CommandType.HasFlag(RedisCommandType.SendAndReceive))
            {
                WriteTo(socket);
            }
            else
            {
                WriteTo(socket);
                using (var reader = new RedisSingleResponseReader(context.Settings))
                    response = reader.Execute(socket);

                if (ReferenceEquals(response, null) && throwException)
                {
                    throw new RedisException("Corrupted redis response data", RedisErrorCode.CorruptResponse);
                }
                response.HandleError();
            }
            return(response);
        }
        private void ProcessReceiveQueue()
        {
            var queue = m_ReceiveQ;

            if (queue == null)
            {
                return;
            }

            m_ReceiveGate.Reset();

            var idleTime  = 0;
            var idleStart = DateTime.MinValue;

            var request = (RedisAsyncRequest)null;

            try
            {
                using (var reader = new RedisSingleResponseReader(Settings))
                {
                    var idleTimeout = IdleTimeout;

                    while (Processing)
                    {
                        if (!queue.TryDequeue(out request))
                        {
                            if (m_ReceiveGate.Wait(SpinSleepTime))
                            {
                                m_ReceiveGate.Reset();
                            }
                            else
                            {
                                idleTime += SpinSleepTime;
                                if (idleTime >= idleTimeout)
                                {
                                    break;
                                }
                            }
                            continue;
                        }

                        using (request)
                        {
                            if (idleTime > 0)
                            {
                                var command = request.Command;
                                if (ReferenceEquals(command, null) || !command.IsHeartBeat)
                                {
                                    idleTime = 0;
                                }
                            }

                            try
                            {
                                var context = m_CurrentContext;
                                if (context == null)
                                {
                                    continue;
                                }

                                if (!request.IsCompleted)
                                {
                                    var socket = context.Socket;
                                    if (socket.IsConnected())
                                    {
                                        try
                                        {
                                            var execResult = reader.Execute(socket);
                                            if (ReferenceEquals(execResult, null))
                                            {
                                                throw new RedisFatalException("Corrupted redis response data", RedisErrorCode.CorruptResponse);
                                            }

                                            execResult.HandleError();

                                            var rawObj = RedisRawObject.ToObject(execResult);
                                            if (ReferenceEquals(rawObj, null))
                                            {
                                                throw new RedisFatalException("Corrupted redis response data", RedisErrorCode.CorruptResponse);
                                            }

                                            if (!request.ProcessResult(rawObj))
                                            {
                                                request.Cancel();
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            request.SetException(e);
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            { }
                        }
                    }
                }
            }
            finally
            {
                while (queue.TryDequeue(out request))
                {
                    try { request.Cancel(); }
                    catch (Exception) { }
                }
            }
        }