EEMSBaseServer.cs 3.0 KB

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