PRSService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Reflection;
  7. using System.ServiceProcess;
  8. using System.Threading;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.Logging;
  12. using InABox.IPC;
  13. using PRSServices;
  14. using PRSServer;
  15. namespace PRSServices;
  16. public abstract class PRSService : ServiceBase
  17. {
  18. private IEngine? _engine;
  19. private string _servicename;
  20. private Thread? _thread;
  21. public PRSService(string serviceName)
  22. {
  23. CanPauseAndContinue = false;
  24. CanShutdown = true;
  25. if (string.IsNullOrWhiteSpace(serviceName))
  26. _servicename = GetServiceName();
  27. else
  28. _servicename = serviceName;
  29. ServiceName = _servicename;
  30. }
  31. private string GetServiceName()
  32. {
  33. if (string.IsNullOrWhiteSpace(_servicename))
  34. {
  35. var processId = Process.GetCurrentProcess().Id;
  36. var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service where ProcessId = " + processId);
  37. var collection = searcher.Get();
  38. var services = collection.Cast<ManagementBaseObject>();
  39. _servicename = services.Any() ? (string)services.First()["Name"] : "";
  40. Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename));
  41. }
  42. return _servicename;
  43. }
  44. public static LocalConfiguration<T> GetConfiguration<T>(string section = "")
  45. where T : ILocalConfigurationSettings, new()
  46. {
  47. // TODO : Remove the fallback location
  48. var configuration = new LocalConfiguration<T>(CoreUtils.GetCommonAppData(), section);
  49. if (!File.Exists(configuration.GetFileName()))
  50. {
  51. var oldsettings = new LocalConfiguration<T>(
  52. Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
  53. ""
  54. );
  55. if (File.Exists(oldsettings.GetFileName()))
  56. {
  57. // Transition old data to new location
  58. var settings = oldsettings.LoadAll();
  59. configuration.SaveAll(settings);
  60. File.Delete(oldsettings.GetFileName());
  61. }
  62. }
  63. return configuration;
  64. }
  65. public static LocalConfiguration<ServerSettings> GetConfiguration(string section = "") => GetConfiguration<ServerSettings>(section);
  66. public void Run(string servicename)
  67. {
  68. _servicename = servicename;
  69. OnStart(new string[] { });
  70. }
  71. public void Halt()
  72. {
  73. OnStop();
  74. }
  75. protected virtual Server CreateServer(ServerSettings settings, string serviceName)
  76. {
  77. return settings.CreateServer(serviceName);
  78. }
  79. protected abstract IEngine? CreateEngine(ServerSettings settings);
  80. protected virtual ServerSettings GetSettings(string serviceName)
  81. {
  82. return GetConfiguration(serviceName).Load();
  83. }
  84. protected override void OnStart(string[] args)
  85. {
  86. Logger.OnLog += MainLogger.Send;
  87. //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath());
  88. var svcname = GetServiceName();
  89. var settings = GetSettings(svcname);
  90. if (settings != null)
  91. {
  92. var server = CreateServer(settings, svcname);
  93. _engine = CreateEngine(settings);
  94. if (_engine != null)
  95. {
  96. _engine.ServiceName = svcname;
  97. _engine.Version = CoreUtils.GetVersion();
  98. _engine.Configure(server);
  99. /*_task = Task.Run(() =>
  100. {
  101. _engine.Run();
  102. }, Source.Token);*/
  103. _thread = new Thread(() => { _engine.Run(); });
  104. _thread.IsBackground = true;
  105. _thread.Start();
  106. }
  107. }
  108. }
  109. protected override void OnStop()
  110. {
  111. Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename);
  112. _engine?.Stop();
  113. /*var timer = new Timer { Interval = 1000 };
  114. timer.Elapsed += (o, e) =>
  115. {
  116. try
  117. {
  118. Logger.Send(LogType.Information, "", "Terminating Process..");
  119. MainLogger.Stop();
  120. Environment.Exit(0);
  121. }
  122. catch (Exception err)
  123. {
  124. Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message);
  125. }
  126. };
  127. timer.AutoReset = false;
  128. timer.Enabled = true;*/
  129. Logger.Send(LogType.Information, "", "Terminating Process..");
  130. MainLogger.Stop();
  131. }
  132. }