123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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 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<ManagementBaseObject>();
- _servicename = services.Any() ? (string)services.First()["Name"] : "";
- Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename));
- }
- return _servicename;
- }
- public static LocalConfiguration<T> GetConfiguration<T>(string section = "")
- where T : ILocalConfigurationSettings, new()
- {
- // TODO : Remove the fallback location
- var configuration = new LocalConfiguration<T>(CoreUtils.GetCommonAppData(), section);
- if (!File.Exists(configuration.GetFileName()))
- {
- var oldsettings = new LocalConfiguration<T>(
- 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<ServerSettings> GetConfiguration(string section = "") => GetConfiguration<ServerSettings>(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();
- }
- }
|