This commit is contained in:
2022-12-16 23:36:51 +04:00
parent 4d8ed1105a
commit fa6ace4dc8
12 changed files with 54 additions and 60 deletions
@@ -1,6 +1,6 @@
namespace Ragon.Core.Time; namespace Ragon.Core.Time;
public interface IScheduleTask public interface IAction
{ {
public void Tick(); public void Tick();
} }
+27
View File
@@ -0,0 +1,27 @@
namespace Ragon.Core.Time;
public class Loop
{
private List<IAction> _tasks;
public Loop()
{
_tasks = new List<IAction>(35);
}
public void Run(IAction task)
{
_tasks.Add(task);
}
public void Stop(IAction task)
{
_tasks.Remove(task);
}
public void Tick()
{
foreach (var task in _tasks)
task.Tick();
}
}
+12 -13
View File
@@ -10,25 +10,24 @@ namespace Ragon.Core;
public class Application : INetworkListener public class Application : INetworkListener
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly INetworkServer _server;
private INetworkServer _server; private readonly Thread _dedicatedThread;
private Thread _dedicatedThread; private readonly Configuration _configuration;
private Configuration _configuration; private readonly HandlerRegistry _handlerRegistry;
private HandlerRegistry _handlerRegistry; private readonly ILobby _lobby;
private ILobby _lobby; private readonly Loop _loop;
private Scheduler _scheduler; private readonly Dictionary<ushort, PlayerContext> _contexts;
private Dictionary<ushort, PlayerContext> _contexts = new();
public Application(Configuration configuration) public Application(Configuration configuration)
{ {
_configuration = configuration; _configuration = configuration;
_dedicatedThread = new Thread(Execute); _dedicatedThread = new Thread(Execute);
_dedicatedThread.IsBackground = true; _dedicatedThread.IsBackground = true;
_contexts = new Dictionary<ushort, PlayerContext>();
_handlerRegistry = new HandlerRegistry(); _handlerRegistry = new HandlerRegistry();
_lobby = new LobbyInMemory(); _lobby = new LobbyInMemory();
_scheduler = new Scheduler(); _loop = new Loop();
if (configuration.Socket == "enet") if (configuration.Socket == "enet")
_server = new ENetServer(); _server = new ENetServer();
@@ -40,7 +39,7 @@ public class Application : INetworkListener
{ {
while (true) while (true)
{ {
_scheduler.Tick(); _loop.Tick();
_server.Poll(); _server.Poll();
Thread.Sleep((int) 1000.0f / _configuration.SendRate); Thread.Sleep((int) 1000.0f / _configuration.SendRate);
@@ -71,7 +70,7 @@ public class Application : INetworkListener
{ {
var context = new PlayerContext(connection, new LobbyPlayer(connection)); var context = new PlayerContext(connection, new LobbyPlayer(connection));
context.Lobby = _lobby; context.Lobby = _lobby;
context.Scheduler = _scheduler; context.Loop = _loop;
_logger.Trace($"Connected {connection.Id}"); _logger.Trace($"Connected {connection.Id}");
_contexts.Add(connection.Id, context); _contexts.Add(connection.Id, context);
+1 -1
View File
@@ -78,7 +78,7 @@ public class Entity
player.Connection.ReliableChannel.Send(sendData); player.Connection.ReliableChannel.Send(sendData);
} }
public void Destroy() public void Destroy(byte[] payload)
{ {
var room = Owner.Room; var room = Owner.Room;
var serializer = room.Writer; var serializer = room.Writer;
+4 -6
View File
@@ -1,8 +1,9 @@
using Ragon.Common; using Ragon.Common;
using Ragon.Core.Time;
namespace Ragon.Core.Game; namespace Ragon.Core.Game;
public class Room public class Room: IAction
{ {
public string Id { get; } public string Id { get; }
public RoomInformation Info { get; } public RoomInformation Info { get; }
@@ -22,15 +23,12 @@ public class Room
private RagonSerializer _writer; private RagonSerializer _writer;
public RagonSerializer Writer => _writer; public RagonSerializer Writer => _writer;
public PluginBase Plugin { get; set; }
public Room(string roomId, RoomInformation info, PluginBase plugin) public Room(string roomId, RoomInformation info)
{ {
Id = roomId; Id = roomId;
Info = info; Info = info;
Plugin = plugin;
Players = new Dictionary<ushort, RoomPlayer>(info.Max); Players = new Dictionary<ushort, RoomPlayer>(info.Max);
WaitPlayersList = new List<RoomPlayer>(info.Max); WaitPlayersList = new List<RoomPlayer>(info.Max);
ReadyPlayersList = new List<RoomPlayer>(info.Max); ReadyPlayersList = new List<RoomPlayer>(info.Max);
@@ -68,7 +66,7 @@ public class Room
DynamicEntitiesList.Remove(entity); DynamicEntitiesList.Remove(entity);
_entitiesDirtySet.Remove(entity); _entitiesDirtySet.Remove(entity);
entity.Destroy(); entity.Destroy(payload);
currentOwner.Entities.Remove(entity); currentOwner.Entities.Remove(entity);
} }
+1 -1
View File
@@ -27,7 +27,7 @@ public sealed class HandlerRegistry
_reader = new RagonSerializer(2048); _reader = new RagonSerializer(2048);
_writer = new RagonSerializer(2048); _writer = new RagonSerializer(2048);
_authorizationHandler = new AuthHandler(); _authorizationHandler = new AuthorizationHandler();
_joinOrCreateHandler = new JoinOrCreateHandler(); _joinOrCreateHandler = new JoinOrCreateHandler();
_sceneLoadedHandler = new SceneLoadedHandler(); _sceneLoadedHandler = new SceneLoadedHandler();
_createHandler = new CreateHandler(); _createHandler = new CreateHandler();
@@ -4,7 +4,7 @@ using Ragon.Core.Lobby;
namespace Ragon.Core.Handlers; namespace Ragon.Core.Handlers;
public sealed class AuthHandler: IHandler public sealed class AuthorizationHandler: IHandler
{ {
private Logger _logger = LogManager.GetCurrentClassLogger(); private Logger _logger = LogManager.GetCurrentClassLogger();
+3 -1
View File
@@ -50,7 +50,7 @@ public sealed class CreateHandler: IHandler
var lobbyPlayer = context.LobbyPlayer; var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var room = new Room(roomId, information, new PluginBase()); var room = new Room(roomId, information);
room.AddPlayer(roomPlayer); room.AddPlayer(roomPlayer);
context.Room?.RemovePlayer(context.RoomPlayer); context.Room?.RemovePlayer(context.RoomPlayer);
@@ -62,6 +62,8 @@ public sealed class CreateHandler: IHandler
JoinSuccess(roomPlayer, room, writer); JoinSuccess(roomPlayer, room, writer);
context.Loop.Run(room);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
} }
@@ -44,7 +44,7 @@ public sealed class JoinOrCreateHandler : IHandler
Min = _roomParameters.Min, Min = _roomParameters.Min,
}; };
var room = new Room(roomId, information, new PluginBase()); var room = new Room(roomId, information);
context.Lobby.Persist(room); context.Lobby.Persist(room);
var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
@@ -57,6 +57,8 @@ public sealed class JoinOrCreateHandler : IHandler
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}"); _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
JoinSuccess(roomPlayer, room, writer); JoinSuccess(roomPlayer, room, writer);
context.Loop.Run(room);
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ namespace Ragon.Core;
public class PlayerContext: IDisposable public class PlayerContext: IDisposable
{ {
public INetworkConnection Connection { get; } public INetworkConnection Connection { get; }
public Scheduler Scheduler; public Loop Loop;
public ILobby Lobby { get; set; } public ILobby Lobby { get; set; }
public LobbyPlayer LobbyPlayer { private set; get; } public LobbyPlayer LobbyPlayer { private set; get; }
public Room? Room { get; set; } public Room? Room { get; set; }
-27
View File
@@ -1,27 +0,0 @@
namespace Ragon.Core.Time;
public class Scheduler
{
private List<IScheduleTask> _tasks;
public Scheduler()
{
_tasks = new List<IScheduleTask>(35);
}
public void Add(IScheduleTask task)
{
_tasks.Add(task);
}
public void Remove(IScheduleTask task)
{
_tasks.Remove(task);
}
public void Tick()
{
foreach (var task in _tasks)
task.Tick();
}
}
-7
View File
@@ -5,13 +5,6 @@ using Ragon.Core;
namespace Ragon.Relay namespace Ragon.Relay
{ {
[StructLayout(LayoutKind.Sequential)]
struct Serializer
{
[FieldOffset(0)] public Guid Uuid;
[FieldOffset(0)] public long Long0;
[FieldOffset(0)] public long Long1;
}
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)