using System; using System.Threading.Tasks; using Grpc.Core; using Aurora.Proto; namespace Aurora.Services { public class ServerService : BaseService { private string _hostname = "127.0.0.1"; private int _port = SettingsService.Instance.DefaultPort; private Grpc.Core.Server _server; /// /// Constructor. Registers GRPC service implementations. /// public ServerService() { } public int Port { get { return _port; } } public string Hostname { get { return _hostname; } } public void Initialize(string hostname) { this._hostname = hostname; _server = new Grpc.Core.Server { Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) } }; } /// /// Start Server /// public void Start() { try { Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port)); _server.Start(); } catch (Exception ex) { Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message)); } } /// /// Shutdown server async. /// /// Task public async Task Stop() { await _server.ShutdownAsync(); } public async Task Reset() { await Stop(); _server = new Grpc.Core.Server { Ports = { new ServerPort("localhost", _port, ServerCredentials.Insecure) } }; } public void RegisterService(ServerServiceDefinition definition) { _server.Services.Add(definition); } } }