コード例 #1
0
ファイル: RedisSentinelClient.cs プロジェクト: bapti/csredis
 /// <summary>
 /// Instantiate a new instance of the RedisSentinelClient class
 /// </summary>
 /// <param name="host">Sentinel server hostname or IP</param>
 /// <param name="port">Sentinel server port</param>
 /// <param name="timeout">Connection timeout in milliseconds (0 for no timeout)</param>
 public RedisSentinelClient(string host, int port, int timeout)
 {
     _activity   = new ActivityTracer("New Redis Sentinel client");
     _connection = new RedisConnection(host, port);
     _connection.Connect(timeout);
     _subscriptionHandler = new RedisSubscriptionHandler(_connection);
     _subscriptionHandler.SubscriptionChanged  += OnSubscriptionChanged;
     _subscriptionHandler.SubscriptionReceived += OnSubscriptionReceived;
 }
コード例 #2
0
ファイル: Handlers.cs プロジェクト: TheCloudlessSky/csredis
        public void Start(bool captureResults = true)
        {
            _activity       = new ActivityTracer("Begin transaction");
            Active          = true;
            _captureResults = captureResults;
            string status = _connection.Call(RedisReader.ReadStatus, "MULTI");

            OnTransactionStarted(status);
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: Handlers.cs プロジェクト: TheCloudlessSky/csredis
 public void Start(bool captureResults = true)
 {
     _asTransaction  = false;
     _activity       = new ActivityTracer("Begin pipeline");
     _captureResults = captureResults;
     if (Active)
     {
         throw new InvalidOperationException("Already in pipeline mode");
     }
     Active = true;
 }
コード例 #5
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);
        }
コード例 #6
0
 /// <summary>
 /// Release resoures used by the current RedisSentinelClient
 /// </summary>
 public void Dispose()
 {
     if (_connection != null)
     {
         _connection.Dispose();
     }
     if (_activity != null)
     {
         _activity.Dispose();
     }
     _activity = null;
 }
コード例 #7
0
 /// <summary>
 /// Create new instance of subscribe-only RedisClient
 /// </summary>
 /// <param name="host">Redis server host or IP</param>
 /// <param name="port">Redis server port</param>
 /// <param name="password">Redis server password</param>
 public RedisSubscriptionClient(string host, int port, string password = null)
 {
     _activity = new ActivityTracer("New Redis subscription client");
     _connection = new RedisConnection(host, port);
     _connection.Connect(0, 1000);
     if (!String.IsNullOrEmpty(password))
     {
         var cmd = RedisCommand.Auth(password);
         _connection.Call(cmd.Parser, cmd.Command, cmd.Arguments);
     }
     _readCancel = new CancellationTokenSource();
     _reader = Task.Factory.StartNew(Read_Task);
     _callbackDispatchers = new Dictionary<string, RedisSubscriptionDispatcher>();
 }
コード例 #8
0
ファイル: Subscriptions.cs プロジェクト: bapti/csredis
 /// <summary>
 /// Create new instance of subscribe-only RedisClient
 /// </summary>
 /// <param name="host">Redis server host or IP</param>
 /// <param name="port">Redis server port</param>
 /// <param name="password">Redis server password</param>
 public RedisSubscriptionClient(string host, int port, string password = null)
 {
     _activity   = new ActivityTracer("New Redis subscription client");
     _connection = new RedisConnection(host, port);
     _connection.Connect(0, 1000);
     if (!String.IsNullOrEmpty(password))
     {
         var cmd = RedisCommand.Auth(password);
         _connection.Call(cmd.Parser, cmd.Command, cmd.Arguments);
     }
     _readCancel          = new CancellationTokenSource();
     _reader              = Task.Factory.StartNew(Read_Task);
     _callbackDispatchers = new Dictionary <string, RedisSubscriptionDispatcher>();
 }
コード例 #9
0
ファイル: Subscriptions.cs プロジェクト: bapti/csredis
        /// <summary>
        /// Instantiate a new instance of the RedisSubscriptionChannel class
        /// </summary>
        /// <param name="type">The type of channel response</param>
        /// <param name="response">Redis multi-bulk response</param>
        public RedisSubscriptionChannel(RedisSubscriptionResponseType type, object[] response)
        {
            switch (type)
            {
            case RedisSubscriptionResponseType.Subscribe:
            case RedisSubscriptionResponseType.Unsubscribe:
                Channel = response[1] as String;
                break;

            case RedisSubscriptionResponseType.PSubscribe:
            case RedisSubscriptionResponseType.PUnsubscribe:
                Pattern = response[1] as String;
                break;
            }
            Count = (long)response[2];
            ActivityTracer.Info("Subscription response: type={0}; channel={1}; pattern={2}; count={3}", Type, Channel, Pattern, Count);
        }
コード例 #10
0
ファイル: RedisConnection.cs プロジェクト: bapti/csredis
        /// <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;
        }
コード例 #11
0
        /// <summary>
        /// Release resources used by the current RedisClientAsync instance
        /// </summary>
        public void Dispose()
        {
            if (_connection != null)
            {
                _connection.Dispose();
            }

            if (_subscriptionClient.IsValueCreated)
            {
                _subscriptionClient.Value.Dispose();
            }

            if (_activity != null)
            {
                _activity.Dispose();
            }
            _activity = null;
        }
コード例 #12
0
ファイル: Subscriptions.cs プロジェクト: bapti/csredis
        /// <summary>
        /// Instantiate a new instance of the RedisSubscriptionMessage class
        /// </summary>
        /// <param name="type">The type of message response</param>
        /// <param name="response">Redis multi-bulk response</param>
        public RedisSubscriptionMessage(RedisSubscriptionResponseType type, object[] response)
        {
            switch (type)
            {
            case RedisSubscriptionResponseType.Message:
                Channel = response[1] as String;
                Body    = response[2] as String;
                break;

            case RedisSubscriptionResponseType.PMessage:
                Pattern = response[1] as String;
                Channel = response[2] as String;
                Body    = response[3] as String;
                break;
            }

            ActivityTracer.Info("Subscription message: type={0}; channel={1}; pattern={2}; body={3}", Type, Channel, Pattern, Body);
        }
コード例 #13
0
ファイル: RedisConnection.cs プロジェクト: bapti/csredis
        /// <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);
            }
        }
コード例 #14
0
        /// <summary>
        /// Release resources used by the current RedisSubscriptionClient
        /// </summary>
        public void Dispose()
        {
            if (_readCancel != null)
                _readCancel.Cancel();

            if (_reader != null)
            {
                _reader.Wait();
                _reader.Dispose();
            }
            if (_connection != null)
                _connection.Dispose();

            if (_callbackDispatchers != null)
                _callbackDispatchers.Clear();

            _count = 0;

            if (_activity != null)
                _activity.Dispose();
            _activity = null;
        }
コード例 #15
0
ファイル: RedisConnection.cs プロジェクト: bapti/csredis
        /// <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);
            }
        }