1.0.11-rc

This commit is contained in:
2022-08-20 12:27:04 +04:00
parent 65c1d9c6d4
commit 6ec29e5dcd
13 changed files with 204 additions and 129 deletions
@@ -25,11 +25,11 @@ public class AuthorizationManager : IAuthorizationManager
_playersByPeers = new Dictionary<uint, Player>();
}
public void OnAuthorization(uint peerId, string key, string name, byte protocol)
public void OnAuthorization(uint peerId, string key, string name)
{
var dispatcher = _gameThread.ThreadDispatcher;
_provider.OnAuthorizationRequest(key, name, protocol, Array.Empty<byte>(),
_provider.OnAuthorizationRequest(key, name, Array.Empty<byte>(),
(playerId, playerName) => { dispatcher.Dispatch(() => Accepted(peerId, playerId, playerName)); },
(errorCode) => { dispatcher.Dispatch(() => Rejected(peerId, errorCode)); });
}
@@ -5,5 +5,5 @@ namespace Ragon.Core;
public interface IAuthorizationProvider
{
Task OnAuthorizationRequest(string key, string playerName, byte protocol, byte[] additionalData, Action<string, string> Accept, Action<uint> Reject);
Task OnAuthorizationRequest(string key, string playerName, byte[] additionalData, Action<string, string> Accept, Action<uint> Reject);
}
@@ -6,6 +6,7 @@ namespace Ragon.Core
public struct Configuration
{
public string Key;
public string Protocol;
public int StatisticsInterval;
public ushort SendRate;
public ushort Port;
+16 -10
View File
@@ -1,33 +1,39 @@
using System;
using Ragon.Common;
namespace Ragon.Core;
public class EntityProperty
{
public int Size => _data.Length;
public bool IsDirty => _dirty;
private bool _dirty;
public int Size { get; set; }
public bool IsDirty { get; private set; }
public bool IsFixed { get; private set; }
private byte[] _data;
public EntityProperty(int size)
public EntityProperty(int size, bool isFixed)
{
_data = new byte[size];
_data = new byte[512];
Size = size;
IsFixed = isFixed;
IsDirty = true;
}
public ReadOnlySpan<byte> Read()
{
return _data.AsSpan();
var dataSpan = _data.AsSpan();
return dataSpan.Slice(0, Size);
}
public void Write(ref ReadOnlySpan<byte> src)
{
src.CopyTo(_data);
_dirty = true;
IsDirty = true;
}
public void Clear()
{
_dirty = false;
IsDirty = false;
}
}
+60 -43
View File
@@ -12,6 +12,7 @@ namespace Ragon.Core
public int PlayersMin { get; private set; }
public int PlayersMax { get; private set; }
public int PlayersCount => _players.Count;
public int EntitiesCount => _entities.Count;
public string Id { get; private set; }
public string Map { get; private set; }
@@ -116,6 +117,20 @@ namespace Ragon.Core
Broadcast(_readyPlayers, sendData);
}
if (_allPlayers.Length > 0)
{
var nextOwnerId = _allPlayers[0];
_owner = nextOwnerId;
var nextOwner = _players[nextOwnerId];
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
_serializer.WriteString(nextOwner.Id);
var sendData = _serializer.ToArray();
Broadcast(_readyPlayers, sendData);
}
_entitiesAll = _entities.Values.ToArray();
}
}
@@ -129,29 +144,40 @@ namespace Ragon.Core
{
case RagonOperation.REPLICATE_ENTITY_STATE:
{
var entityId = _serializer.ReadUShort();
if (_entities.TryGetValue(entityId, out var ent))
var entitiesCount = _serializer.ReadUShort();
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
{
if (ent.OwnerId != peerId)
var entityId = _serializer.ReadUShort();
if (_entities.TryGetValue(entityId, out var ent))
{
_logger.Warn($"Not owner can't change properties of object {entityId}");
return;
}
var mask = _serializer.ReadLong();
for (var i = 0; i < ent.Properties.Length; i++)
{
if (((mask >> i) & 1) == 1)
if (ent.OwnerId != peerId)
{
var propertyPayload = _serializer.ReadData(ent.Properties[i].Size);
ent.Properties[i].Write(ref propertyPayload);
_logger.Warn($"Not owner can't change properties of object {entityId}");
return;
}
for (var i = 0; i < ent.Properties.Length; i++)
{
if (_serializer.ReadBool())
{
var property = ent.Properties[i];
if (!property.IsFixed)
property.Size = _serializer.ReadUShort();
var propertyPayload = _serializer.ReadData(property.Size);
property.Write(ref propertyPayload);
}
}
if (_entitiesDirtySet.Add(ent))
_entitiesDirty.Add(ent);
}
else
{
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
break;
}
if (_entitiesDirtySet.Add(ent))
_entitiesDirty.Add(ent);
}
break;
}
case RagonOperation.REPLICATE_ENTITY_EVENT:
@@ -182,6 +208,7 @@ namespace Ragon.Core
_serializer.WriteByte(evntMode);
_serializer.WriteUShort(entityId);
_serializer.WriteData(ref payload);
var sendData = _serializer.ToArray();
switch (targetMode)
@@ -273,22 +300,18 @@ namespace Ragon.Core
break;
}
case RagonOperation.CREATE_STATIC_ENTITY:
case RagonOperation.CREATE_SCENE_ENTITY:
{
var entityType = _serializer.ReadUShort();
var staticId = _serializer.ReadUShort();
var propertiesCount = _serializer.ReadUShort();
if (propertiesCount > 63)
{
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
return;
}
var entity = new Entity(peerId, entityType, staticId, RagonAuthority.ALL, RagonAuthority.OWNER_ONLY, propertiesCount);
for (var i = 0; i < propertiesCount; i++)
{
var propertyType = _serializer.ReadBool();
var propertySize = _serializer.ReadUShort();
entity.Properties[i] = new EntityProperty(propertySize);
entity.Properties[i] = new EntityProperty(propertySize, propertyType);
}
{
@@ -308,7 +331,7 @@ namespace Ragon.Core
_plugin.OnEntityCreated(player, entity);
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.CREATE_STATIC_ENTITY);
_serializer.WriteOperation(RagonOperation.CREATE_SCENE_ENTITY);
_serializer.WriteUShort(entityType);
_serializer.WriteUShort(entity.EntityId);
_serializer.WriteUShort(staticId);
@@ -328,16 +351,13 @@ namespace Ragon.Core
var entityType = _serializer.ReadUShort();
var propertiesCount = _serializer.ReadUShort();
var entity = new Entity(peerId, entityType, 0, RagonAuthority.ALL, RagonAuthority.ALL, propertiesCount);
if (propertiesCount > 63)
{
_logger.Warn($"Allowed only 64 properties per entity. EntityType(Static) {entityType}");
return;
}
// _logger.Trace("Created entity with properties: " + propertiesCount);
for (var i = 0; i < propertiesCount; i++)
{
var propertyType = _serializer.ReadBool();
var propertySize = _serializer.ReadUShort();
entity.Properties[i] = new EntityProperty(propertySize);
entity.Properties[i] = new EntityProperty(propertySize, propertyType);
// _logger.Trace($"Property: {i} Size: {propertySize} IsFixed: {propertyType}");
}
{
@@ -461,38 +481,35 @@ namespace Ragon.Core
_serializer.Clear();
_serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
_serializer.WriteUShort((ushort) _entitiesDirty.Count);
for (var entityIndex = 0; entityIndex < _entitiesDirty.Count; entityIndex++)
{
var entity = _entitiesDirty[entityIndex];
var mask = 0L;
_serializer.WriteUShort(entity.EntityId);
var offset = _serializer.Lenght;
_serializer.WriteLong(mask);
for (int propertyIndex = 0; propertyIndex < entity.Properties.Length; propertyIndex++)
{
var property = entity.Properties[propertyIndex];
if (property.IsDirty)
{
mask |= (uint) (1 << propertyIndex);
_serializer.WriteBool(true);
var span = _serializer.GetWritableData(property.Size);
var data = property.Read();
data.CopyTo(span);
property.Clear();
}
else
{
_serializer.WriteBool(false);
}
}
_serializer.WriteLong(mask, offset);
}
_entitiesDirty.Clear();
_entitiesDirtySet.Clear();
var sendData = _serializer.ToArray();
Broadcast(_readyPlayers, sendData, DeliveryType.Unreliable);
Broadcast(_readyPlayers, sendData);
}
}
+37 -3
View File
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using Ragon.Common;
using ENet;
@@ -12,10 +14,14 @@ namespace Ragon.Core
private readonly RoomManager _roomManager;
private readonly Thread _thread;
private readonly Stopwatch _gameLoopTimer;
private readonly Stopwatch _statisticsTimer;
private readonly Stopwatch _serverTimer;
private readonly Stopwatch _logicTimer;
private readonly Lobby _lobby;
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly float _deltaTime = 0.0f;
private readonly Stopwatch _statisticsTimer;
private readonly Configuration _configuration;
private readonly IDispatcherInternal _dispatcherInternal;
@@ -40,6 +46,8 @@ namespace Ragon.Core
_gameLoopTimer = new Stopwatch();
_statisticsTimer = new Stopwatch();
_serverTimer = new Stopwatch();
_logicTimer = new Stopwatch();
_thread = new Thread(Execute);
_thread.Name = "Game Thread";
@@ -48,10 +56,30 @@ namespace Ragon.Core
public void Start()
{
Server.Start(_configuration.Port, _configuration.MaxConnections);
var strings = _configuration.Protocol.Split(".");
if (strings.Length < 3)
{
_logger.Error("Wrong protocol passed to connect method");
return;
}
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;
}
parts[i] = v;
}
uint encoded = (parts[0] << 16) | (parts[1] << 8) | parts[2];
Server.Start(_configuration.Port, _configuration.MaxConnections, encoded);
_gameLoopTimer.Start();
_statisticsTimer.Start();
_logicTimer.Start();
_serverTimer.Start();
_thread.Start();
}
@@ -68,6 +96,9 @@ namespace Ragon.Core
{
while (true)
{
_logicTimer.Restart();
_serverTimer.Restart();
Server.Process();
_dispatcherInternal.Process();
@@ -82,7 +113,10 @@ namespace Ragon.Core
if (_statisticsTimer.Elapsed.Seconds > _configuration.StatisticsInterval && _roomManager.RoomsBySocket.Count > 0)
{
_logger.Trace($"Rooms: {_roomManager.Rooms.Count} Clients: {_roomManager.RoomsBySocket.Count}");
var rooms = _roomManager.Rooms.Count;
var clients = _roomManager.RoomsBySocket.Count;
var entities = _roomManager.Rooms.Select(r => r.EntitiesCount).Sum();
_logger.Trace($"Rooms: {rooms} Clients: {clients} Entities: {entities}");
_statisticsTimer.Restart();
}
}
+1 -2
View File
@@ -32,8 +32,7 @@ public class Lobby : ILobby
{
var key = _serializer.ReadString();
var playerName = _serializer.ReadString();
var protocol = _serializer.ReadByte();
_authorizationManager.OnAuthorization(peerId, key, playerName, protocol);
_authorizationManager.OnAuthorization(peerId, key, playerName);
return;
}
+29 -22
View File
@@ -1,46 +1,42 @@
using System;
using System.Diagnostics;
using System.Timers;
using ENet;
using NLog;
namespace Ragon.Core
{
public enum Status
{
Stopped,
Listening,
Disconnecting,
Connecting,
Assigning,
Connected
}
public class ENetServer : ISocketServer
{
public Status Status { get; private set; }
private ILogger _logger = LogManager.GetCurrentClassLogger();
private Host _host;
private uint _protocol;
private Address _address;
private Event _netEvent;
private Peer[] _peers;
private IHandler _handler;
private Stopwatch _timer;
public ENetServer(IHandler handler)
{
_handler = handler;
_timer = Stopwatch.StartNew();
_peers = Array.Empty<Peer>();
_host = new Host();
}
public void Start(ushort port, int connections)
public void Start(ushort port, int connections, uint protocol)
{
_address = default;
_address.Port = port;
_peers = new Peer[connections];
_host = new Host();
_protocol = protocol;
_host.Create(_address, connections, 2, 0, 0, 1024 * 1024);
Status = Status.Listening;
var protocolDecoded = (protocol >> 16 & 0xFF) + "." + (protocol >> 8 & 0xFF) + "." + (protocol & 0xFF);
_logger.Info($"Network listening on {port}");
_logger.Info($"Protocol: {protocolDecoded}");
}
public void Broadcast(uint[] peersIds, byte[] data, DeliveryType type)
@@ -57,14 +53,14 @@ namespace Ragon.Core
else if (type == DeliveryType.Unreliable)
{
channel = 1;
packetFlags = PacketFlags.None;
packetFlags = PacketFlags.UnreliableFragmented;
}
newPacket.Create(data, data.Length, packetFlags);
foreach (var peerId in peersIds)
_peers[peerId].Send(channel, ref newPacket);
}
public void Send(uint peerId, byte[] data, DeliveryType type)
{
var newPacket = new Packet();
@@ -107,11 +103,17 @@ namespace Ragon.Core
switch (_netEvent.Type)
{
case EventType.None:
Console.WriteLine("None event");
{
_logger.Trace("None event");
break;
}
case EventType.Connect:
{
// if (IsValidProtocol(_netEvent.Data))
// {
// _logger.Warn("Mismatched protocol, close connection");
// break;
// }
_peers[_netEvent.Peer.ID] = _netEvent.Peer;
_handler.OnEvent(_netEvent);
break;
@@ -140,5 +142,10 @@ namespace Ragon.Core
{
_host?.Dispose();
}
private bool IsValidProtocol(uint protocol)
{
return protocol == _protocol;
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ namespace Ragon.Core;
public interface ISocketServer
{
public void Start(ushort port, int connections);
public void Start(ushort port, int connections, uint protocol);
public void Process();
public void Stop();
public void Send(uint peerId, byte[] data, DeliveryType type);