EEMSBaseServer.cs 2.9 KB

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