aurora/Aurora/Services/ServerService.cs
2019-07-05 14:17:09 -04:00

80 lines
2.0 KiB
C#

using System;
using System.Threading.Tasks;
using Grpc.Core;
using Aurora.Proto;
namespace Aurora.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 int Port
{
get { return _port; }
}
public string Hostname
{
get { return _hostname; }
}
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);
}
}
}