AppManagerComponent.cs 2.5 KB

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