AppManagerComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. namespace ETModel
  7. {
  8. [ObjectSystem]
  9. public class AppManagerComponentAwakeSystem : AwakeSystem<AppManagerComponent, NetworkProtocol>
  10. {
  11. public override void Awake(AppManagerComponent self, NetworkProtocol protocol)
  12. {
  13. self.Awake(protocol);
  14. }
  15. }
  16. public class AppManagerComponent: Component
  17. {
  18. private NetworkProtocol networkProtocol;
  19. private readonly Dictionary<int, Process> processes = new Dictionary<int, Process>();
  20. public void Awake(NetworkProtocol protocol)
  21. {
  22. this.networkProtocol = protocol;
  23. string[] ips = NetHelper.GetAddressIPs();
  24. StartConfig[] startConfigs = StartConfigComponent.Instance.GetAll();
  25. foreach (StartConfig startConfig in startConfigs)
  26. {
  27. Game.Scene.GetComponent<TimerComponent>().WaitAsync(100);
  28. if (!ips.Contains(startConfig.ServerIP) && startConfig.ServerIP != "*")
  29. {
  30. continue;
  31. }
  32. if (startConfig.AppType.Is(AppType.Manager))
  33. {
  34. continue;
  35. }
  36. StartProcess(startConfig.AppId);
  37. }
  38. this.WatchProcessAsync();
  39. }
  40. private void StartProcess(int appId)
  41. {
  42. OptionComponent optionComponent = Game.Scene.GetComponent<OptionComponent>();
  43. StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
  44. string configFile = optionComponent.Options.Config;
  45. StartConfig startConfig = startConfigComponent.Get(appId);
  46. const string exe = "dotnet";
  47. string arguments = $"App.dll --appId={startConfig.AppId} --appType={startConfig.AppType} --config={configFile} --protocol={this.networkProtocol}";
  48. Log.Info($"{exe} {arguments}");
  49. try
  50. {
  51. bool useShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  52. ProcessStartInfo info = new ProcessStartInfo { FileName = exe, Arguments = arguments, CreateNoWindow = true, UseShellExecute = useShellExecute };
  53. Process process = Process.Start(info);
  54. this.processes.Add(startConfig.AppId, process);
  55. }
  56. catch (Exception e)
  57. {
  58. Log.Error(e);
  59. }
  60. }
  61. /// <summary>
  62. /// 监控启动的进程,如果进程挂掉了,重新拉起
  63. /// </summary>
  64. private async void WatchProcessAsync()
  65. {
  66. long instanceId = this.InstanceId;
  67. while (true)
  68. {
  69. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(5000);
  70. if (this.InstanceId != instanceId)
  71. {
  72. return;
  73. }
  74. foreach (int appId in this.processes.Keys.ToArray())
  75. {
  76. Process process = this.processes[appId];
  77. if (!process.HasExited)
  78. {
  79. continue;
  80. }
  81. this.processes.Remove(appId);
  82. process.Dispose();
  83. this.StartProcess(appId);
  84. }
  85. }
  86. }
  87. }
  88. }