using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; using System.Reflection; using System.ServiceProcess; using System.Threading; using InABox.Configuration; using InABox.Core; using InABox.Logging; using InABox.IPC; using PRSServices; using System; using PRSServer; namespace PRSServices; public abstract class PRSService : ServiceBase { private IEngine? _engine; private string _servicename; private Thread? _thread; public PRSService(string serviceName) { CanPauseAndContinue = false; CanShutdown = true; if (string.IsNullOrWhiteSpace(serviceName)) _servicename = GetServiceName(); else _servicename = serviceName; ServiceName = _servicename; } private string GetServiceName() { if (string.IsNullOrWhiteSpace(_servicename)) { var processId = Process.GetCurrentProcess().Id; var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service where ProcessId = " + processId); var collection = searcher.Get(); var services = collection.Cast(); _servicename = services.Any() ? (string)services.First()["Name"] : ""; Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename)); } return _servicename; } public static LocalConfiguration GetConfiguration(string section = "") where T : ILocalConfigurationSettings, new() { // TODO : Remove the fallback location var configuration = new LocalConfiguration(CoreUtils.GetCommonAppData(), section); if (!File.Exists(configuration.GetFileName())) { var oldsettings = new LocalConfiguration( Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location), "" ); if (File.Exists(oldsettings.GetFileName())) { // Transition old data to new location var settings = oldsettings.LoadAll(); configuration.SaveAll(settings); File.Delete(oldsettings.GetFileName()); } } return configuration; } public static LocalConfiguration GetConfiguration(string section = "") => GetConfiguration(section); public void Run(string servicename) { _servicename = servicename; OnStart(new string[] { }); } public void Halt() { OnStop(); } protected virtual Server CreateServer(ServerSettings settings, string serviceName) { return settings.CreateServer(serviceName); } protected abstract IEngine? CreateEngine(ServerSettings settings); protected virtual ServerSettings GetSettings(string serviceName) { return GetConfiguration(serviceName).Load(); } protected override void OnStart(string[] args) { Logger.OnLog += MainLogger.Send; //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath()); var svcname = GetServiceName(); var settings = GetSettings(svcname); if (settings != null) { var server = CreateServer(settings, svcname); _engine = CreateEngine(settings); if (_engine != null) { _engine.ServiceName = svcname; _engine.Version = CoreUtils.GetVersion(); _engine.Configure(server); /*_task = Task.Run(() => { _engine.Run(); }, Source.Token);*/ _thread = new Thread(() => { _engine.Run(); }); _thread.IsBackground = true; _thread.Start(); } } } protected override void OnStop() { Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename); _engine?.Stop(); /*var timer = new Timer { Interval = 1000 }; timer.Elapsed += (o, e) => { try { Logger.Send(LogType.Information, "", "Terminating Process.."); MainLogger.Stop(); Environment.Exit(0); } catch (Exception err) { Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message); } }; timer.AutoReset = false; timer.Enabled = true;*/ Logger.Send(LogType.Information, "", "Terminating Process.."); MainLogger.Stop(); } }