Engine.cs 2.3 KB

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