예제 #1
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="proxy"></param>
        //private void TestAndUpdateProxy(IPProxy proxy)
        //{
        //    var isValid = false;
        //    proxy.CheckStatus = IPProxy.CheckStatusEnum.Checking;
        //    if (Helper.ProxyIsGood(proxy.IPAddress, proxy.PortNo))
        //    {
        //        proxy.CheckerTokenId = _checkerTokenId;
        //        proxy.LastChecked = DateTime.Now;
        //        proxy.CheckStatus = IPProxy.CheckStatusEnum.Checked;

        //        isValid = true;
        //    }
        //    else proxy.CheckStatus = IPProxy.CheckStatusEnum.CheckedInvalid;
        //    OnIPProxyChecked?.Invoke(this, new IPProxyCheckedEventArgs(isValid, proxy));
        //}

        /// <summary>
        ///
        /// </summary>
        protected override void ServiceActionCallback()
        {
            if (ProxyBucket == null || ProxyBucket.Count == 0)
            {
                return;
            }
            var opnPrxs = GrabOpenProxies(_workersCount);

            if (!opnPrxs.Any())
            {
                return;
            }

            var prxTaskList = new List <Task>();

            foreach (var prx in opnPrxs)
            {
                var tsk = new Task(() =>
                {
                    var isValid = Helper.TestIPProxy2(prx);
                    OnIPProxyChecked?.Invoke(this, new IPProxyCheckedEventArgs(isValid, prx));
                });
                tsk.Start();
                prxTaskList.Add(tsk);
            }
            Task.WaitAll(prxTaskList.ToArray());

            lock (ProxyBucket)
            {
                var removedCnt = ProxyBucket.Remove(proxy => proxy.CheckStatus == IPProxy.CheckStatusEnum.CheckedInvalid);
                OnIPProxiesChecked?.Invoke(this, new IPProxiesCheckedEventArgs(removedCnt));
            }

            if (ProxyBucket.Any(p => p.CheckerTokenId != _checkerTokenId))
            {
                return;
            }

            // resume to repeat in 1 minutes
            base.Suspend(new TimeSpan(0, 1, 0));

            // renew the process token;
            _checkerTokenId = Guid.NewGuid();
        }
        /// <summary>
        ///
        /// </summary>
        private void RunChecker()
        {
            if (_proxyCheckerSvc == null)
            {
                _proxyCheckerSvc = new IPProxyChecker(IPCheckWorkerCounts)
                {
                    ProxyBucket = ProxyBucket
                }
            }
            ;

            _proxyCheckerSvc.OnIPProxyChecked += (s, e) =>
            {
                OnIPProxyChecked?.Invoke(this, e);

                // fire ready event
                if (!e.IsValid || ProxyBucket.Count <= 0 || _firedReady)
                {
                    return;
                }

                _firedReady = true;
                OnIPProxyReady?.Invoke(this, new EventArgs());
            };

            _proxyCheckerSvc.OnIPProxiesChecked += (s, e) =>
            {
                OnIPProxiesChecked?.Invoke(this, e);
            };

            Task.Delay(1000)
            .ContinueWith(t =>
            {
                _proxyCheckerSvc.Resume();
                _checkerIsRunning = true;
            });
        }