Esempio n. 1
0
        /// <summary>
        /// Creates a new SockIO obj for the given server.
        ///
        ///If server fails to connect, then return null and do not try
        ///again until a duration has passed.  This duration will grow
        ///by doubling after each failed attempt to connect.
        /// </summary>
        /// <param name="host">host:port to connect to</param>
        /// <returns>SockIO obj or null if failed to create</returns>
        protected SockIO CreateSocket(string host)
        {
            SockIO socket = null;

            // if host is dead, then we don't need to try again
            // until the dead status has expired
            // we do not try to put back in if failover is off
            if (_failover && _hostDead.ContainsKey(host) && _hostDeadDuration.ContainsKey(host))
            {
                DateTime store  = (DateTime)_hostDead[host];
                long     expire = ((long)_hostDeadDuration[host]);

                if ((store.AddMilliseconds(expire)) > DateTime.Now)
                {
                    return(null);
                }
            }

            try
            {
                socket = new SockIO(this, host, _socketTimeout, _socketConnectTimeout, _nagle);

                if (!socket.IsConnected)
                {
                    //if(Log.IsErrorEnabled)
                    //{
                    //    Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host));
                    //}
                    try
                    {
                        socket.TrueClose();
                    }
                    catch                    //(SocketException ex)
                    {
                        //if(Log.IsErrorEnabled)
                        //{
                        //    Log.Error(GetLocalizedString("failed to close socket on host").Replace("$$Host$$", host), ex);
                        //}
                        socket = null;
                    }
                }
            }
            catch            //(SocketException ex)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
                //}
                socket = null;
            }
            //catch(ArgumentException ex)
            //{
            //    if(Log.IsErrorEnabled)
            //    {
            //        Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
            //    }
            //    socket = null;
            //}
            //catch(IOException ex)
            //{
            //    if(Log.IsErrorEnabled)
            //    {
            //        Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
            //    }
            //    socket = null;
            //}

            // if we failed to get socket, then mark
            // host dead for a duration which falls off
            if (socket == null)
            {
                DateTime now = DateTime.Now;
                _hostDead[host] = now;
                long expire = (_hostDeadDuration.ContainsKey(host)) ? (((long)_hostDeadDuration[host]) * 2) : 100;
                _hostDeadDuration[host] = expire;
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("ignoring dead host").Replace("$$Host$$", host).Replace("$$Expire$$", expire.ToString(new NumberFormatInfo())));
                //}

                // also clear all entries for this host from availPool
                ClearHostFromPool(_availPool, host);
            }
            else
            {
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("created socket").Replace("$$ToString$$", socket.ToString()).Replace("$$Host$$", host));
                //}
                _hostDead.Remove(host);
                _hostDeadDuration.Remove(host);
                if (_buckets.BinarySearch(host) < 0)
                {
                    _buckets.Add(host);
                }
            }

            return(socket);
        }