private CommunicationPool <TKey, TItem> .EndpointConnectionPool GetEndpointPool(TKey key, TimeSpan timeout)
        {
            CommunicationPool <TKey, TItem> .EndpointConnectionPool endpointConnectionPool = null;
            List <TItem> tItems = null;

            lock (this.ThisLock)
            {
                if (!this.endpointPools.TryGetValue(key, out endpointConnectionPool))
                {
                    tItems = this.PruneIfNecessary();
                    endpointConnectionPool = this.CreateEndpointConnectionPool(key);
                    this.endpointPools.Add(key, endpointConnectionPool);
                }
            }
            Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DebugAssert(endpointConnectionPool != null, "EndpointPool must be non-null at this point");
            if (tItems != null && tItems.Count > 0)
            {
                TimeoutHelper timeoutHelper = new TimeoutHelper(TimeoutHelper.Divide(timeout, 2));
                for (int i = 0; i < tItems.Count; i++)
                {
                    endpointConnectionPool.CloseIdleConnection(tItems[i], timeoutHelper.RemainingTime());
                }
            }
            return(endpointConnectionPool);
        }
        public void AddConnection(TKey key, TItem connection, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            CommunicationPool <TKey, TItem> .EndpointConnectionPool endpointPool = this.GetEndpointPool(key, timeoutHelper.RemainingTime());
            endpointPool.AddConnection(connection, timeoutHelper.RemainingTime());
        }
        public bool Close(TimeSpan timeout)
        {
            bool flag;

            lock (this.ThisLock)
            {
                if (this.openCount > 0)
                {
                    CommunicationPool <TKey, TItem> communicationPool = this;
                    communicationPool.openCount = communicationPool.openCount - 1;
                    if (this.openCount != 0)
                    {
                        flag = false;
                    }
                    else
                    {
                        this.OnClose(timeout);
                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }
            }
            return(flag);
        }
        public void ReturnConnection(TKey key, TItem connection, bool connectionIsStillGood, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            CommunicationPool <TKey, TItem> .EndpointConnectionPool endpointPool = this.GetEndpointPool(key, timeoutHelper.RemainingTime());
            endpointPool.ReturnConnection(connection, connectionIsStillGood, timeoutHelper.RemainingTime());
        }
        public TItem TakeConnection(EndpointAddress address, Uri via, TimeSpan timeout, out TKey key)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            key = this.GetPoolKey(address, via);
            CommunicationPool <TKey, TItem> .EndpointConnectionPool endpointPool = this.GetEndpointPool(key, timeoutHelper.RemainingTime());
            return(endpointPool.TakeConnection(timeoutHelper.RemainingTime()));
        }
        public bool TryOpen()
        {
            bool flag;

            lock (this.ThisLock)
            {
                if (this.openCount > 0)
                {
                    CommunicationPool <TKey, TItem> communicationPool = this;
                    communicationPool.openCount = communicationPool.openCount + 1;
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            return(flag);
        }
        private List <TItem> PruneIfNecessary()
        {
            List <TItem> tItems = null;
            CommunicationPool <TKey, TItem> communicationPool = this;

            communicationPool.pruneAccrual = communicationPool.pruneAccrual + 1;
            if (this.pruneAccrual > 30)
            {
                this.pruneAccrual = 0;
                tItems            = new List <TItem>();
                foreach (CommunicationPool <TKey, TItem> .EndpointConnectionPool value in this.endpointPools.Values)
                {
                    value.Prune(tItems);
                }
                List <TKey> tKeys = null;
                foreach (KeyValuePair <TKey, CommunicationPool <TKey, TItem> .EndpointConnectionPool> endpointPool in this.endpointPools)
                {
                    if (!endpointPool.Value.CloseIfEmpty())
                    {
                        continue;
                    }
                    if (tKeys == null)
                    {
                        tKeys = new List <TKey>();
                    }
                    tKeys.Add(endpointPool.Key);
                }
                if (tKeys != null)
                {
                    for (int i = 0; i < tKeys.Count; i++)
                    {
                        this.endpointPools.Remove(tKeys[i]);
                    }
                }
            }
            return(tItems);
        }
 public EndpointConnectionPool(CommunicationPool <TKey, TItem> parent, TKey key)
 {
     this.key             = key;
     this.parent          = parent;
     this.busyConnections = new List <TItem>();
 }