static void BeginReadReplies(ClientAsyncReadState state)
        {
            var client = state.WorkSocket;

            client.BeginReceive(state.Buffer, 0, ClientAsyncReadState.BufferSize, SocketFlags.None,
              new AsyncCallback(EndReadReplies), state);

            var self = (RedisCommandQueue)state.CallbackArg;
            self._receiveMre.WaitOne();
            self._receiveMre.Reset();
        }
        private ClientAsyncReadState CreateAsyncReadState()
        {
            int expectedReplies = _receiveQueue.Count;
            var state = new ClientAsyncReadState
            {
                WorkSocket = Socket.Socket,
                ExpectedReplies = expectedReplies,
                CallbackArg = this
            };

            bool execExpected = false;
            state.ValueReceived += (stateObject, data, value) => HandleValueReceived(stateObject, data, value, ref execExpected);

            return state;
        }
        private static void HandleValueReceived(ClientAsyncReadState state, object data, RedisValue value, ref bool execExpected)
        {
            var self = (RedisCommandQueue)data;

            var dequeued = self._receiveQueue.Peek();

            if (value.Type == RedisValueType.Error)
            {
                if (IsAuth(dequeued))
                {
                    self._receiveQueue.Dequeue();
                    throw new RedisAuthenticationException(value.Text);
                }
                throw new RedisClientException(value.Text);
            }

            var socket = self.Socket;

            if (IsMulti(dequeued))
            {
                execExpected = true;
                // value here should be "OK"
                dequeued.Value = value;
                return;
            }

            if (IsExec(dequeued))
            {
                if (!execExpected)
                {
                    // throw
                    throw new RedisClientException("EXEC found without a matching MULTI");
                }
                execExpected = false;
                self._multiCount--;
            }
            else if (execExpected)
            {
                // value should be "QUEUED" for each command between multi and exec
                return;
            }

            self._receiveQueue.Dequeue();

            if (!socket.IsAuthorized && IsAuth(dequeued))
            {
                if (value.Text != "OK")
                    throw new RedisAuthenticationException("Invalid credentials for node : " + self.Socket);
                socket.IsAuthorized = true;
                dequeued.Value = value;
            }
            else if (socket.CurrentDb != self.CurrentDB && IsSelect(dequeued))
            {
                socket.CurrentDb = self.CurrentDB;
            }

            dequeued.Value = value;            
        }
Beispiel #4
0
        public static void AsyncRead(this PooledSocket client, 
                                        ClientAsyncReadState.ValueReceivedHandler hander, object arg)
        {
            try
            {
                // Create the state object.
                var state = new ClientAsyncReadState
                                {
                                    CallbackArg = arg,
                                    WorkSocket = client.Socket
                                };

                if (hander != null)
                    state.ValueReceived += hander;

                // Begin receiving the data from the remote device.
                client.Socket.BeginReceive(state.Buffer, 0, ClientAsyncReadState.BufferSize, 0,
                    new AsyncCallback(AsyncReadCallback), state);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }