This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-sharp-desktop/Aurora/Backend/Services/ServerService.cs

70 lines
1.8 KiB
C#

using System;
using System.Threading.Tasks;
using Grpc.Core;
using Aurora.Backend.Proto;
namespace Aurora.Backend.Services
{
public class ServerService : BaseService<ServerService>
{
private string _hostname = "127.0.0.1";
private int _port = 50001;
private Grpc.Core.Server _server;
/// <summary>
/// Constructor. Registers GRPC service implementations.
/// </summary>
public ServerService()
{
}
public void Initialize(string hostname, int port)
{
this._port = port;
this._hostname = hostname;
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
};
}
/// <summary>
/// Start Server
/// </summary>
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));
}
}
/// <summary>
/// Shutdown server async.
/// </summary>
/// <returns>Task</returns>
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);
}
}
}