コード例 #1
0
ファイル: NetConnUtils.cs プロジェクト: WildGenie/nettools
        static public void ConfirmIpAddressAsync()
        {
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (wSender, doWorkEvent) =>
            {
                try
                {
                    NetConnUtils.ConfirmIPAddress(NetConnUtils.CurrentProxyServer, null, new JobLog());
                }
                catch (Exception ex)
                {
                    Log.LogError(ex);
                }
            };
            worker.RunWorkerCompleted += (wSender, runWorkerCompletedEvent) =>
            {
                try
                {
                    if (runWorkerCompletedEvent.Cancelled == false)
                    {
                        if (OnNetworkChekComplatedEvent != null)
                        {
                            NetworkChekComplatedEventArgs e = new NetworkChekComplatedEventArgs();
                            OnNetworkChekComplatedEvent(null, e);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.ShowError(ex);
                }
            };
            worker.RunWorkerAsync();
        }
コード例 #2
0
 private void JustNetworkUtils_OnNetworkChekComplatedEvent(object sender, NetworkChekComplatedEventArgs e)
 {
     try
     {
         if (this.InvokeRequired)
         {
             this.Invoke((MethodInvoker) delegate() { ProcessNetworkChekComplatedEvent(); });
         }
         else
         {
             ProcessNetworkChekComplatedEvent();
         }
     }
     catch (Exception ex)
     {
         Log.LogError(ex);
     }
 }
コード例 #3
0
ファイル: NetConnUtils.cs プロジェクト: WildGenie/nettools
        static public bool ConnectWithConfirmationLocal(BaseProxyServer item, string homeIP, out bool createdNew, JobLog jobLog)
        {
            bool result = false;

            jobLog.Debug("Start VpnServerConnectWithFullTestLocal for item " + item.JProxyServerId);
            createdNew = false;
            int closeAttemptCount = 0;

            while (NetConnUtils.IsActiveConnectionPresent())
            {
                if (closeAttemptCount > 10)
                {
                    throw new Exception("Unable to close previous active connection");
                }
                jobLog.Debug("Found previous active connection. Going to close it");
                NetConnUtils.CloseAllActiveConnections(false);
                Thread.Sleep(2 * 1000);//!!!
                closeAttemptCount++;
            }
            ///////test
            if (homeIP != null)
            {
                string externalIP = NetConnUtils.GetMyExternalIP();
                if (externalIP == null)
                {
                    throw new Exception("Ip of default connection is null");
                }
                jobLog.Info("Default ExternalIP: " + externalIP);
                if (!homeIP.Equals(externalIP))
                {
                    throw new Exception("Ip address of default connection not equals home ip address.");
                }
            }
            //////////
            if (NetConnUtils.IsConnectionEntryExist(item) == false)
            {
                if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.PPTP))
                {
                    NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.PPTP);
                    createdNew = true;
                }
                else if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.L2TP))
                {
                    NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.L2TP);
                    createdNew = true;
                }
                else
                {
                    throw new ArgumentException(VpnSelectorLibRes.Non_PPTP_no_L2TP_protocols_available_for_this_vpn_entry);
                }
            }
            try
            {
                NetConnUtils.OpenConnectLocal(item, false); //sync

                Thread.Sleep(2 * 1000);                     //!!!
                //for (int i = 0; i < 60; i++)//~ 1 min
                //{
                if (NetConnUtils.IsConnected(item))
                {
                    ConfirmIPAddress(item, homeIP, jobLog);
                    result               = true;
                    item.SuccessCount    = item.SuccessCount + 1;
                    item.LastSuccessDate = DateTimeOffset.Now;
                    Dm.Instance.SaveObject(item);
                }
                else
                {
                    jobLog.Info("Not connected for item " + item.JProxyServerId);
                }
                //Thread.Sleep(1 * 1000);
                //}
                //todo error event
            }
            finally {
                //change label
                if (OnNetworkChekComplatedEvent != null)
                {
                    NetworkChekComplatedEventArgs e = new NetworkChekComplatedEventArgs();
                    OnNetworkChekComplatedEvent(null, e);
                }
            }
            return(result);
        }