Esempio n. 1
0
        // 这个方法提供实际的计算流程.它在worker线程上被执行.
        private void CalculateWorker(
            ref List <ProxyInfo> proxyList,
            AsyncOperation asyncOp)
        {
            _ProxyList   = proxyList;
            _ProxyListOK = new List <ProxyInfo>();
            Exception e = null;

            // 检查taskId是否被取消.因为操作可能已经在之前被预先取消了.
            if (!TaskCanceled(asyncOp.UserSuppliedState))
            {
                try
                {
                    string currentIP = ProxyUtility.GetCurrentIP_RegexPage(ConnectValidatePage(asyncOp, null), _ProxyValidateUrlInfo.RegexString);
                    Natsuhime.Events.MessageEventArgs me = new Natsuhime.Events.MessageEventArgs("", string.Format("[校验]{0}[当前IP]", currentIP), "", asyncOp.UserSuppliedState);
                    asyncOp.Post(this.onStatusChangeDelegate, me);
                    Validate(currentIP, asyncOp);
                }
                catch (Exception ex)
                {
                    e = ex;
                }
            }

            this.CompletionMethod(
                _ProxyListOK,
                e,
                TaskCanceled(asyncOp.UserSuppliedState),
                asyncOp);
        }
Esempio n. 2
0
        private void Validate(string currentIP, AsyncOperation asyncOp)
        {
            if (currentIP == null || currentIP.Trim() == string.Empty)
            {
                throw new ArgumentNullException("currentIP");
            }

            Natsuhime.Events.MessageEventArgs e = null;
            while (true)
            {
                ProxyInfo info = GetProxy();
                if (info == null)
                {
                    break;
                }
                e = new Natsuhime.Events.MessageEventArgs("", string.Format("[校验]{0}:{1}", info.Address, info.Port), "", asyncOp.UserSuppliedState);
                asyncOp.Post(this.onStatusChangeDelegate, e);

                string returnData;
                try
                {
                    returnData = ConnectValidatePage(asyncOp, info);
                }
                catch (Exception ex)
                {
                    returnData = string.Empty;
                }
                if (returnData == string.Empty)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1} - Failed", info.Address, info.Port));

                    e = new Natsuhime.Events.MessageEventArgs("", string.Format("[失败]{0}:{1}", info.Address, info.Port), "", asyncOp.UserSuppliedState);
                    asyncOp.Post(this.onStatusChangeDelegate, e);
                    continue;
                }
                if (currentIP == ProxyUtility.GetCurrentIP_RegexPage(returnData, _ProxyValidateUrlInfo.RegexString))
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1} - Bad", info.Address, info.Port));

                    e = new Natsuhime.Events.MessageEventArgs("", string.Format("[透明]{0}:{1}", info.Address, info.Port), "", asyncOp.UserSuppliedState);
                    asyncOp.Post(this.onStatusChangeDelegate, e);
                    continue;
                }

                Monitor.Enter(_ProxyListOK);
                _ProxyListOK.Add(info);
                Monitor.Exit(_ProxyListOK);
                System.Diagnostics.Debug.WriteLine(string.Format("{0}:{1} - OK", info.Address, info.Port));

                e = new Natsuhime.Events.MessageEventArgs("", string.Format("[成功]{0}:{1}", info.Address, info.Port), "", asyncOp.UserSuppliedState);
                asyncOp.Post(this.onStatusChangeDelegate, e);
            }
        }