NewLife/XCoder

升级星尘到最新版
大石头 authored at 2024-08-22 11:17:54
2c7436f
Tree
1 Parent(s) bcf9db2
Summary: 6 changed files with 233 additions and 159 deletions.
Modified +18 -145
Modified +8 -8
Added +203 -0
Modified +1 -3
Modified +2 -2
Modified +1 -1
Modified +18 -145
diff --git a/XCoder/Program.cs b/XCoder/Program.cs
index bf733e0..cf22908 100644
--- a/XCoder/Program.cs
+++ b/XCoder/Program.cs
@@ -1,18 +1,12 @@
 using System;
-using System.Diagnostics;
-using System.IO;
+using System.Net.NetworkInformation;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 using NewLife;
 using NewLife.Log;
 using NewLife.Threading;
-using System.Threading.Tasks;
-using NewLife.Model;
-using Stardust.Services;
-using Stardust.Models;
-using System.Net.NetworkInformation;
-#if !NET4
 using Stardust;
-#endif
+using Stardust.Models;
 
 namespace XCoder;
 
@@ -22,29 +16,22 @@ static class Program
     [STAThread]
     static void Main()
     {
-#if NC30
-        XTrace2.UseWinForm();
-        Application.SetHighDpiMode(HighDpiMode.SystemAware);
-#else
         XTrace.UseWinForm();
-#endif
         MachineInfo.RegisterAsync();
 
-#if !NET4
         StartClient();
-#endif
 
-        StringHelper.EnableSpeechTip = XConfig.Current.SpeechTip;
+        var set = XConfig.Current;
+        StringHelper.EnableSpeechTip = set.SpeechTip;
 
-        if (XConfig.Current.IsNew) "学无先后达者为师,欢迎使用新生命码神工具!".SpeechTip();
+        if (set.IsNew) "学无先后达者为师,欢迎使用新生命码神工具!".SpeechTip();
 
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new FrmMDI());
     }
 
-#if !NET4
-    static TimerX _timer;
+    static StarFactory _factory;
     static StarClient _Client;
     private static void StartClient()
     {
@@ -54,150 +41,36 @@ static class Program
 
         XTrace.WriteLine("初始化服务端地址:{0}", server);
 
+        _factory = new StarFactory(server, "XCoder", null)
+        {
+            Log = XTrace.Log,
+        };
+
         var client = new StarClient(server)
         {
             Code = set.Code,
             Secret = set.Secret,
             ProductCode = "XCoder",
+
+            Tracer = _factory.Tracer,
             Log = XTrace.Log,
         };
 
         // 登录后保存证书
         client.OnLogined += (s, e) =>
         {
-            var inf = client.Info;
-            if (inf != null && !inf.Code.IsNullOrEmpty())
+            if (client.Logined && !client.Code.IsNullOrEmpty())
             {
-                set.Code = inf.Code;
-                set.Secret = inf.Secret;
+                set.Code = client.Code;
+                set.Secret = client.Secret;
                 set.Save();
             }
         };
 
-        // 使用跟踪
-        client.UseTrace();
+        client.Open();
 
         Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit");
 
-        // 可能需要多次尝试
-        _timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
-
         _Client = client;
     }
