Files
Ragon/Ragon/Sources/Game/GameThread.cs
T

125 lines
3.3 KiB
C#
Raw Normal View History

2022-06-23 20:45:41 +04:00
using System;
using System.Threading;
2022-07-30 14:07:48 +04:00
using Ragon.Common;
2022-06-23 20:45:57 +04:00
using ENet;
2022-06-23 20:45:41 +04:00
using NLog;
namespace Ragon.Core
{
2022-06-23 20:45:57 +04:00
public class GameThread : IGameThread, IHandler
2022-06-23 20:45:41 +04:00
{
private readonly RoomManager _roomManager;
private readonly Thread _thread;
private readonly Lobby _lobby;
2022-08-20 22:05:43 +04:00
private readonly ISocketServer _server;
private readonly IDispatcherInternal _dispatcherInternal;
private readonly IDispatcher _dispatcher;
2022-06-23 20:45:41 +04:00
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly float _deltaTime = 0.0f;
2022-06-25 11:08:50 +04:00
private readonly Configuration _configuration;
2022-08-20 22:05:43 +04:00
public ISocketServer Server => _server;
public IDispatcher ThreadDispatcher => _dispatcher;
2022-06-25 11:08:50 +04:00
2022-06-23 20:45:41 +04:00
public GameThread(PluginFactory factory, Configuration configuration)
{
2022-08-20 22:05:43 +04:00
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
2022-06-25 11:08:50 +04:00
_configuration = configuration;
var dispatcher = new Dispatcher();
_dispatcherInternal = dispatcher;
2022-08-20 22:05:43 +04:00
_dispatcher = dispatcher;
2022-07-30 14:07:48 +04:00
2022-08-20 22:05:43 +04:00
_server = new ENetServer(this);
2022-06-25 11:08:50 +04:00
_deltaTime = 1000.0f / configuration.SendRate;
2022-06-23 20:45:41 +04:00
_roomManager = new RoomManager(factory, this);
_lobby = new Lobby(authorizationProvider, _roomManager, this);
2022-06-25 11:08:50 +04:00
2022-06-23 20:45:41 +04:00
_thread = new Thread(Execute);
_thread.Name = "Game Thread";
_thread.IsBackground = true;
}
public void Start()
{
2022-08-20 12:27:04 +04:00
var strings = _configuration.Protocol.Split(".");
if (strings.Length < 3)
{
_logger.Error("Wrong protocol passed to connect method");
return;
}
2022-08-20 22:05:43 +04:00
2022-08-20 12:27:04 +04:00
var parts = new uint[] {0, 0, 0};
for (int i = 0; i < parts.Length; i++)
{
if (!uint.TryParse(strings[i], out var v))
{
_logger.Error("Wrong protocol");
return;
}
2022-08-20 22:05:43 +04:00
2022-08-20 12:27:04 +04:00
parts[i] = v;
}
2022-06-25 11:08:50 +04:00
2022-08-20 22:05:43 +04:00
uint encoded = (parts[0] << 16) | (parts[1] << 8) | parts[2];
_server.Start(_configuration.Port, _configuration.MaxConnections, encoded);
2022-06-23 20:45:41 +04:00
_thread.Start();
}
public void Stop()
{
2022-08-20 22:05:43 +04:00
_server.Stop();
2022-06-23 20:45:41 +04:00
_thread.Interrupt();
}
private void Execute()
{
while (true)
{
2022-08-20 22:05:43 +04:00
_server.Process();
2022-06-25 11:08:50 +04:00
_dispatcherInternal.Process();
2022-08-20 22:05:43 +04:00
_roomManager.Tick(_deltaTime);
2022-06-25 11:08:50 +04:00
2022-08-20 22:05:43 +04:00
Thread.Sleep((int) _deltaTime);
2022-06-23 20:45:41 +04:00
}
}
2022-06-23 20:45:57 +04:00
public void OnEvent(Event evnt)
2022-06-23 20:45:41 +04:00
{
2022-06-25 11:08:50 +04:00
if (evnt.Type == EventType.Timeout || evnt.Type == EventType.Disconnect)
2022-06-23 20:45:57 +04:00
{
2022-06-25 11:08:50 +04:00
var player = _lobby.AuthorizationManager.GetPlayer(evnt.Peer.ID);
if (player != null)
_roomManager.Left(player, Array.Empty<byte>());
2022-06-23 20:45:41 +04:00
2022-06-23 20:45:57 +04:00
_lobby.OnDisconnected(evnt.Peer.ID);
}
2022-06-25 11:08:50 +04:00
if (evnt.Type == EventType.Receive)
2022-06-23 20:45:57 +04:00
{
try
{
2022-08-14 12:36:12 +04:00
var peerId = (ushort) evnt.Peer.ID;
2022-06-23 20:45:57 +04:00
var dataRaw = new byte[evnt.Packet.Length];
evnt.Packet.CopyTo(dataRaw);
2022-06-25 11:08:50 +04:00
2022-06-23 20:45:57 +04:00
var data = new ReadOnlySpan<byte>(dataRaw);
2022-07-30 14:07:48 +04:00
var operation = (RagonOperation) data[0];
var payload = data.Slice(1, data.Length - 1);
2022-08-20 22:05:43 +04:00
2022-06-25 11:08:50 +04:00
if (_roomManager.RoomsBySocket.TryGetValue(peerId, out var room))
2022-07-30 14:07:48 +04:00
room.ProcessEvent(peerId, operation, payload);
2022-08-20 22:05:43 +04:00
2022-07-30 14:07:48 +04:00
_lobby.ProcessEvent(peerId, operation, payload);
2022-06-23 20:45:57 +04:00
}
catch (Exception exception)
{
_logger.Error(exception);
}
}
2022-06-23 20:45:41 +04:00
}
}
}