Beispiel #1
0
        /// <summary>
        /// 创建一个连接,并压入队列
        /// </summary>
        /// <param name="pollCapacity"></param>
        /// <param name="isForceCreate"></param>
        /// <returns></returns>
        private INTCPConnection CreatOneConnectionToPool(bool polling, int pollCapacity, bool isForceCreate = false)
        {
            INTCPConnection driver = null;//SingleConnectionCable

            if (isForceCreate == false && _hasInitDriverCount > this._config.PoolingMaxSize)
            {
                return(driver);
                //throw new NTcpException("超过最大连接池设置的数目!", ErrorCode.OverPoolingSize);
            }

            string address = _config.Address;
            int    port    = _config.Port;

            lock (_locker)
            {
                driver = CreatNewConnection(address, port, polling, pollCapacity);
                if (null != driver)
                {
                    //尝试打开驱动连接
                    driver.Connect(_config.TimeOut * 1000, true);
                }
                _driverQueue.Enqueue(driver);

                if (isForceCreate == false)
                {
                    _hasInitDriverCount += 1;
                }
            }


            return(driver);
        }
        /// <summary>
        /// 打开连接
        /// </summary>
        public void Open()
        {
            if (null != this.driver && State == ConnectionState.Open)
            {
                return;
            }

            try
            {
                var currentSettings = new WebCrawlerConnection {
                    Address        = this.IPAddress,
                    Port           = this.Port,
                    TimeOut        = this.TimeOut,
                    Pooling        = this.Pooling,
                    PoolingMinSize = this.PoolingMinSize,
                    PoolingMaxSize = this.PoolingMaxSize,
                };
                SoapTcpPool pool = SoapTcpPool.GetPool(currentSettings);

                if (null != pool)
                {
                    driver = pool.GetConnection();
                }
                if (driver == null)
                {
                    driver = SoapTcpPool.CreatNewConnection(this.IPAddress, this.Port, this.Pooling, this.PoolingMinSize);
                }
                if (!driver.Connected)
                {
                    driver.Connect(this.TimeOut * 1000);
                }

                this.State = ConnectionState.Open;
            }
            catch (Exception ex)
            {
                this.State = ConnectionState.Closed;
                throw ex;
            }
        }