EEMSBaseServer.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. namespace EEMSService;
  2. public class EEMSBaseServer : IDisposable
  3. {
  4. public WebApplication? WebApplication { get; private set; }
  5. private IEEMSBaseServerProvider _provider;
  6. public bool Initialize(IEEMSBaseServerProvider provider, long messageSize = 26144)//26144:256kb
  7. {
  8. if (WebApplication is not null)
  9. return false;
  10. WebApplicationBuilder builder = WebApplication.CreateBuilder();
  11. builder.Services.AddSignalR(
  12. options =>
  13. {
  14. options.EnableDetailedErrors = true;
  15. options.MaximumReceiveMessageSize = messageSize;
  16. });
  17. builder.Services.AddHostedService<HostLifeTime>();
  18. builder.Services.AddSingleton<DeviceManager>();
  19. builder.Services.AddSingleton<ClientManager>();
  20. builder.Services.AddSingleton<UISender>();
  21. builder.Services.AddSingleton<IEEMSBaseServerProvider>(provider);
  22. this._provider = provider;
  23. WebApplication = builder.Build();
  24. WebApplication.MapHub<UIHub>("/UIHub");
  25. WebApplication.MapHub<ClientsHub>("/ClientHub");
  26. return true;
  27. }
  28. public async Task<bool> StartServiceAsync(string ip, ushort port)
  29. {
  30. if (WebApplication is null)
  31. return false;
  32. if (string.IsNullOrEmpty(ip) || port < 1000)
  33. return false;
  34. try
  35. {
  36. await WebApplication.RunAsync($"http://{ip}:{port}");
  37. }
  38. catch
  39. {
  40. return false;
  41. }
  42. return true;
  43. }
  44. #region Dispose
  45. private bool disposedValue;
  46. protected virtual void Dispose(bool disposing)
  47. {
  48. if (!disposedValue)
  49. {
  50. if (disposing)
  51. {
  52. // TODO: dispose managed state (managed objects)
  53. this.WebApplication?.DisposeAsync().AsTask().Wait();
  54. this.WebApplication = null;
  55. }
  56. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  57. // TODO: set large fields to null
  58. disposedValue = true;
  59. }
  60. }
  61. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  62. // ~EEMS_Server()
  63. // {
  64. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  65. // Dispose(disposing: false);
  66. // }
  67. public void Dispose()
  68. {
  69. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  70. Dispose(disposing: true);
  71. GC.SuppressFinalize(this);
  72. }
  73. #endregion
  74. }
  75. public interface IEEMSBaseServerProvider
  76. {
  77. void Started();
  78. void OnConnected(string ip, ushort port, ServiceHub serviceHub);
  79. void OnDisConnected(string ip, ushort port, ServiceHub serviceHub);
  80. }
  81. public enum ServiceHub
  82. {
  83. Undefined,
  84. ClinetHub,
  85. UIHub
  86. }