-
-    private static async Task TryConnectServer(Object state)
-    {
-        if (!NetworkInterface.GetIsNetworkAvailable() || AgentInfo.GetIps().IsNullOrEmpty())
-        {
-            return;
-        }
-
-        var client = state as StarClient;
-
-        try
-        {
-            await client.Login();
-            //await CheckUpgrade(client);
-        }
-        catch (Exception ex)
-        {
-            // 登录报错后,加大定时间隔,输出简单日志
-            //_timer.Period = 30_000;
-            if (_timer.Period < 30_000) _timer.Period += 5_000;
-
-            XTrace.Log?.Error(ex.Message);
-
-            return;
-        }
-
-        _timer.TryDispose();
-        _timer = new TimerX(CheckUpgrade, null, 5_000, 600_000) { Async = true };
-
-        client.RegisterCommand("node/upgrade", s => _timer.SetNext(-1));
-    }
-
-    private static String _lastVersion;
-    private static async Task CheckUpgrade(Object data)
-    {
-        var client = _Client;
-        using var span = client.Tracer?.NewSpan("CheckUpgrade", new { _lastVersion });
-
-        // 运行过程中可能改变配置文件的通道
-        var set = XConfig.Current;
-        var ug = new Stardust.Web.Upgrade { Log = XTrace.Log };
-
-        // 去除多余入口文件
-        ug.Trim("XCoder");
-
-        // 检查更新
-        var ur = await client.Upgrade(set.Channel);
-        if (ur != null && ur.Version != _lastVersion)
-        {
-            client.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{ur.Version}],开始下载 {ur.Source}");
-            try
-            {
-                ug.Url = client.BuildUrl(ur.Source);
-                await ug.Download();
-
-                // 检查文件完整性
-                var checkHash = ug.CheckFileHash(ur.FileHash);
-                if (!ur.FileHash.IsNullOrEmpty() && !checkHash)
-                {
-                    client.WriteInfoEvent("Upgrade", "下载完成,哈希校验失败");
-                }
-                else
-                {
-                    client.WriteInfoEvent("Upgrade", "下载完成,准备解压文件");
-                    if (!ug.Extract())
-                    {
-                        client.WriteInfoEvent("Upgrade", "解压失败");
-                    }
-                    else
-                    {
-                        if (!ur.Preinstall.IsNullOrEmpty())
-                        {
-                            client.WriteInfoEvent("Upgrade", "执行预安装脚本");
-
-                            ug.Run(ur.Preinstall);
-                        }
-
-                        client.WriteInfoEvent("Upgrade", "解压完成,准备覆盖文件");
-
-                        // 执行更新,解压缩覆盖文件
-                        var rs = ug.Update();
-                        if (rs && !ur.Executor.IsNullOrEmpty()) ug.Run(ur.Executor);
-                        _lastVersion = ur.Version;
-
-                        // 去除多余入口文件
-                        ug.Trim("XCoder");
-
-                        // 强制更新时,马上重启
-                        if (rs && ur.Force)
-                        {
-                            // 重新拉起进程
-                            rs = ug.Run("XCoder", "-run -upgrade");
-
-                            if (rs)
-                            {
-                                var pid = Process.GetCurrentProcess().Id;
-                                client.WriteInfoEvent("Upgrade", "强制更新完成,新进程已拉起,准备退出当前进程!PID=" + pid);
-
-                                ug.KillSelf();
-                            }
-                            else
-                            {
-                                client.WriteInfoEvent("Upgrade", "强制更新完成,但拉起新进程失败");
-                            }
-                        }
-                    }
-                }
-            }
-            catch (Exception ex)
-            {
-                XTrace.WriteException(ex);
-                client.WriteErrorEvent("Upgrade", ex.ToString());
-            }
-        }
-    }
-#endif
 }
\ No newline at end of file
Modified +8 -8
diff --git a/XCoder/XCoder.csproj b/XCoder/XCoder.csproj
index 4e1ec57..9443a79 100644
--- a/XCoder/XCoder.csproj
+++ b/XCoder/XCoder.csproj
@@ -17,7 +17,7 @@
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <DebugSymbols>true</DebugSymbols>
-    <OutputPath>..\..\XCoder\</OutputPath>
+    <OutputPath>..\Bin\XCoder\</OutputPath>
     <DefineConstants>TRACE;DEBUG</DefineConstants>
     <DebugType>full</DebugType>
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -25,7 +25,7 @@
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
-    <OutputPath>..\..\XCoder\</OutputPath>
+    <OutputPath>..\Bin\XCoder\</OutputPath>
     <DefineConstants>TRACE</DefineConstants>
     <Optimize>true</Optimize>
     <DebugType>pdbonly</DebugType>
@@ -384,25 +384,25 @@
   </ItemGroup>
   <ItemGroup>
     <PackageReference Include="NewLife.Core">
-      <Version>10.5.2023.801</Version>
+      <Version>10.10.2024.803</Version>
     </PackageReference>
     <PackageReference Include="NewLife.Map">
-      <Version>2.3.2023.806</Version>
+      <Version>2.6.2024.801</Version>
     </PackageReference>
     <PackageReference Include="NewLife.MQTT">
-      <Version>1.4.2023.806</Version>
+      <Version>2.0.2024.708</Version>
     </PackageReference>
     <PackageReference Include="NewLife.Net">
       <Version>4.3.2023.806</Version>
     </PackageReference>
     <PackageReference Include="NewLife.Redis">
-      <Version>5.5.2023.803</Version>
+      <Version>5.7.2024.801</Version>
     </PackageReference>
     <PackageReference Include="NewLife.Stardust">
-      <Version>2.9.2023.801</Version>
+      <Version>3.0.2024.806</Version>
     </PackageReference>
     <PackageReference Include="NewLife.XCode">
