Files
Ragon/Ragon.Core/Application.cs
T

115 lines
3.0 KiB
C#
Raw Normal View History

2022-12-17 21:16:02 +04:00
using System.Diagnostics;
using NLog;
using Ragon.Common;
using Ragon.Core.Lobby;
2022-12-20 12:20:52 -08:00
using Ragon.Core.Server;
2022-12-17 21:16:02 +04:00
using Ragon.Core.Time;
using Ragon.Server;
using Ragon.Server.ENet;
namespace Ragon.Core;
public class Application : INetworkListener
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly INetworkServer _server;
private readonly Thread _dedicatedThread;
2022-12-20 12:20:52 -08:00
private readonly Executor _executor;
2022-12-17 21:16:02 +04:00
private readonly Configuration _configuration;
private readonly HandlerRegistry _handlerRegistry;
private readonly ILobby _lobby;
private readonly Loop _loop;
private readonly Dictionary<ushort, PlayerContext> _contexts;
public Application(Configuration configuration)
{
_configuration = configuration;
2022-12-20 12:20:52 -08:00
_executor = new Executor();
2022-12-17 21:16:02 +04:00
_dedicatedThread = new Thread(Execute);
_dedicatedThread.IsBackground = true;
_contexts = new Dictionary<ushort, PlayerContext>();
_handlerRegistry = new HandlerRegistry();
_lobby = new LobbyInMemory();
_loop = new Loop();
2022-12-20 12:20:52 -08:00
if (configuration.ServerType == "enet")
2022-12-17 21:16:02 +04:00
_server = new ENetServer();
2022-12-20 12:20:52 -08:00
if (configuration.ServerType == "websocket")
_server = new NativeWebSocketServer(_executor);
2022-12-17 21:16:02 +04:00
2022-12-20 12:20:52 -08:00
Debug.Assert(_server != null, $"Socket type not supported: {configuration.ServerType}. Supported: [enet, websocket]");
2022-12-17 21:16:02 +04:00
}
public void Execute()
{
while (true)
{
2022-12-20 12:20:52 -08:00
_executor.Execute();
2022-12-17 21:16:02 +04:00
_loop.Tick();
_server.Poll();
2022-12-20 12:20:52 -08:00
Thread.Sleep((int)1000.0f / _configuration.ServerTickRate);
2022-12-17 21:16:02 +04:00
}
}
public void Start()
{
var networkConfiguration = new NetworkConfiguration()
{
LimitConnections = _configuration.LimitConnections,
2022-12-20 12:20:52 -08:00
Protocol = RagonVersion.Parse(_configuration.GameProtocol),
2022-12-17 21:16:02 +04:00
Address = "0.0.0.0",
Port = _configuration.Port,
};
_server.Start(this, networkConfiguration);
_dedicatedThread.Start();
}
public void Stop()
{
_server.Stop();
_dedicatedThread.Interrupt();
}
public void OnConnected(INetworkConnection connection)
{
var context = new PlayerContext(connection, new LobbyPlayer(connection));
context.Lobby = _lobby;
context.Loop = _loop;
_logger.Trace($"Connected {connection.Id}");
_contexts.Add(connection.Id, context);
}
public void OnDisconnected(INetworkConnection connection)
{
_logger.Trace($"Disconnected {connection.Id}");
if (_contexts.Remove(connection.Id, out var context))
{
var room = context.Room;
if (room != null)
{
room.RemovePlayer(context.RoomPlayer);
2022-12-20 12:20:52 -08:00
2022-12-17 21:16:02 +04:00
_lobby.RemoveIfEmpty(room);
}
2022-12-20 12:20:52 -08:00
2022-12-17 21:16:02 +04:00
context.Dispose();
}
}
public void OnTimeout(INetworkConnection connection)
{
if (_contexts.Remove(connection.Id, out var context))
context.Dispose();
}
public void OnData(INetworkConnection connection, byte[] data)
{
if (_contexts.TryGetValue(connection.Id, out var context))
_handlerRegistry.Handle(context, data);
}
2022-12-16 00:05:46 +04:00
}