aurora/Aurora/Services/ServerService.cs

80 lines
2.0 KiB
C#
Raw Normal View History

2019-06-03 14:57:05 +00:00
using System;
using System.Threading.Tasks;
using Grpc.Core;
2019-07-05 18:17:09 +00:00
using Aurora.Proto;
2019-06-03 14:57:05 +00:00
2019-07-05 18:17:09 +00:00
namespace Aurora.Services
2019-06-03 14:57:05 +00:00
{
public class ServerService : BaseService<ServerService>
{
private string _hostname = "127.0.0.1";
private int _port = 50001;
2019-06-03 14:57:05 +00:00
private Grpc.Core.Server _server;
/// <summary>
/// Constructor. Registers GRPC service implementations.
/// </summary>
public ServerService()
{
}
2019-07-05 18:17:09 +00:00
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;
2019-06-03 14:57:05 +00:00
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
2019-06-03 14:57:05 +00:00
};
}
/// <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));
}
2019-06-03 14:57:05 +00:00
}
/// <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) }
2019-06-03 14:57:05 +00:00
};
}
public void RegisterService(ServerServiceDefinition definition)
{
_server.Services.Add(definition);
}
}
}