-      <Version>11.9.2023.801</Version>
+      <Version>11.15.2024.806</Version>
     </PackageReference>
     <PackageReference Include="SSH.NET">
       <Version>2020.0.2</Version>
Added +203 -0
diff --git a/XCoder40/Program.cs b/XCoder40/Program.cs
new file mode 100644
index 0000000..c6194de
--- /dev/null
+++ b/XCoder40/Program.cs
@@ -0,0 +1,203 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Windows.Forms;
+using NewLife;
+using NewLife.Log;
+using NewLife.Threading;
+using System.Threading.Tasks;
+using NewLife.Model;
+using Stardust.Services;
+using Stardust.Models;
+using System.Net.NetworkInformation;
+#if !NET4
+using Stardust;
+#endif
+
+namespace XCoder;
+
+static class Program
+{
+    /// <summary>应用程序的主入口点。</summary>
+    [STAThread]
+    static void Main()
+    {
+#if NC30
+        XTrace2.UseWinForm();
+        Application.SetHighDpiMode(HighDpiMode.SystemAware);
+#else
+        XTrace.UseWinForm();
+#endif
+        MachineInfo.RegisterAsync();
+
+#if !NET4
+        StartClient();
+#endif
+
+        StringHelper.EnableSpeechTip = XConfig.Current.SpeechTip;
+
+        if (XConfig.Current.IsNew) "学无先后达者为师,欢迎使用新生命码神工具!".SpeechTip();
+
+        Application.EnableVisualStyles();
+        Application.SetCompatibleTextRenderingDefault(false);
+        Application.Run(new FrmMDI());
+    }
+
+#if !NET4
+    static TimerX _timer;
+    static StarClient _Client;
+    private static void StartClient()
+    {
+        var set = XConfig.Current;
+        var server = set.Server;
+        if (server.IsNullOrEmpty()) return;
+
+        XTrace.WriteLine("初始化服务端地址:{0}", server);
+
+        var client = new StarClient(server)
+        {
+            Code = set.Code,
+            Secret = set.Secret,
+            ProductCode = "XCoder",
+            Log = XTrace.Log,
+        };
+
+        // 登录后保存证书
+        client.OnLogined += (s, e) =>
+        {
+            var inf = client.Info;
+            if (inf != null && !inf.Code.IsNullOrEmpty())
+            {
+                set.Code = inf.Code;
+                set.Secret = inf.Secret;
+                set.Save();
+            }
+        };
+
+        // 使用跟踪
+        client.UseTrace();
+
+        Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit");
+
+        // 可能需要多次尝试
+        _timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
+
+        _Client = client;
+    }
+
+    private static async Task TryConnectServer(Object state)
+    {
+        if (!NetworkInterface.GetIsNetworkAvailable() || AgentInfo.GetIps().IsNullOrEmpty())
+        {
+            return;
+        }
+
+        var client = state as StarClient;
+
+        try
+        {
+            await client.Login();
+            //await CheckUpgrade(client);
+        }
+        catch (Exception ex)
+        {
+            // 登录报错后,加大定时间隔,输出简单日志
+            //_timer.Period = 30_000;
+            if (_timer.Period < 30_000) _timer.Period += 5_000;
+
+            XTrace.Log?.Error(ex.Message);
+
+            return;
+        }
+
+        _timer.TryDispose();
+        _timer = new TimerX(CheckUpgrade, null, 5_000, 600_000) { Async = true };
+
+        client.RegisterCommand("node/upgrade", s => _timer.SetNext(-1));
+    }
+
+    private static String _lastVersion;
+    private static async Task CheckUpgrade(Object data)
+    {
+        var client = _Client;
+        using var span = client.Tracer?.NewSpan("CheckUpgrade", new { _lastVersion });
+
+        // 运行过程中可能改变配置文件的通道
+        var set = XConfig.Current;
+        var ug = new Stardust.Web.Upgrade { Log = XTrace.Log };
+
+        // 去除多余入口文件
+        ug.Trim("XCoder");
+
+        // 检查更新
+        var ur = await client.Upgrade(set.Channel);
+        if (ur != null && ur.Version != _lastVersion)
+        {
+            client.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{ur.Version}],开始下载 {ur.Source}");
+            try
+            {
+                ug.Url = client.BuildUrl(ur.Source);
+                await ug.Download();
+
+                // 检查文件完整性
+                var checkHash = ug.CheckFileHash(ur.FileHash);
+                if (!ur.FileHash.IsNullOrEmpty() && !checkHash)
+                {
+                    client.WriteInfoEvent("Upgrade", "下载完成,哈希校验失败");
+                }
+                else
+                {
+                    client.WriteInfoEvent("Upgrade", "下载完成,准备解压文件");
+                    if (!ug.Extract())
+                    {
+                        client.WriteInfoEvent("Upgrade", "解压失败");
+                    }
+                    else
+                    {
+                        if (!ur.Preinstall.IsNullOrEmpty())
+                        {
+                            client.WriteInfoEvent("Upgrade", "执行预安装脚本");
+
+                            ug.Run(ur.Preinstall);
+                        }
+
+                        client.WriteInfoEvent("Upgrade", "解压完成,准备覆盖文件");
+
+                        // 执行更新,解压缩覆盖文件
+                        var rs = ug.Update();
+                        if (rs && !ur.Executor.IsNullOrEmpty()) ug.Run(ur.Executor);
+                        _lastVersion = ur.Version;
+
+                        // 去除多余入口文件
+                        ug.Trim("XCoder");
+
+                        // 强制更新时,马上重启
+                        if (rs && ur.Force)
+                        {
+                            // 重新拉起进程
+                            rs = ug.Run("XCoder", "-run -upgrade");
+
+                            if (rs)
+                            {
+                                var pid = Process.GetCurrentProcess().Id;
+                                client.WriteInfoEvent("Upgrade", "强制更新完成,新进程已拉起,准备退出当前进程!PID=" + pid);
+
+                                ug.KillSelf();
+                            }
+                            else
+                            {
+                                client.WriteInfoEvent("Upgrade", "强制更新完成,但拉起新进程失败");
+                            }
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                XTrace.WriteException(ex);
+                client.WriteErrorEvent("Upgrade", ex.ToString());
+            }
+        }
+    }
+#endif
+}
\ No newline at end of file
Modified +1 -3
diff --git a/XCoder40/XCoder40.csproj b/XCoder40/XCoder40.csproj
index 692e472..1098ffd 100644
--- a/XCoder40/XCoder40.csproj
+++ b/XCoder40/XCoder40.csproj
@@ -100,9 +100,6 @@
     <Compile Include="..\XCoder\IXForm.cs">
       <Link>IXForm.cs</Link>
     </Compile>
-    <Compile Include="..\XCoder\Program.cs">
-      <Link>Program.cs</Link>
-    </Compile>
     <Compile Include="..\XCoder\Properties\AssemblyInfo.cs">
       <Link>Properties\AssemblyInfo.cs</Link>
     </Compile>
@@ -270,6 +267,7 @@
     <Compile Include="..\XCoder\Yun\MapSetting.cs">
       <Link>Yun\MapSetting.cs</Link>
     </Compile>
+    <Compile Include="Program.cs" />
     <EmbeddedResource Include="..\XCoder\FileEncoding\FrmMain.resx">
       <Link>FileEncoding\FrmMain.resx</Link>
     </EmbeddedResource>
Modified +2 -2
diff --git a/XCoderLinux/XCoderLinux.csproj b/XCoderLinux/XCoderLinux.csproj
index 3815414..8893159 100644
--- a/XCoderLinux/XCoderLinux.csproj
+++ b/XCoderLinux/XCoderLinux.csproj
@@ -48,8 +48,8 @@
 
   <ItemGroup>
     <PackageReference Include="GtkSharp" Version="3.24.24.34" />
-    <PackageReference Include="NewLife.Core" Version="8.11.2021.1204" />
-    <PackageReference Include="NewLife.XCode" Version="10.3.2021.1204" />
+    <PackageReference Include="NewLife.Core" Version="10.10.2024.803" />
+    <PackageReference Include="NewLife.XCode" Version="11.15.2024.806" />
     <PackageReference Include="System.IO.Ports" Version="6.0.0" />
     <PackageReference Include="System.Management" Version="6.0.0" />
   </ItemGroup>
Modified +1 -1
diff --git a/XCoderWpf/XCoderWpf.csproj b/XCoderWpf/XCoderWpf.csproj
index 02c07fd..bf963b4 100644
--- a/XCoderWpf/XCoderWpf.csproj
+++ b/XCoderWpf/XCoderWpf.csproj
@@ -31,7 +31,7 @@
   <ItemGroup>
     <PackageReference Include="HandyControls" Version="3.3.9" />
     <PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
-    <PackageReference Include="NewLife.XCode" Version="10.3.2021.1204" />
+    <PackageReference Include="NewLife.XCode" Version="11.15.2024.806" />
     <PackageReference Include="Prism.DryIoc" Version="8.1.97" />
   </ItemGroup>