Files
Ragon/Ragon.Server/Sources/RagonServer.cs
T

240 lines
8.0 KiB
C#
Raw Permalink Normal View History

2023-03-06 10:06:43 +04:00
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Diagnostics;
using NLog;
using Ragon.Protocol;
2023-04-09 11:06:52 +04:00
using Ragon.Server.Handler;
2023-04-13 20:42:05 +04:00
using Ragon.Server.Http;
2023-04-09 11:06:52 +04:00
using Ragon.Server.IO;
using Ragon.Server.Lobby;
2023-04-09 10:52:18 +04:00
using Ragon.Server.Plugin;
2023-04-09 11:06:52 +04:00
using Ragon.Server.Plugin.Web;
2023-04-09 10:52:18 +04:00
using Ragon.Server.Time;
2023-03-06 10:06:43 +04:00
namespace Ragon.Server;
2023-04-13 20:42:05 +04:00
public class RagonServer : IRagonServer, INetworkListener
2023-03-06 10:06:43 +04:00
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly INetworkServer _server;
2023-10-07 19:30:52 +03:00
private readonly BaseOperation[] _handlers;
2023-04-13 20:42:05 +04:00
private readonly IRagonLobby _lobby;
private readonly IServerPlugin _serverPlugin;
2023-03-06 10:06:43 +04:00
private readonly Thread _dedicatedThread;
private readonly Executor _executor;
2023-07-01 07:47:57 +03:00
private readonly RagonServerConfiguration _configuration;
2023-04-13 20:42:05 +04:00
private readonly RagonWebHookPlugin _webhooks;
private readonly RagonHttpServer _httpServer;
2023-03-06 10:06:43 +04:00
private readonly RagonBuffer _reader;
private readonly RagonBuffer _writer;
private readonly RagonScheduler _scheduler;
2023-04-13 20:42:05 +04:00
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
2023-04-09 10:52:18 +04:00
private readonly Stopwatch _timer;
private readonly long _tickRate = 0;
2023-10-07 19:30:52 +03:00
2023-04-09 10:52:18 +04:00
public RagonServer(
INetworkServer server,
IServerPlugin plugin,
2023-07-01 07:47:57 +03:00
RagonServerConfiguration configuration)
2023-03-06 10:06:43 +04:00
{
_server = server;
_executor = _server.Executor;
_configuration = configuration;
2023-04-13 20:42:05 +04:00
_serverPlugin = plugin;
_contextsByConnection = new Dictionary<ushort, RagonContext>();
_contextsByPlayerId = new Dictionary<string, RagonContext>();
2023-03-06 10:06:43 +04:00
_lobby = new LobbyInMemory();
_scheduler = new RagonScheduler();
2023-04-13 20:42:05 +04:00
_webhooks = new RagonWebHookPlugin(this, configuration);
2023-04-09 10:52:18 +04:00
_dedicatedThread = new Thread(Execute);
_dedicatedThread.IsBackground = true;
2023-04-13 20:42:05 +04:00
_httpServer = new RagonHttpServer(_executor, plugin);
2023-03-06 10:06:43 +04:00
_reader = new RagonBuffer();
_writer = new RagonBuffer();
2023-04-09 10:52:18 +04:00
_tickRate = 1000 / _configuration.ServerTickRate;
2023-03-06 10:06:43 +04:00
_timer = new Stopwatch();
2023-10-07 19:30:52 +03:00
2023-04-13 20:42:05 +04:00
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
2023-10-07 19:30:52 +03:00
2023-04-13 20:42:05 +04:00
_serverPlugin.OnAttached(this);
2023-10-07 19:30:52 +03:00
_handlers = new BaseOperation[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _webhooks, contextObserver);
_handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _webhooks);
_handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _webhooks);
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer, _webhooks);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer, _webhooks);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadOperation(_reader, _writer);
_handlers[(byte)RagonOperation.SCENE_LOADED] = new SceneLoadedOperation(_reader, _writer);
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityDestroyOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new EntityStateOperation(_reader, _writer);
_handlers[(byte)RagonOperation.TRANSFER_ROOM_OWNERSHIP] = new EntityOwnershipOperation(_reader, _writer);
_handlers[(byte)RagonOperation.TRANSFER_ENTITY_OWNERSHIP] = new EntityOwnershipOperation(_reader, _writer);
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer);
2023-03-23 18:07:48 +04:00
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
2023-03-06 10:06:43 +04:00
}
public void Execute()
{
_timer.Start();
while (true)
{
2023-04-09 10:52:18 +04:00
if (_timer.ElapsedMilliseconds > _tickRate)
2023-03-06 10:06:43 +04:00
{
_timer.Restart();
2023-10-04 14:42:59 +03:00
_scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f);
2023-10-07 19:30:52 +03:00
2023-10-04 14:42:59 +03:00
SendTimestamp();
2023-03-06 10:06:43 +04:00
}
2023-10-07 19:30:52 +03:00
2023-04-13 20:42:05 +04:00
_executor.Update();
2023-03-06 10:06:43 +04:00
_server.Update();
Thread.Sleep(1);
}
}
public void Start(bool executeInDedicatedThread = false)
{
var networkConfiguration = new NetworkConfiguration()
{
LimitConnections = _configuration.LimitConnections,
Protocol = RagonVersion.Parse(_configuration.GameProtocol),
Address = "0.0.0.0",
Port = _configuration.Port,
};
2023-10-07 19:30:52 +03:00
2023-04-13 20:42:05 +04:00
_httpServer.Start(_configuration);
2023-03-06 10:06:43 +04:00
_server.Start(this, networkConfiguration);
if (executeInDedicatedThread)
_dedicatedThread.Start();
2023-10-07 19:30:52 +03:00
else
2023-03-06 10:06:43 +04:00
Execute();
}
public void Dispose()
{
2023-04-13 20:42:05 +04:00
_serverPlugin.OnDetached();
2023-03-06 10:06:43 +04:00
_server.Stop();
_dedicatedThread.Interrupt();
}
public void OnConnected(INetworkConnection connection)
{
2023-07-01 07:47:57 +03:00
var context = new RagonContext(connection, _configuration, _executor, _lobby, _scheduler);
2023-10-07 19:30:52 +03:00
2023-03-06 10:06:43 +04:00
_logger.Trace($"Connected: {connection.Id}");
2023-04-13 20:42:05 +04:00
_contextsByConnection.Add(connection.Id, context);
2023-03-06 10:06:43 +04:00
}
public void OnDisconnected(INetworkConnection connection)
{
2023-04-13 20:42:05 +04:00
if (_contextsByConnection.Remove(connection.Id, out var context))
2023-03-06 10:06:43 +04:00
{
var room = context.Room;
if (room != null)
{
room.DetachPlayer(context.RoomPlayer);
2023-10-07 19:30:52 +03:00
if (_lobby.RemoveIfEmpty(room))
2023-04-09 10:52:18 +04:00
_webhooks.RoomRemoved(context, room);
2023-03-06 10:06:43 +04:00
}
2023-10-07 19:30:52 +03:00
2023-04-09 10:52:18 +04:00
_logger.Trace($"Disconnected: {connection.Id}");
2023-03-06 10:06:43 +04:00
}
else
{
2023-10-04 14:42:59 +03:00
_logger.Trace($"Disconnected without context: {connection.Id}");
2023-03-06 10:06:43 +04:00
}
}
public void OnTimeout(INetworkConnection connection)
{
2023-10-12 16:09:24 +03:00
if (_contextsByConnection.Remove(connection.Id, out var context) && context.ConnectionStatus == ConnectionStatus.Authorized)
2023-03-06 10:06:43 +04:00
{
var room = context.Room;
if (room != null)
{
room.DetachPlayer(context.RoomPlayer);
_lobby.RemoveIfEmpty(room);
}
2023-10-07 19:30:52 +03:00
2023-03-06 10:06:43 +04:00
_logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
}
else
{
_logger.Trace($"Timeout: {connection.Id}");
}
}
2023-10-07 19:30:52 +03:00
public void OnData(INetworkConnection connection, NetworkChannel channel, byte[] data)
2023-03-06 10:06:43 +04:00
{
try
{
2023-04-13 20:42:05 +04:00
if (_contextsByConnection.TryGetValue(connection.Id, out var context))
2023-03-06 10:06:43 +04:00
{
_writer.Clear();
_reader.Clear();
2023-10-07 19:30:52 +03:00
_reader.FromArray(data);
2023-03-06 10:06:43 +04:00
var operation = _reader.ReadByte();
2023-10-11 19:37:50 +03:00
_handlers[operation].Handle(context, channel);
2023-03-06 10:06:43 +04:00
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
2023-04-14 14:32:04 +04:00
2023-10-04 14:42:59 +03:00
public void SendTimestamp()
{
var timestamp = RagonTime.CurrentTimestamp();
var value = new DoubleToUInt
{
Double = timestamp,
};
2023-10-07 19:30:52 +03:00
2023-10-04 14:42:59 +03:00
_writer.Clear();
_writer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION);
_writer.Write(value.Int0, 32);
_writer.Write(value.Int1, 32);
var sendData = _writer.ToArray();
2023-10-11 19:37:50 +03:00
_server.Broadcast(sendData, NetworkChannel.UNRELIABLE);
2023-10-04 14:42:59 +03:00
}
2023-10-07 19:30:52 +03:00
public BaseOperation ResolveHandler(RagonOperation operation)
2023-04-14 14:32:04 +04:00
{
return _handlers[(byte)operation];
}
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
{
2023-10-07 19:30:52 +03:00
return _contextsByConnection.TryGetValue(connection.Id, out var context) ? context.LobbyPlayer : null;
2023-04-14 14:32:04 +04:00
}
public RagonLobbyPlayer? GetPlayerById(string playerId)
{
2023-10-07 19:30:52 +03:00
return _contextsByPlayerId.TryGetValue(playerId, out var context) ? context.LobbyPlayer : null;
2023-04-14 14:32:04 +04:00
}
2023-03-06 10:06:43 +04:00
}