Exemplo n.º 1
0
        public void Stop()
        {
            reConnectThread?.Abort();
            reConnectThread = null;

            heartThread?.Abort();
            heartThread = null;

            NatClient?.Close();
            NatClient = null;
        }
Exemplo n.º 2
0
 static void ConnectNatServer()
 {
     try
     {
         if (IsStop)
         {
             return;
         }
         HandleLog.WriteLine($"正在连接服务器...");
         NatClient?.Close();
         NatClient = null;
         NatClient = new EasyClient <NatPackageInfo>()
         {
             NoDelay  = true,
             Security = new SecurityOption()
             {
                 EnabledSslProtocols          = System.Security.Authentication.SslProtocols.Tls12,
                 AllowNameMismatchCertificate = true,
                 AllowCertificateChainErrors  = true,
                 AllowUnstrustedCertificate   = true
             }
         };
         NatClient.Initialize(new NatReceiveFilter());
         NatClient.Connected          += OnClientConnected;
         NatClient.NewPackageReceived += OnPackageReceived;
         NatClient.Error  += OnClientError;
         NatClient.Closed += OnClientClosed;
         //解析主机名
         IPHostEntry ipInfo   = Dns.GetHostEntry(ServerUrl);
         var         serverIp = ipInfo.AddressList.Any() ? ipInfo.AddressList[0].ToString() : throw new Exception($"域名【{ServerUrl}】无法解析");
         //连接NAT转发服务
         var res = NatClient.ConnectAsync(new IPEndPoint(IPAddress.Parse(serverIp), NatPort)).Result;
         if (!res)
         {
             Thread.Sleep(2000);
             ConnectNatServer();
         }
     }
     catch (Exception ex)
     {
         HandleLog.WriteLine($"连接服务器失败:{ex}");
         Thread.Sleep(1000);
         ConnectNatServer();
     }
 }