✨ http-commands
This commit is contained in:
@@ -18,6 +18,7 @@ using System.Diagnostics;
|
||||
using NLog;
|
||||
using Ragon.Protocol;
|
||||
using Ragon.Server.Handler;
|
||||
using Ragon.Server.Http;
|
||||
using Ragon.Server.IO;
|
||||
using Ragon.Server.Lobby;
|
||||
using Ragon.Server.Plugin;
|
||||
@@ -26,20 +27,23 @@ using Ragon.Server.Time;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public class RagonServer : INetworkListener
|
||||
public class RagonServer : IRagonServer, INetworkListener
|
||||
{
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly INetworkServer _server;
|
||||
private readonly IRagonOperation[] _handlers;
|
||||
private readonly IRagonLobby _lobby;
|
||||
private readonly IServerPlugin _serverPlugin;
|
||||
private readonly Thread _dedicatedThread;
|
||||
private readonly Executor _executor;
|
||||
private readonly WebHookPlugin _webhooks;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly IRagonOperation[] _handlers;
|
||||
private readonly RagonWebHookPlugin _webhooks;
|
||||
private readonly RagonHttpServer _httpServer;
|
||||
private readonly RagonBuffer _reader;
|
||||
private readonly RagonBuffer _writer;
|
||||
private readonly IRagonLobby _lobby;
|
||||
private readonly RagonScheduler _scheduler;
|
||||
private readonly Dictionary<ushort, RagonContext> _contexts;
|
||||
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
|
||||
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
|
||||
private readonly Stopwatch _timer;
|
||||
private readonly long _tickRate = 0;
|
||||
|
||||
@@ -51,20 +55,26 @@ public class RagonServer : INetworkListener
|
||||
_server = server;
|
||||
_executor = _server.Executor;
|
||||
_configuration = configuration;
|
||||
_contexts = new Dictionary<ushort, RagonContext>();
|
||||
_serverPlugin = plugin;
|
||||
_contextsByConnection = new Dictionary<ushort, RagonContext>();
|
||||
_contextsByPlayerId = new Dictionary<string, RagonContext>();
|
||||
_lobby = new LobbyInMemory();
|
||||
_scheduler = new RagonScheduler();
|
||||
_webhooks = new WebHookPlugin(this, configuration);
|
||||
_webhooks = new RagonWebHookPlugin(this, configuration);
|
||||
_dedicatedThread = new Thread(Execute);
|
||||
_dedicatedThread.IsBackground = true;
|
||||
|
||||
_httpServer = new RagonHttpServer(_executor, plugin);
|
||||
_reader = new RagonBuffer();
|
||||
_writer = new RagonBuffer();
|
||||
_tickRate = 1000 / _configuration.ServerTickRate;
|
||||
_timer = new Stopwatch();
|
||||
|
||||
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
|
||||
|
||||
_serverPlugin.OnAttached(this);
|
||||
|
||||
_handlers = new IRagonOperation[byte.MaxValue];
|
||||
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, _writer, configuration);
|
||||
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer, configuration);
|
||||
_handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(plugin, _webhooks);
|
||||
_handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation(plugin, _webhooks);
|
||||
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(plugin, _webhooks);
|
||||
@@ -78,8 +88,6 @@ public class RagonServer : INetworkListener
|
||||
|
||||
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
|
||||
}
|
||||
|
||||
public IRagonOperation Resolve(RagonOperation operation) => _handlers[(byte)operation];
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
@@ -88,11 +96,11 @@ public class RagonServer : INetworkListener
|
||||
{
|
||||
if (_timer.ElapsedMilliseconds > _tickRate)
|
||||
{
|
||||
_executor.Update();
|
||||
_scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f);
|
||||
_timer.Restart();
|
||||
}
|
||||
|
||||
_executor.Update();
|
||||
_server.Update();
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
@@ -108,6 +116,7 @@ public class RagonServer : INetworkListener
|
||||
Port = _configuration.Port,
|
||||
};
|
||||
|
||||
_httpServer.Start(_configuration);
|
||||
_server.Start(this, networkConfiguration);
|
||||
|
||||
if (executeInDedicatedThread)
|
||||
@@ -118,6 +127,7 @@ public class RagonServer : INetworkListener
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_serverPlugin.OnDetached();
|
||||
_server.Stop();
|
||||
_dedicatedThread.Interrupt();
|
||||
}
|
||||
@@ -127,12 +137,12 @@ public class RagonServer : INetworkListener
|
||||
var context = new RagonContext(connection, _executor, _lobby, _scheduler);
|
||||
|
||||
_logger.Trace($"Connected: {connection.Id}");
|
||||
_contexts.Add(connection.Id, context);
|
||||
_contextsByConnection.Add(connection.Id, context);
|
||||
}
|
||||
|
||||
public void OnDisconnected(INetworkConnection connection)
|
||||
{
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
if (_contextsByConnection.Remove(connection.Id, out var context))
|
||||
{
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
@@ -152,7 +162,7 @@ public class RagonServer : INetworkListener
|
||||
|
||||
public void OnTimeout(INetworkConnection connection)
|
||||
{
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
if (_contextsByConnection.Remove(connection.Id, out var context))
|
||||
{
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
@@ -173,7 +183,7 @@ public class RagonServer : INetworkListener
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_contexts.TryGetValue(connection.Id, out var context))
|
||||
if (_contextsByConnection.TryGetValue(connection.Id, out var context))
|
||||
{
|
||||
_writer.Clear();
|
||||
_reader.Clear();
|
||||
@@ -188,4 +198,7 @@ public class RagonServer : INetworkListener
|
||||
_logger.Error(ex);
|
||||
}
|
||||
}
|
||||
public IRagonOperation ResolveOperation(RagonOperation operation) => _handlers[(byte)operation];
|
||||
public RagonContext? ResolveContext(INetworkConnection connection) => _contextsByConnection.TryGetValue(connection.Id, out var context) ? context : null;
|
||||
public RagonContext? ResolveContext(string playerId) => _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null;
|
||||
}
|
||||
Reference in New Issue
Block a user