Engine.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using InABox.Core;
  5. using InABox.IPC;
  6. using InABox.Logging;
  7. namespace PRSServer
  8. {
  9. public interface IEngine
  10. {
  11. string ServiceName { get; set; }
  12. string Version { get; set; }
  13. void Run();
  14. void Stop();
  15. void Configure(Server settings);
  16. PortStatus[] PortStatusList();
  17. }
  18. public abstract class Engine<TProperties> : IEngine where TProperties : ServerProperties
  19. {
  20. private RPCServerPipeTransport _enginemanager;
  21. public TProperties Properties { get; private set; }
  22. public abstract void Run();
  23. public abstract void Stop();
  24. public virtual PortStatus[] PortStatusList()
  25. {
  26. return new PortStatus[] { };
  27. }
  28. public string ServiceName { get; set; }
  29. public string Version { get; set; }
  30. protected string AppDataFolder { get; set; }
  31. public virtual void Configure(Server server)
  32. {
  33. Properties = server.Properties as TProperties;
  34. AppDataFolder = GetPath(server.Key);
  35. MainLogger.AddLogger(new LogFileLogger(AppDataFolder));
  36. MainLogger.AddLogger(new NamedPipeLogger(server.Key));
  37. _enginemanager = new RPCServerPipeTransport($"{ServiceName}$");
  38. _enginemanager.AddHandler<IEngine, PortStatusCommand, PortStatusParameters, PortStatus[]>(new PortStatusHandler(this));
  39. _enginemanager.AfterMessage += (transport, args) => MainLogger.Send(LogType.Information,"",$"Engine Manager Message: {args.Message?.Command}");
  40. _enginemanager.Start();
  41. }
  42. public static string GetPath(string key)
  43. {
  44. if (Assembly.GetEntryAssembly() != null)
  45. return Path.Combine(
  46. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
  47. Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location),
  48. key
  49. );
  50. return Path.Combine(
  51. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
  52. Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location),
  53. key
  54. );
  55. }
  56. }
  57. }