PRSService.cs 4.6 KB

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