using Microsoft.Extensions.Options; using RPG.Services.Core.Network; using RPG.Services.Core.Network.Command; using RPG.Services.Core.Options; namespace RPG.Services.Core; public class ServiceManager { private readonly IOptions _nodeOptions; private readonly IOptions _serviceOptions; private readonly ServiceCommandHandler _handler; private readonly ServiceBox _serviceBox; private ServiceEndPoint? _serviceEndPoint; public ServiceManager(IOptions options, IOptions serviceOptions, ServiceCommandHandler commandHandler, ServiceBox serviceBox) { _nodeOptions = options; _serviceOptions = serviceOptions; _handler = commandHandler; _serviceBox = serviceBox; } public void Start() { ServiceNodeOptions nodeOptions = _nodeOptions.Value; RPGServiceOptions serviceOptions = _serviceOptions.Value; _serviceBox.Construct(); _serviceEndPoint = new(nodeOptions.GetEntry(serviceOptions.ServiceType)); _serviceEndPoint.OnCommand += _handler.HandleAsync; _serviceEndPoint.Start(); } public async Task ShutdownAsync() { if (_serviceEndPoint != null) { await _serviceEndPoint.StopAsync(); _serviceEndPoint.OnCommand -= _handler.HandleAsync; } } }