Ejemplo 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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        /// <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);
        }