Пример #1
0
        /// <summary>
        /// 获取一个连接
        /// </summary>
        /// <returns></returns>
        public TCPClient Pop()
        {
            TCPClient tcpClient  = null;
            bool      needSupply = false;

            lock (this.pool)
            {
                if (ErrCount >= _InitCount)
                {
                    //首先尝试补充连接,如果失败,则返回null
                    needSupply = true;
                    if (!Supply())
                    {
                        return(null);
                    }
                }
            }

            if (this.semaphoreClients.WaitOne(20000)) //防止无法获取, 阻塞等待
            {
                lock (this.pool)
                {
                    tcpClient = this.pool.Dequeue();

                    if (!tcpClient.ValidateIpPort(RemoteIP, RemotePort))
                    {
                        try
                        {
                            tcpClient.Disconnect();
                            tcpClient.Connect(RemoteIP, RemotePort, tcpClient.ServerName);
                        }
                        catch (System.Exception ex)
                        {
                            ErrCount++;
                            ErrorClientStack.Push(tcpClient);
                            LastConnectErrorTime = TimeUtil.NowDateTime();
                            LogManager.WriteExceptionUseCache(ex.ToString());
                        }
                    }
                }
            }

            return(tcpClient);
        }
Пример #2
0
        public TCPClient Pop()
        {
            TCPClient tcpClient = null;

            lock (this.pool)
            {
                if (this.ErrCount >= this._InitCount)
                {
                    if (!this.Supply())
                    {
                        return(null);
                    }
                }
            }
            if (this.semaphoreClients.WaitOne(20000))
            {
                lock (this.pool)
                {
                    tcpClient = this.pool.Dequeue();
                    if (!tcpClient.ValidateIpPort(this.RemoteIP, this.RemotePort))
                    {
                        try
                        {
                            tcpClient.Disconnect();
                            tcpClient.Connect(this.RemoteIP, this.RemotePort, tcpClient.ServerName);
                        }
                        catch (Exception ex)
                        {
                            this.ErrCount++;
                            this.ErrorClientStack.Push(tcpClient);
                            this.LastConnectErrorTime = TimeUtil.NowDateTime();
                            LogManager.WriteExceptionUseCache(ex.ToString());
                        }
                    }
                }
            }
            return(tcpClient);
        }