Exemplo n.º 1
0
        /// <summary>
        /// Open connection to the Redis server
        /// </summary>
        /// <param name="millisecondsTimeout">Timeout to wait for connection (0 for no timeout)</param>
        /// <param name="readTimeout">Time to wait for reading (0 for no timeout)</param>
        /// <returns>True if connected</returns>
        public bool Connect(int millisecondsTimeout, int readTimeout = 0)
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            ActivityTracer.Verbose("Opening connection with {0} ms timeout", millisecondsTimeout);
            if (millisecondsTimeout > 0)
            {
                _socket
                .BeginConnect(Host, Port, null, null)
                .AsyncWaitHandle.WaitOne(millisecondsTimeout, true);
            }
            else
            {
                _socket.Connect(Host, Port);
            }

            if (_socket.Connected)
            {
                ActivityTracer.Info("Connected. Read timeout is {0}", readTimeout);
                _stream = new NetworkStream(_socket);
                if (readTimeout > 0)
                {
                    _stream.ReadTimeout = readTimeout;
                }
            }
            else
            {
                ActivityTracer.Info("Connection timed out");
                _socket.Close();
            }

            return(_socket.Connected);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get a synchronous RedisClient for blocking calls (e.g. BLPop, Subscriptions, Transactions, etc)
        /// </summary>
        /// <returns>RedisClient to be used in single thread context</returns>
        public RedisClient Clone()
        {
            ActivityTracer.Verbose("Cloning client");
            RedisClient client = new RedisClient(Host, Port, 0);

            if (_password != null)
            {
                client.Auth(_password);
            }
            return(client);
        }
Exemplo n.º 3
0
        public static RedisMessage ReadType(Stream stream)
        {
            RedisMessage type = (RedisMessage)stream.ReadByte();

            ActivityTracer.Verbose("Received response type: {0}", type);
            if (type == RedisMessage.Error)
            {
                throw new RedisException(ReadStatus(stream, false));
            }
            return(type);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Release resources used by the current RedisConnection
        /// </summary>
        public void Dispose()
        {
            if (_asyncTaskQueue != null)
            {
                _asyncTaskQueue.CompleteAdding();
            }

            if (_asyncReader != null)
            {
                try
                {
                    _asyncReader.Wait();
                }
                catch (AggregateException ae)
                {
                    throw ae;
                }
                _asyncReader.Dispose();
                _asyncReader = null;
            }

            if (_asyncTaskQueue != null)
            {
                _asyncTaskQueue.Dispose();
                _asyncTaskQueue = null;
            }

            ActivityTracer.Verbose("Closing connection stream");
            if (_stream != null)
            {
                _stream.Dispose();
            }

            ActivityTracer.Info("Closing connection");
            if (_socket != null)
            {
                _socket.Dispose();
            }

            if (_activity != null)
            {
                _activity.Dispose();
            }
            _activity = null;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Read response from server into a stream
        /// </summary>
        /// <param name="destination">The stream that will contain the contents of the server response.</param>
        /// <param name="bufferSize">Size of internal buffer used to copy streams</param>
        public void Read(Stream destination, int bufferSize)
        {
            using (new ActivityTracer("Read response to stream"))
            {
                ActivityTracer.Verbose("Buffer size is {0}", bufferSize);
                RedisMessage type = RedisReader.ReadType(_stream);

                if (type == RedisMessage.Error)
                {
                    throw new RedisException(RedisReader.ReadStatus(_stream, false));
                }

                if (type != RedisMessage.Bulk)
                {
                    throw new InvalidOperationException("Cannot stream from non-bulk response. Received: " + type);
                }

                RedisReader.ReadBulk(_stream, destination, bufferSize, false);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Read server response bytes into buffer and advance the server response stream (requires Buffering=true)
        /// </summary>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            using (new ActivityTracer("Read response to buffer"))
            {
                ActivityTracer.Verbose("Offset={0}; Count={1}", offset, count);
                if (offset > buffer.Length || count > buffer.Length)
                {
                    throw new InvalidOperationException("Buffer offset or count is larger than buffer");
                }

                if (!Buffering)
                {
                    ActivityTracer.Verbose("Not buffering; zeroing out buffer");
                    for (int i = offset; i < count; i++)
                    {
                        buffer[i] = 0;
                    }
                    return(0);
                }

                if (_bytesRemaining == 0)
                {
                    RedisMessage type = RedisReader.ReadType(_stream);

                    if (type == RedisMessage.Error)
                    {
                        throw new RedisException(RedisReader.ReadStatus(_stream, false));
                    }

                    if (type != RedisMessage.Bulk)
                    {
                        throw new InvalidOperationException("Cannot buffer from non-bulk response. Received: " + type);
                    }

                    _bytesRemaining = RedisReader.ReadInt(_stream, false);
                }

                ActivityTracer.Verbose("Bytes remaining: {0}", _bytesRemaining);

                int bytes_to_read = count;
                if (bytes_to_read > _bytesRemaining)
                {
                    bytes_to_read = (int)_bytesRemaining;
                }

                int bytes_read = 0;
                while (bytes_read < bytes_to_read)
                {
                    bytes_read += _stream.Read(buffer, offset + bytes_read, bytes_to_read - bytes_read);
                }

                _bytesRemaining -= bytes_read;

                if (_bytesRemaining == 0)
                {
                    RedisReader.ReadCRLF(_stream);
                    Buffering = false;
                }

                return(bytes_read);
            }
        }