csharp/2881099/csredis/src/CSRedisCore/RedisSentinelClient.cs

RedisSentinelClient.cs
using CSRedis.Internal;
using CSRedis.Internal.Commands;
using CSRedis.Internal.IO;
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using System.Text;

namespace CSRedis
{
    /// 
    /// Represents a client connection to a Redis sentinel instance
    /// 
    public partial clast RedisSentinelClient : IDisposable
    {
        const int DefaultPort = 26379;
        const bool DefaultSSL = false;
        const int DefaultConcurrency = 1000;
        const int DefaultBufferSize = 1024;
        readonly RedisConnector _connector;
        readonly SubscriptionListener _subscription;

        /// 
        /// Occurs when a subscription message is received
        /// 
        public event EventHandler SubscriptionReceived;

        /// 
        /// Occurs when a subscription channel is added or removed
        /// 
        public event EventHandler SubscriptionChanged;

        /// 
        /// Occurs when the connection has sucessfully reconnected
        /// 
        public event EventHandler Reconnected;

        /// 
        /// Get the Redis sentinel hostname
        /// 
        public string Host { get { return GetHost(); } }

        /// 
        /// Get the Redis sentinel port
        /// 
        public int Port { get { return GetPort(); } }

        /// 
        /// Get a value indicating whether the Redis sentinel client is connected to the server
        /// 
        public bool Connected { get { return _connector.IsConnected; } }

        /// 
        /// Get the string encoding used to communicate with the server
        /// 
        public Encoding Encoding { get { return _connector.Encoding; } }

        /// 
        /// Get or set the connection read timeout (milliseconds)
        /// 
        public int ReceiveTimeout
        {
            get { return _connector.ReceiveTimeout; }
            set { _connector.ReceiveTimeout = value; }
        }

        /// 
        /// Get or set the connection send timeout (milliseconds)
        /// 
        public int SendTimeout
        {
            get { return _connector.SendTimeout; }
            set { _connector.SendTimeout = value; }
        }

        /// 
        /// Get or set the number of times to attempt a reconnect after a connection fails
        /// 
        public int ReconnectAttempts
        {
            get { return _connector.ReconnectAttempts; }
            set { _connector.ReconnectAttempts = value; }
        }

        /// 
        /// Get or set the amount of time to wait between reconnect attempts
        /// 
        public int ReconnectWait
        {
            get { return _connector.ReconnectWait; }
            set { _connector.ReconnectWait = value; }
        }

        /// 
        /// Create a new RedisSentinelClient using default port and encoding
        /// 
        /// Redis sentinel hostname
        public RedisSentinelClient(string host)
            : this(host, DefaultPort)
        { }

        /// 
        /// Create a new RedisSentinelClient using default encoding
        /// 
        /// Redis sentinel hostname
        /// Redis sentinel port
        public RedisSentinelClient(string host, int port)
            : this(host, port, DefaultSSL)
        { }

        /// 
        /// Create a new RedisSentinelClient using default encoding
        /// 
        /// Redis sentinel hostname
        /// Redis sentinel port
        /// Set to true if remote Redis server expects SSL
        public RedisSentinelClient(string host, int port, bool ssl)
            : this(new RedisSocket(ssl), new DnsEndPoint(host, port), DefaultConcurrency, DefaultBufferSize)
        { }

        internal RedisSentinelClient(IRedisSocket socket, EndPoint endpoint)
            : this(socket, endpoint, DefaultConcurrency, DefaultBufferSize)
        { }

        internal RedisSentinelClient(IRedisSocket socket, EndPoint endpoint, int concurrency, int bufferSize)
        {
            _connector = new RedisConnector(endpoint, socket, concurrency, bufferSize);
            _subscription = new SubscriptionListener(_connector);

            _subscription.MessageReceived += OnSubscriptionReceived;
            _subscription.Changed += OnSubscriptionChanged;
            _connector.Connected += OnConnectionReconnected;
        }

        /// 
        /// Release resoures used by the current RedisSentinelClient
        /// 
        public void Dispose()
        {
            if (_connector != null)
                _connector.Dispose();
        }

        void OnSubscriptionReceived(object sender, RedisSubscriptionReceivedEventArgs args)
        {
            if (SubscriptionReceived != null)
                SubscriptionReceived(this, args);
        }

        void OnSubscriptionChanged(object sender, RedisSubscriptionChangedEventArgs args)
        {
            if (SubscriptionChanged != null)
                SubscriptionChanged(this, args);
        }

        void OnConnectionReconnected(object sender, EventArgs args)
        {
            if (Reconnected != null)
                Reconnected(this, args);
        }

        string GetHost()
        {
            if (_connector.EndPoint is IPEndPoint)
                return (_connector.EndPoint as IPEndPoint).Address.ToString();
            else if (_connector.EndPoint is DnsEndPoint)
                return (_connector.EndPoint as DnsEndPoint).Host;
            else
                return null;
        }

        int GetPort()
        {
            if (_connector.EndPoint is IPEndPoint)
                return (_connector.EndPoint as IPEndPoint).Port;
            else if (_connector.EndPoint is DnsEndPoint)
                return (_connector.EndPoint as DnsEndPoint).Port;
            else
                return -1;
        }
    }
}