Compare commits

..

6 Commits

22 changed files with 176 additions and 82 deletions
+1 -1
View File
@@ -12,7 +12,7 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>none</DebugType> <DebugType>none</DebugType>
<OutputPath>/Users/edmand46/RagonProjects/ragon-oss-examples/Assets/Ragon/Runtime/Plugins</OutputPath> <OutputPath>/Users/edmand46/UnityProjects/itd-client/Assets/Ragon/Runtime/Plugins/</OutputPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
+2 -2
View File
@@ -39,8 +39,8 @@ namespace Ragon.Client
public void Dispose() public void Dispose()
{ {
_callbacks.Remove(_callback); _callbacks?.Remove(_callback);
_localCallbacks.Remove(_callback); _localCallbacks?.Remove(_callback);
_callbacks = null!; _callbacks = null!;
_localCallbacks = null!; _localCallbacks = null!;
+13 -12
View File
@@ -30,7 +30,7 @@ namespace Ragon.Client
public int Size => _size; public int Size => _size;
private RagonBuffer _propertyBuffer; private RagonBuffer _propertyBuffer;
private RagonEntity _entity; private RagonEntity _entity;
private bool _dirty; private bool _dirty;
private int _size; private int _size;
private int _ticks; private int _ticks;
@@ -46,7 +46,7 @@ namespace Ragon.Client
_priority = priority; _priority = priority;
_fixed = false; _fixed = false;
_propertyBuffer = new RagonBuffer(); _propertyBuffer = new RagonBuffer();
InvokeLocal = invokeLocal; InvokeLocal = invokeLocal;
} }
@@ -63,7 +63,7 @@ namespace Ragon.Client
protected void InvokeChanged() protected void InvokeChanged()
{ {
if (!InvokeLocal) if (_entity.HasAuthority)
return; return;
Changed?.Invoke(); Changed?.Invoke();
@@ -71,8 +71,9 @@ namespace Ragon.Client
protected void MarkAsChanged() protected void MarkAsChanged()
{ {
InvokeChanged(); if (InvokeLocal)
Changed?.Invoke();
if (_dirty || _entity == null) if (_dirty || _entity == null)
return; return;
@@ -101,34 +102,34 @@ namespace Ragon.Client
internal void Write(RagonBuffer buffer) internal void Write(RagonBuffer buffer)
{ {
_propertyBuffer.Clear(); _propertyBuffer.Clear();
if (_fixed) if (_fixed)
{ {
Serialize(_propertyBuffer); Serialize(_propertyBuffer);
buffer.CopyFrom(_propertyBuffer, _size); buffer.CopyFrom(_propertyBuffer, _size);
return; return;
} }
Serialize(_propertyBuffer); Serialize(_propertyBuffer);
var propertySize = (ushort) _propertyBuffer.WriteOffset; var propertySize = (ushort)_propertyBuffer.WriteOffset;
buffer.WriteUShort(propertySize);; buffer.WriteUShort(propertySize);
buffer.CopyFrom(_propertyBuffer, propertySize); buffer.CopyFrom(_propertyBuffer, propertySize);
} }
internal void Read(RagonBuffer buffer) internal void Read(RagonBuffer buffer)
{ {
_propertyBuffer.Clear(); _propertyBuffer.Clear();
if (_fixed) if (_fixed)
{ {
buffer.ToBuffer(_propertyBuffer, _size); buffer.ToBuffer(_propertyBuffer, _size);
Deserialize(_propertyBuffer); Deserialize(_propertyBuffer);
return; return;
} }
var propSize = buffer.ReadUShort(); var propSize = buffer.ReadUShort();
buffer.ToBuffer(_propertyBuffer, propSize); buffer.ToBuffer(_propertyBuffer, propSize);
Deserialize(_propertyBuffer); Deserialize(_propertyBuffer);
@@ -51,6 +51,8 @@ internal class EntityCreateHandler : IHandler
if (player == null) if (player == null)
{ {
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players"); RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
_playerCache.Dump();
return; return;
} }
@@ -20,35 +20,37 @@ namespace Ragon.Client;
internal class EntityEventHandler : IHandler internal class EntityEventHandler : IHandler
{ {
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
public EntityEventHandler( public EntityEventHandler(
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
RagonEntityCache entityCache RagonEntityCache entityCache
) )
{
_playerCache = playerCache;
_entityCache = entityCache;
}
public void Handle(RagonBuffer reader)
{
var eventCode = reader.ReadUShort();
var peerId = reader.ReadUShort();
var executionMode = (RagonReplicationMode)reader.ReadByte();
var entityId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{ {
_playerCache = playerCache; RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_entityCache = entityCache;
_playerCache.Dump();
return;
} }
public void Handle(RagonBuffer reader)
{
var eventCode = reader.ReadUShort();
var peerId = reader.ReadUShort();
var executionMode = (RagonReplicationMode)reader.ReadByte();
var entityId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(peerId); if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
if (player == null) return;
{
RagonLog.Warn($"Player not found for event {eventCode}");
return;
}
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer) _entityCache.OnEvent(player, entityId, eventCode, reader);
return; }
_entityCache.OnEvent(player, entityId, eventCode, reader);
}
} }
@@ -41,6 +41,14 @@ internal class EntityOwnershipHandler: IHandler
var entities = reader.ReadUShort(); var entities = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId); var player = _playerCache.GetPlayerByPeer(newOwnerId);
if (player == null)
{
RagonLog.Error($"Player with Id:{newOwnerId} not found in cache");
_playerCache.Dump();
return;
}
for (var i = 0; i < entities; i++) for (var i = 0; i < entities; i++)
{ {
var entityId = reader.ReadUShort(); var entityId = reader.ReadUShort();
@@ -19,12 +19,12 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
internal class OwnershipRoomHandler: IHandler internal class OwnershipRoomHandler : IHandler
{ {
private readonly RagonListenerList _listenerList; private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache; private readonly RagonEntityCache _entityCache;
public OwnershipRoomHandler( public OwnershipRoomHandler(
RagonListenerList listenerList, RagonListenerList listenerList,
RagonPlayerCache playerCache, RagonPlayerCache playerCache,
@@ -34,11 +34,18 @@ internal class OwnershipRoomHandler: IHandler
_playerCache = playerCache; _playerCache = playerCache;
_entityCache = entityCache; _entityCache = entityCache;
} }
public void Handle(RagonBuffer reader) public void Handle(RagonBuffer reader)
{ {
var newOwnerId = reader.ReadUShort(); var newOwnerId = reader.ReadUShort();
var player = _playerCache.GetPlayerByPeer(newOwnerId); var player = _playerCache.GetPlayerByPeer(newOwnerId);
if (player == null)
{
RagonLog.Warn($"Player with peerId:{newOwnerId} not found in cache");
_playerCache.Dump();
return;
}
_playerCache.OnOwnershipChanged(newOwnerId); _playerCache.OnOwnershipChanged(newOwnerId);
_listenerList.OnOwnershipChanged(player); _listenerList.OnOwnershipChanged(player);
@@ -45,6 +45,6 @@ internal class PlayerJoinHandler : IHandler
if (player != null) if (player != null)
_listenerList.OnPlayerJoined(player); _listenerList.OnPlayerJoined(player);
else else
RagonLog.Trace($"[Joined] {playerId}"); RagonLog.Warn($"Player with Id:{playerId} not found in cache");
} }
} }
@@ -57,5 +57,9 @@ internal class PlayerLeftHandler : IHandler
foreach (var id in toDeleteIds) foreach (var id in toDeleteIds)
_entityCache.OnDestroy(id, emptyPayload); _entityCache.OnDestroy(id, emptyPayload);
} }
else
{
RagonLog.Warn($"Player with Id:{playerId} not found in cache");
}
} }
} }
@@ -37,6 +37,15 @@ internal class RoomDataHandler: IHandler
var rawData = reader.RawData; var rawData = reader.RawData;
var peerId = (ushort)(rawData[1] + (rawData[2] << 8)); var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
var player = _playerCache.GetPlayerByPeer(peerId); var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found");
_playerCache.Dump();
return;
}
var headerSize = 3; var headerSize = 3;
var payload = new byte[rawData.Length - headerSize]; var payload = new byte[rawData.Length - headerSize];
@@ -18,11 +18,11 @@ using Ragon.Protocol;
namespace Ragon.Client; namespace Ragon.Client;
public class RoomEventHandler: IHandler public class RoomEventHandler : IHandler
{ {
private readonly RagonClient _client; private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache; private readonly RagonPlayerCache _playerCache;
public RoomEventHandler( public RoomEventHandler(
RagonClient client, RagonClient client,
RagonPlayerCache playerCache RagonPlayerCache playerCache
@@ -31,23 +31,25 @@ public class RoomEventHandler: IHandler
_client = client; _client = client;
_playerCache = playerCache; _playerCache = playerCache;
} }
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer buffer)
{ {
var eventCode = buffer.ReadUShort(); var eventCode = buffer.ReadUShort();
var peerId = buffer.ReadUShort(); var peerId = buffer.ReadUShort();
var executionMode = (RagonReplicationMode)buffer.ReadByte(); var executionMode = (RagonReplicationMode)buffer.ReadByte();
var player = _playerCache.GetPlayerByPeer(peerId); var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null) if (player == null)
{ {
RagonLog.Warn($"Player not found for event {eventCode}"); RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
_playerCache.Dump();
return; return;
} }
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer) if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
return; return;
_client.Room.Event(eventCode, player, buffer); _client.Room.Event(eventCode, player, buffer);
} }
} }
@@ -45,6 +45,7 @@ internal class SnapshotHandler : IHandler
public void Handle(RagonBuffer buffer) public void Handle(RagonBuffer buffer)
{ {
var entities = new List<RagonEntity>();
var playersCount = buffer.ReadUShort(); var playersCount = buffer.ReadUShort();
RagonLog.Trace("Players: " + playersCount); RagonLog.Trace("Players: " + playersCount);
for (var i = 0; i < playersCount; i++) for (var i = 0; i < playersCount; i++)
@@ -70,7 +71,9 @@ internal class SnapshotHandler : IHandler
var player = _playerCache.GetPlayerByPeer(ownerPeerId); var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null) if (player == null)
{ {
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}"); RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return; return;
} }
@@ -88,7 +91,8 @@ internal class SnapshotHandler : IHandler
_entityListener.OnEntityCreated(entity); _entityListener.OnEntityCreated(entity);
entity.Read(buffer); entity.Read(buffer);
entity.Attach();
entities.Add(entity);
} }
var staticEntities = buffer.ReadUShort(); var staticEntities = buffer.ReadUShort();
@@ -104,7 +108,9 @@ internal class SnapshotHandler : IHandler
var player = _playerCache.GetPlayerByPeer(ownerPeerId); var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null) if (player == null)
{ {
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}"); RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
_playerCache.Dump();
return; return;
} }
@@ -112,9 +118,9 @@ internal class SnapshotHandler : IHandler
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _); var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty); entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
entity.Read(buffer); entity.Read(buffer);
entity.Attach();
entities.Add(entity);
} }
if (_client.Status == RagonStatus.LOBBY) if (_client.Status == RagonStatus.LOBBY)
@@ -123,6 +129,9 @@ internal class SnapshotHandler : IHandler
_listenerList.OnJoined(); _listenerList.OnJoined();
} }
foreach (var entity in entities)
entity.Attach();
_listenerList.OnSceneLoaded(); _listenerList.OnSceneLoaded();
} }
} }
+10 -14
View File
@@ -88,9 +88,9 @@ public sealed class RagonEntityCache
RagonLog.Warn("Can't destroy object"); RagonLog.Warn("Can't destroy object");
return; return;
} }
entity.SetReplication(false); entity.SetReplication(false);
var buffer = _client.Buffer; var buffer = _client.Buffer;
buffer.Clear(); buffer.Clear();
@@ -168,7 +168,7 @@ public sealed class RagonEntityCache
_entityMap.Clear(); _entityMap.Clear();
_entityList.Clear(); _entityList.Clear();
} }
internal RagonEntity TryGetEntity(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority, out bool hasCreated) internal RagonEntity TryGetEntity(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority, out bool hasCreated)
{ {
if (sceneId > 0) if (sceneId > 0)
@@ -179,36 +179,33 @@ public sealed class RagonEntityCache
if (hasAuthority) if (hasAuthority)
_entityList.Add(sceneEntity); _entityList.Add(sceneEntity);
hasCreated = false; hasCreated = false;
return sceneEntity; return sceneEntity;
} }
} }
if (_pendingEntities.TryGetValue(attachId, out var pendingEntity)) if (_pendingEntities.TryGetValue(attachId, out var pendingEntity) && hasAuthority)
{ {
_pendingEntities.Remove(attachId); _pendingEntities.Remove(attachId);
_entityMap.Add(entityId, pendingEntity); _entityMap.Add(entityId, pendingEntity);
_entityList.Add(pendingEntity);
if (hasAuthority)
_entityList.Add(pendingEntity);
hasCreated = false; hasCreated = false;
return pendingEntity; return pendingEntity;
} }
var entity = new RagonEntity(entityType, sceneId); var entity = new RagonEntity(entityType, sceneId);
_entityMap.Add(entityId, entity); _entityMap.Add(entityId, entity);
if (hasAuthority) if (hasAuthority)
_entityList.Add(entity); _entityList.Add(entity);
hasCreated = true; hasCreated = true;
return entity; return entity;
} }
@@ -217,7 +214,6 @@ public sealed class RagonEntityCache
{ {
if (_entityMap.TryGetValue(entityId, out var entity)) if (_entityMap.TryGetValue(entityId, out var entity))
{ {
_entityMap.Remove(entityId); _entityMap.Remove(entityId);
_entityList.Remove(entity); _entityList.Remove(entity);
+26 -3
View File
@@ -26,9 +26,22 @@ public sealed class RagonPlayerCache
public RagonPlayer Owner { get; private set; } public RagonPlayer Owner { get; private set; }
public RagonPlayer Local { get; private set; } public RagonPlayer Local { get; private set; }
public bool IsRoomOwner => _ownerId == _localId; public bool IsRoomOwner => _ownerId == _localId;
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId]; public RagonPlayer? GetPlayerById(string playerId)
public RagonPlayer? GetPlayerByPeer(ushort peerId) => _playersByConnection[peerId]; {
if (_playersById.TryGetValue(playerId, out var player))
return player;
return null;
}
public RagonPlayer? GetPlayerByPeer(ushort peerId)
{
if (_playersByConnection.TryGetValue(peerId, out var player))
return player;
return null;
}
private string _ownerId; private string _ownerId;
private string _localId; private string _localId;
@@ -91,4 +104,14 @@ public sealed class RagonPlayerCache
_playersByConnection.Clear(); _playersByConnection.Clear();
_playersById.Clear(); _playersById.Clear();
} }
public void Dump()
{
RagonLog.Trace("Players: ");
RagonLog.Trace("[Connection] [ID] [Name]");
foreach (var player in _players)
{
RagonLog.Trace($"[{player.PeerId}] {player.Id} {player.Name}");
}
}
} }
+2 -2
View File
@@ -51,9 +51,9 @@ namespace Ragon.Client
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers}); Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, string sceneNa, int minPlayers, int maxPlayers) public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
{ {
Create(roomId, new RagonRoomParameters() {Scene = sceneNa, Min = minPlayers, Max = maxPlayers}); Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
} }
public void Create(string roomId, RagonRoomParameters parameters) public void Create(string roomId, RagonRoomParameters parameters)
+1 -1
View File
@@ -75,7 +75,7 @@ namespace Ragon.Protocol
public int ReadOffset => _read; public int ReadOffset => _read;
public int WriteOffset => _write; public int WriteOffset => _write;
public int Length => ((_write - 1) >> 3) + 1; public int Length => ((_write - 1) >> 3) + 1;
public int Capacity => _write - _read; public int Capacity => _write - _read - 1;
public RagonBuffer(int capacity = 128) public RagonBuffer(int capacity = 128)
{ {
+6
View File
@@ -1,5 +1,6 @@
using System; using System;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ragon.Server;
using Ragon.Server.Plugin; using Ragon.Server.Plugin;
namespace Ragon.Relay; namespace Ragon.Relay;
@@ -21,4 +22,9 @@ public class RelayServerPlugin: BaseServerPlugin
return true; return true;
} }
public override IRoomPlugin CreateRoomPlugin(RoomInformation information)
{
return new RelayRoomPlugin();
}
} }
+1 -1
View File
@@ -28,7 +28,7 @@ public class RagonPayload
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
_size = buffer.Capacity - 1; _size = buffer.Capacity;
buffer.ReadArray(_data, _size); buffer.ReadArray(_data, _size);
} }
+1 -1
View File
@@ -39,7 +39,7 @@ public class RagonEvent
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
{ {
_size = buffer.Capacity - 1; _size = buffer.Capacity;
buffer.ReadArray(_data, _size); buffer.ReadArray(_data, _size);
} }
@@ -25,12 +25,20 @@ public class LobbyInMemory : IRagonLobby
private readonly List<RagonRoom> _rooms = new(); private readonly List<RagonRoom> _rooms = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger(); private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public bool FindRoomById(string RagonRoomId, [MaybeNullWhen(false)] out RagonRoom room) public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room)
{ {
foreach (var existRagonRoom in _rooms) foreach (var existRagonRoom in _rooms)
{ {
if (existRagonRoom.Id == RagonRoomId && existRagonRoom.PlayerMin < existRagonRoom.PlayerMax) if (existRagonRoom.Id == roomId)
{ {
if (existRagonRoom.PlayerCount >= existRagonRoom.PlayerMax)
{
_logger.Warn($"Room with id {roomId} fulfilled");
room = default;
return false;
}
room = existRagonRoom; room = existRagonRoom;
return true; return true;
} }
@@ -44,8 +52,16 @@ public class LobbyInMemory : IRagonLobby
{ {
foreach (var existsRoom in _rooms) foreach (var existsRoom in _rooms)
{ {
if (existsRoom.Scene == sceneName && existsRoom.PlayerCount < existsRoom.PlayerMax) if (existsRoom.Scene == sceneName)
{ {
if (existsRoom.PlayerCount >= existsRoom.PlayerMax)
{
_logger.Warn($"Room with scene {sceneName} fulfilled");
room = default;
return false;
}
room = existsRoom; room = existsRoom;
return true; return true;
} }
@@ -0,0 +1,9 @@
namespace Ragon.Server.Logging;
public interface IRagonLogger
{
public void Warning(string tag, string message);
public void Info(string tag, string message);
public void Error(string tag, string message);
public void Trace(string tag, string message);
}
@@ -49,7 +49,7 @@ public class BaseServerPlugin: IServerPlugin
return true; return true;
} }
public IRoomPlugin CreateRoomPlugin(RoomInformation information) public virtual IRoomPlugin CreateRoomPlugin(RoomInformation information)
{ {
return new BaseRoomPlugin(); return new BaseRoomPlugin();
} }