/// <summary>
        /// 移除索引的连接
        /// </summary>
        /// <param name="pluginname">插件名称</param>
        /// <param name="Index"></param>
        /// <returns></returns>
        public bool RemovePoolAt(string pluginname, int?Index)
        {
            bool flag = false;

            lock (lockhelper)
            {
                int len = poollist.Count;
                for (int i = 0; i < poollist.Count; i++)
                {
                    ClientLink obj = poollist[i];
                    if (Index == obj.Index && pluginname == obj.PluginName)
                    {
                        obj.Dispose();
                        poollist.Remove(obj);
                        if (countNumsDic.ContainsKey(obj.PluginName))
                        {
                            countNumsDic[obj.PluginName] = countNumsDic[obj.PluginName] == 0 ? 0 : countNumsDic[obj.PluginName] - 1;
                        }
                        if (usedNumsDic.ContainsKey(obj.PluginName))
                        {
                            usedNumsDic[obj.PluginName] = usedNumsDic[obj.PluginName] == 0 ? 0 : usedNumsDic[obj.PluginName] - 1;
                        }

                        flag = true;
                        i--;
                        break;
                    }
                }
            }

            return(flag);
        }
        /// <summary>
        /// 处理连接池
        /// </summary>
        private void ReapPool()
        {
            lock (lockhelper)
            {
                //string content = "";
                for (int i = 0; i < poollist.Count; i++)
                {
                    ClientLink obj = poollist[i];
                    if ((!obj.IsUsed && DateTime.Now - obj.LastUsedTime > this.wcfFailureTime) || (obj.State != CommunicationState.Opened))
                    {
                        obj.Dispose();
                        poollist.Remove(obj);
                        if (countNumsDic.ContainsKey(obj.PluginName))
                        {
                            countNumsDic[obj.PluginName] = countNumsDic[obj.PluginName] == 0 ? 0 : countNumsDic[obj.PluginName] - 1;
                        }
                        if (usedNumsDic.ContainsKey(obj.PluginName))
                        {
                            usedNumsDic[obj.PluginName] = usedNumsDic[obj.PluginName] == 0 ? 0 : usedNumsDic[obj.PluginName] - 1;
                        }

                        i--;
                    }
                }
                //write("F:\\2.txt", content);
            }
        }
Example #3
0
        /// <summary>
        /// 踢掉一个非当前契约的空闲连接
        /// </summary>
        /// <returns></returns>
        public bool RemovePoolOneNotAt(string pluginname, out ClientLink clientlink, out int?Index)
        {
            bool flag = false;

            Index      = null;
            clientlink = null;
            lock (lockhelper)
            {
                int len = poollist.Count;
                //如果池子满了,先踢出一个非当前创建契约的连接
                if (poollist.Count >= this.wcfMaxPoolSize)
                {
                    for (int i = 0; i < poollist.Count; i++)
                    {
                        ClientLink obj = poollist[i];
                        if (!obj.IsUsed && obj.PluginName != pluginname)
                        {
                            obj.Dispose();

                            poollist.Remove(obj);
                            if (countNumsDic.ContainsKey(obj.PluginName))
                            {
                                countNumsDic[obj.PluginName] = countNumsDic[obj.PluginName] == 0 ? 0 : countNumsDic[obj.PluginName] - 1;
                            }
                            if (usedNumsDic.ContainsKey(obj.PluginName))
                            {
                                usedNumsDic[obj.PluginName] = usedNumsDic[obj.PluginName] == 0 ? 0 : usedNumsDic[obj.PluginName] - 1;
                            }

                            flag = true;
                            i--;
                            break;
                        }
                    }
                }
                //增加一个连接到池子
                if (poollist.Count < this.wcfMaxPoolSize)
                {
                    clientlink = new ClientLink(pluginname);
                    clientlink.CreateConnection();
                    index                   = index >= Int32.MaxValue ? 1 : index + 1;
                    Index                   = index;
                    clientlink.Index        = index;
                    clientlink.UsedNums     = 1;
                    clientlink.CreatedTime  = DateTime.Now;
                    clientlink.LastUsedTime = DateTime.Now;
                    clientlink.IsUsed       = true;
                    //obj.Scope = new OperationContextScope(((IClientChannel)channel));
                    poollist.Add(clientlink);
                    countNumsDic[clientlink.PluginName] = countNumsDic.ContainsKey(clientlink.PluginName) ? countNumsDic[clientlink.PluginName] + 1 : 1;
                    usedNumsDic[clientlink.PluginName]  = usedNumsDic.ContainsKey(clientlink.PluginName) ? usedNumsDic[clientlink.PluginName] + 1 : 1;
                    flag = true;
                }
            }

            return(flag);
        }
        /// <summary>
        /// 从连接池中获取一个连接
        /// </summary>
        /// <returns></returns>
        public ClientLink GetClientLink(string pluginname)
        {
            //先做一次清理
            //ReapPool();

            ClientLink clientlink = null;

            if (GetFreePoolNums(pluginname) > 0)
            {
                lock (lockhelper)
                {
                    if (GetFreePoolNums(pluginname) > 0)
                    {
                        for (int i = 0; i < poollist.Count; i++)
                        {
                            ClientLink obj = poollist[i];
                            if (!obj.IsUsed && DateTime.Now - obj.LastUsedTime < this.wcfFailureTime && pluginname == obj.PluginName)
                            {
                                if (obj.State == CommunicationState.Opened)
                                {
                                    obj.IsUsed = true;
                                    obj.UsedNums++;
                                    obj.LastUsedTime            = DateTime.Now;
                                    usedNumsDic[obj.PluginName] = usedNumsDic.ContainsKey(obj.PluginName) ? usedNumsDic[obj.PluginName] + 1 : 1;

                                    clientlink = obj;
                                    break;
                                }
                                else//如果当前连接无效,则清理出连接池
                                {
                                    obj.Dispose();
                                    poollist.Remove(obj);
                                    if (countNumsDic.ContainsKey(obj.PluginName))
                                    {
                                        countNumsDic[obj.PluginName] = countNumsDic[obj.PluginName] == 0 ? 0 : countNumsDic[obj.PluginName] - 1;
                                    }
                                    if (usedNumsDic.ContainsKey(obj.PluginName))
                                    {
                                        usedNumsDic[obj.PluginName] = usedNumsDic[obj.PluginName] == 0 ? 0 : usedNumsDic[obj.PluginName] - 1;
                                    }
                                    i--;
                                }
                            }
                        }
                    }
                }
            }

            return(clientlink);
        }