This commit is contained in:
2022-06-25 11:08:50 +04:00
parent 679608bc48
commit 1e41b9f2eb
31 changed files with 152 additions and 645 deletions
+6 -8
View File
@@ -19,7 +19,6 @@ namespace Ragon.Core
private Dictionary<uint, Player> _players = new();
private Dictionary<int, Entity> _entities = new();
private uint _owner;
private uint _ticks;
private readonly PluginBase _plugin;
private readonly IGameThread _gameThread;
@@ -40,7 +39,6 @@ namespace Ragon.Core
PlayersMax = max;
Id = Guid.NewGuid().ToString();
_logger.Info($"Room created with plugin: {_plugin.GetType().Name}");
_plugin.Attach(this);
}
@@ -119,7 +117,9 @@ namespace Ragon.Core
{
var newRoomOwnerId = _allPlayers[0];
var newRoomOwner = _players[newRoomOwnerId];
_owner = newRoomOwnerId;
{
_plugin.OnOwnershipChanged(newRoomOwner);
@@ -323,8 +323,7 @@ namespace Ragon.Core
public void Tick(float deltaTime)
{
_ticks++;
_plugin.OnTick(_ticks, deltaTime);
_plugin.OnTick(deltaTime);
foreach (var entity in _entitiesAll)
{
@@ -347,15 +346,12 @@ namespace Ragon.Core
public void Start()
{
_logger.Info("Room started");
_plugin.OnStart();
}
public void Stop()
{
_logger.Info("Room stopped");
_plugin.OnStop();
_plugin.Detach();
}
@@ -365,6 +361,8 @@ namespace Ragon.Core
public Player GetOwner() => _players[_owner];
public IDispatcher GetThreadDispatcher() => _gameThread.ThreadDispatcher;
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable)
{
_gameThread.Server.Send(peerId, rawData, deliveryType);
+33 -41
View File
@@ -9,37 +9,37 @@ namespace Ragon.Core
{
public class GameThread : IGameThread, IHandler
{
private readonly Dictionary<uint, GameRoom> _socketByRooms;
private readonly RoomManager _roomManager;
private readonly ISocketServer _server;
private readonly Thread _thread;
private readonly Server _serverConfiguration;
private readonly Stopwatch _gameLoopTimer;
private readonly Lobby _lobby;
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly float _deltaTime = 0.0f;
private readonly Stopwatch _packetsTimer;
private int _packets = 0;
public IDispatcher Dispatcher { get; private set; }
private readonly Stopwatch _statisticsTimer;
private readonly Configuration _configuration;
private readonly IDispatcherInternal _dispatcherInternal;
public IDispatcher ThreadDispatcher { get; private set; }
public ISocketServer Server { get; private set; }
public GameThread(PluginFactory factory, Configuration configuration)
{
_configuration = configuration;
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
var dispatcher = new Dispatcher();
_dispatcherInternal = dispatcher;
Dispatcher = new Dispatcher();
ThreadDispatcher = dispatcher;
Server = new ENetServer(this);
_serverConfiguration = configuration.Server;
_deltaTime = 1000.0f / configuration.TickRate;
_deltaTime = 1000.0f / configuration.SendRate;
_roomManager = new RoomManager(factory, this);
_lobby = new Lobby(authorizationProvider, _roomManager, this);
_gameLoopTimer = new Stopwatch();
_packetsTimer = new Stopwatch();
_socketByRooms = new Dictionary<uint, GameRoom>();
_statisticsTimer = new Stopwatch();
_thread = new Thread(Execute);
_thread.Name = "Game Thread";
@@ -48,19 +48,19 @@ namespace Ragon.Core
public void Start()
{
Server.Start(_serverConfiguration.Port);
Server.Start(_configuration.Port, _configuration.MaxConnections);
_gameLoopTimer.Start();
_packetsTimer.Start();
_statisticsTimer.Start();
_thread.Start();
}
public void Stop()
{
Server.Stop();
_gameLoopTimer.Stop();
_packetsTimer.Stop();
_statisticsTimer.Stop();
_thread.Interrupt();
}
@@ -69,8 +69,9 @@ namespace Ragon.Core
while (true)
{
Server.Process();
Dispatcher.Process();
_dispatcherInternal.Process();
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
if (elapsedMilliseconds > _deltaTime)
{
@@ -79,47 +80,38 @@ namespace Ragon.Core
continue;
}
if (_packetsTimer.Elapsed.Seconds > 1)
if (_statisticsTimer.Elapsed.Seconds > _configuration.StatisticsInterval && _roomManager.RoomsBySocket.Count > 0)
{
_logger.Trace($"Clients: {_socketByRooms.Keys.Count} Packets: {_packets} per sec");
_packetsTimer.Restart();
_packets = 0;
_logger.Trace($"Rooms: {_roomManager.Rooms.Count} Clients: {_roomManager.RoomsBySocket.Count}");
_statisticsTimer.Restart();
}
Thread.Sleep(15);
}
}
public void Attach(uint peerId, GameRoom room)
{
_socketByRooms.Add(peerId, room);
}
public void Detach(uint peerId)
{
_socketByRooms.Remove(peerId);
}
public void OnEvent(Event evnt)
{
if (evnt.Type == ENet.EventType.Timeout || evnt.Type == ENet.EventType.Disconnect)
if (evnt.Type == EventType.Timeout || evnt.Type == EventType.Disconnect)
{
if (_socketByRooms.Remove(evnt.Peer.ID, out var room))
room.Leave(evnt.Peer.ID);
var player = _lobby.AuthorizationManager.GetPlayer(evnt.Peer.ID);
if (player != null)
_roomManager.Left(player, Array.Empty<byte>());
_lobby.OnDisconnected(evnt.Peer.ID);
}
if (evnt.Type == ENet.EventType.Receive)
if (evnt.Type == EventType.Receive)
{
_packets += 1;
try
{
var peerId = evnt.Peer.ID;
var dataRaw = new byte[evnt.Packet.Length];
evnt.Packet.CopyTo(dataRaw);
var data = new ReadOnlySpan<byte>(dataRaw);
if (_socketByRooms.TryGetValue(peerId, out var room))
if (_roomManager.RoomsBySocket.TryGetValue(peerId, out var room))
{
room.ProcessEvent(peerId, data);
}
+1
View File
@@ -11,6 +11,7 @@ public interface IGameRoom
public Player GetPlayerById(uint peerId);
public Entity GetEntityById(int entityId);
public Player GetOwner();
public IDispatcher GetThreadDispatcher();
public void Send(uint peerId, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable);
public void Broadcast(uint[] peersIds, byte[] rawData, DeliveryType deliveryType = DeliveryType.Unreliable);
+1 -3
View File
@@ -4,8 +4,6 @@ namespace Ragon.Core;
public interface IGameThread
{
public void Attach(uint peerId, GameRoom room);
public void Detach(uint peerId);
public IDispatcher Dispatcher { get; }
public IDispatcher ThreadDispatcher { get; }
public ISocketServer Server { get; }
}