From 672bb1ff6de6fb8dd56ebcb0f43b047c3cf6b916 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sat, 28 Sep 2024 20:11:56 +0300 Subject: [PATCH] wip --- Ragon.Client.Property/Sources/RagonBool.cs | 1 + Ragon.Client.Property/Sources/RagonFloat.cs | 1 + Ragon.Client.Property/Sources/RagonInt.cs | 1 + .../Sources/RagonQuaternion.cs | 1 + Ragon.Client.Property/Sources/RagonString.cs | 1 + Ragon.Client.Property/Sources/RagonVector3.cs | 1 + .../Sources/Handler/AuthorizeFailedHandler.cs | 2 +- .../Handler/AuthorizeSuccessHandler.cs | 2 +- Ragon.Client/Sources/Handler/IHandler.cs | 2 +- .../Sources/Handler/JoinFailedHandler.cs | 2 +- .../Sources/Handler/JoinSuccessHandler.cs | 11 +- .../Sources/Handler/LeaveRoomHandler.cs | 8 +- .../Sources/Handler/OwnershipRoomHandler.cs | 7 +- .../Sources/Handler/PlayerJoinHandler.cs | 2 +- .../Sources/Handler/PlayerLeftHandler.cs | 11 +- .../Sources/Handler/PlayerUserDataHandler.cs | 2 +- .../Sources/Handler/RoomDataHandler.cs | 4 +- .../Sources/Handler/RoomEventHandler.cs | 2 +- .../Sources/Handler/RoomListHandler.cs | 2 +- .../Sources/Handler/RoomUserDataHandler.cs | 2 +- .../Sources/Handler/TimestampHandler.cs | 6 +- Ragon.Client/Sources/IUserData.cs | 4 +- Ragon.Client/Sources/RagonClient.cs | 68 ++------- Ragon.Client/Sources/RagonListenerList.cs | 36 ----- Ragon.Client/Sources/RagonRoom.cs | 103 +++++++++---- Ragon.Client/Sources/RagonSession.cs | 4 +- Ragon.Client/Sources/RagonUserData.cs | 8 +- Ragon.Client/Sources/RagonUserDataReadOnly.cs | 6 +- .../Compressor/Extensions.cs} | 0 .../Compressor/FloatCompressor.cs | 0 .../Compressor/IntCompressor.cs | 0 .../{ => Replication}/Entity/RagonEntity.cs | 8 +- .../Entity/RagonEntityState.cs | 2 +- .../{ => Replication}/Entity/RagonPayload.cs | 12 +- .../{ => Replication}/Entity/RagonProperty.cs | 2 +- .../Handler/EntityCreateHandler.cs | 12 +- .../Handler/EntityEventHandler.cs | 4 +- .../Handler/EntityOwnershipHandler.cs | 4 +- .../Handler/EntityRemoveHandler.cs | 10 +- .../Handler/EntityStateHandler.cs | 4 +- .../Handler/LoadSceneHandler.cs | 6 +- .../Handler/SnapshotHandler.cs | 12 +- .../{ => Replication}/IRagonEntityListener.cs | 2 +- .../{ => Replication}/IRagonPayload.cs | 5 +- .../{ => Replication}/IRagonSceneCollector.cs | 2 +- .../{ => Replication}/RagonEntityCache.cs | 4 +- .../Sources/{ => Replication}/RagonScene.cs | 40 +++--- Ragon.Protocol/Sources/RagonRoomParameters.cs | 14 +- Ragon.Protocol/Sources/RagonSerializable.cs | 4 +- .../Replication}/Entity/IRagonEntity.cs | 0 .../Replication}/Entity/IRagonEntityState.cs | 0 .../Replication}/Entity/RagonEntity.cs | 8 +- .../Replication/Entity}/RagonEntityCache.cs | 1 + .../Entity/RagonEntityParameters.cs | 0 .../Replication}/Entity/RagonEntityState.cs | 5 +- .../Replication}/Entity/RagonPayload.cs | 12 +- .../Replication}/Entity/RagonProperty.cs | 2 + .../Handler/EntityCreateOperation.cs | 39 ++--- .../Handler/EntityEventOperation.cs | 26 ++-- .../Handler/EntityOwnershipOperation.cs | 54 +++---- .../Handler/EntityRemoveOperation.cs | 34 ++--- .../Handler/EntityStateOperation.cs | 20 +-- .../Handler/SceneLoadOperation.cs | 2 +- .../Handler/SceneLoadedOperation.cs | 81 ++++++----- .../Sources/Replication/ReplicationRoom.cs | 136 ++++++++++++++++++ Ragon.Server/Sources/Data/RagonData.cs | 12 +- Ragon.Server/Sources/Event/RagonEvent.cs | 12 +- .../Sources/Handler/AuthorizationOperation.cs | 6 +- Ragon.Server/Sources/Handler/BaseOperation.cs | 6 +- .../Handler/PlayerUserDataOperation.cs | 4 +- .../Sources/Handler/RoomCreateOperation.cs | 6 +- .../Sources/Handler/RoomDataOperation.cs | 6 +- .../Sources/Handler/RoomEventOperation.cs | 2 +- .../Sources/Handler/RoomJoinOperation.cs | 6 +- .../Handler/RoomJoinOrCreateOperation.cs | 6 +- .../Sources/Handler/RoomLeaveOperation.cs | 2 +- .../Sources/Handler/RoomOwnershipOperation.cs | 2 +- .../Sources/Handler/RoomUserDataOperation.cs | 4 +- .../Sources/Handler/TimestampSyncOperation.cs | 10 +- .../Sources/Lobby/RagonLobbyDispatcher.cs | 2 +- .../Sources/Lobby/RagonLobbyInMemory.cs | 4 +- Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs | 14 -- Ragon.Server/Sources/Plugin/IRoomPlugin.cs | 3 - Ragon.Server/Sources/RagonServer.cs | 28 ++-- Ragon.Server/Sources/Room/IRagonRoom.cs | 3 - Ragon.Server/Sources/Room/RagonRoom.cs | 120 +--------------- Ragon.Server/Sources/Room/RagonRoomPlayer.cs | 14 -- Ragon.Tests/Connection.cs | 45 ++++++ Ragon.Tests/Ragon.Tests.csproj | 31 ++++ Ragon.Tests/Utilities/FakeClientNetwork.cs | 45 ++++++ .../Utilities/FakeClientNetworkChannel.cs | 11 ++ Ragon.Tests/Utilities/FakeNetwork.cs | 15 ++ Ragon.Tests/Utilities/FakeServerNetwork.cs | 28 ++++ .../Utilities/FakeServerNetworkChannel.cs | 18 +++ Ragon.sln | 10 +- 95 files changed, 741 insertions(+), 601 deletions(-) rename Ragon.Client/Sources/{Utils/CompressorExtension.cs => Replication/Compressor/Extensions.cs} (100%) rename Ragon.Client/Sources/{ => Replication}/Compressor/FloatCompressor.cs (100%) rename Ragon.Client/Sources/{ => Replication}/Compressor/IntCompressor.cs (100%) rename Ragon.Client/Sources/{ => Replication}/Entity/RagonEntity.cs (98%) rename Ragon.Client/Sources/{ => Replication}/Entity/RagonEntityState.cs (98%) rename Ragon.Client/Sources/{ => Replication}/Entity/RagonPayload.cs (81%) rename Ragon.Client/Sources/{ => Replication}/Entity/RagonProperty.cs (98%) rename Ragon.Client/Sources/{ => Replication}/Handler/EntityCreateHandler.cs (86%) rename Ragon.Client/Sources/{ => Replication}/Handler/EntityEventHandler.cs (95%) rename Ragon.Client/Sources/{ => Replication}/Handler/EntityOwnershipHandler.cs (95%) rename Ragon.Client/Sources/{ => Replication}/Handler/EntityRemoveHandler.cs (81%) rename Ragon.Client/Sources/{ => Replication}/Handler/EntityStateHandler.cs (93%) rename Ragon.Client/Sources/{ => Replication}/Handler/LoadSceneHandler.cs (90%) rename Ragon.Client/Sources/{ => Replication}/Handler/SnapshotHandler.cs (94%) rename Ragon.Client/Sources/{ => Replication}/IRagonEntityListener.cs (95%) rename Ragon.Client/Sources/{ => Replication}/IRagonPayload.cs (84%) rename Ragon.Client/Sources/{ => Replication}/IRagonSceneCollector.cs (95%) rename Ragon.Client/Sources/{ => Replication}/RagonEntityCache.cs (98%) rename Ragon.Client/Sources/{ => Replication}/RagonScene.cs (85%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/IRagonEntity.cs (100%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/IRagonEntityState.cs (100%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/RagonEntity.cs (97%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication/Entity}/RagonEntityCache.cs (98%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/RagonEntityParameters.cs (100%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/RagonEntityState.cs (93%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/RagonPayload.cs (79%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Entity/RagonProperty.cs (98%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/EntityCreateOperation.cs (76%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/EntityEventOperation.cs (74%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/EntityOwnershipOperation.cs (56%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/EntityRemoveOperation.cs (62%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/EntityStateOperation.cs (75%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/SceneLoadOperation.cs (96%) rename {Ragon.Server/Sources => Ragon.Relay/Sources/Replication}/Handler/SceneLoadedOperation.cs (68%) create mode 100644 Ragon.Relay/Sources/Replication/ReplicationRoom.cs create mode 100644 Ragon.Tests/Connection.cs create mode 100644 Ragon.Tests/Ragon.Tests.csproj create mode 100644 Ragon.Tests/Utilities/FakeClientNetwork.cs create mode 100644 Ragon.Tests/Utilities/FakeClientNetworkChannel.cs create mode 100644 Ragon.Tests/Utilities/FakeNetwork.cs create mode 100644 Ragon.Tests/Utilities/FakeServerNetwork.cs create mode 100644 Ragon.Tests/Utilities/FakeServerNetworkChannel.cs diff --git a/Ragon.Client.Property/Sources/RagonBool.cs b/Ragon.Client.Property/Sources/RagonBool.cs index d401f23..73f36e1 100644 --- a/Ragon.Client.Property/Sources/RagonBool.cs +++ b/Ragon.Client.Property/Sources/RagonBool.cs @@ -15,6 +15,7 @@ */ +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client.Property diff --git a/Ragon.Client.Property/Sources/RagonFloat.cs b/Ragon.Client.Property/Sources/RagonFloat.cs index c571d5d..c5e0c9c 100644 --- a/Ragon.Client.Property/Sources/RagonFloat.cs +++ b/Ragon.Client.Property/Sources/RagonFloat.cs @@ -15,6 +15,7 @@ */ +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client diff --git a/Ragon.Client.Property/Sources/RagonInt.cs b/Ragon.Client.Property/Sources/RagonInt.cs index 175557c..23a6acf 100644 --- a/Ragon.Client.Property/Sources/RagonInt.cs +++ b/Ragon.Client.Property/Sources/RagonInt.cs @@ -15,6 +15,7 @@ */ +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client.Property diff --git a/Ragon.Client.Property/Sources/RagonQuaternion.cs b/Ragon.Client.Property/Sources/RagonQuaternion.cs index e914a64..10cb6f9 100644 --- a/Ragon.Client.Property/Sources/RagonQuaternion.cs +++ b/Ragon.Client.Property/Sources/RagonQuaternion.cs @@ -16,6 +16,7 @@ using System.Numerics; using Ragon.Client.Compressor; +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client.Property; diff --git a/Ragon.Client.Property/Sources/RagonString.cs b/Ragon.Client.Property/Sources/RagonString.cs index ba1bc51..301c497 100644 --- a/Ragon.Client.Property/Sources/RagonString.cs +++ b/Ragon.Client.Property/Sources/RagonString.cs @@ -16,6 +16,7 @@ using System.Text; +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client.Property diff --git a/Ragon.Client.Property/Sources/RagonVector3.cs b/Ragon.Client.Property/Sources/RagonVector3.cs index 0c79911..7bd1895 100644 --- a/Ragon.Client.Property/Sources/RagonVector3.cs +++ b/Ragon.Client.Property/Sources/RagonVector3.cs @@ -17,6 +17,7 @@ using System; using System.Numerics; using Ragon.Client.Compressor; +using Ragon.Client.Replication; using Ragon.Protocol; namespace Ragon.Client.Property diff --git a/Ragon.Client/Sources/Handler/AuthorizeFailedHandler.cs b/Ragon.Client/Sources/Handler/AuthorizeFailedHandler.cs index 7738217..989d6a1 100644 --- a/Ragon.Client/Sources/Handler/AuthorizeFailedHandler.cs +++ b/Ragon.Client/Sources/Handler/AuthorizeFailedHandler.cs @@ -27,7 +27,7 @@ internal class AuthorizeFailedHandler: IHandler _listenerList = list; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var message = reader.ReadString(); _listenerList.OnAuthorizationFailed(message); diff --git a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs index 59db3a2..0e1addc 100644 --- a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs +++ b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs @@ -32,7 +32,7 @@ internal class AuthorizeSuccessHandler: IHandler _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var playerId = reader.ReadString(); var playerName = reader.ReadString(); diff --git a/Ragon.Client/Sources/Handler/IHandler.cs b/Ragon.Client/Sources/Handler/IHandler.cs index 5b1431c..0528847 100644 --- a/Ragon.Client/Sources/Handler/IHandler.cs +++ b/Ragon.Client/Sources/Handler/IHandler.cs @@ -21,5 +21,5 @@ namespace Ragon.Client; public interface IHandler { - public void Handle(RagonBuffer reader); + public void Handle(RagonStream reader); } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/JoinFailedHandler.cs b/Ragon.Client/Sources/Handler/JoinFailedHandler.cs index 5d9c946..8818222 100644 --- a/Ragon.Client/Sources/Handler/JoinFailedHandler.cs +++ b/Ragon.Client/Sources/Handler/JoinFailedHandler.cs @@ -28,7 +28,7 @@ internal class JoinFailedHandler: IHandler _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var message = reader.ReadString(); _listenerList.OnFailed(message); diff --git a/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs b/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs index c362e63..e9bd96f 100644 --- a/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs +++ b/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs @@ -41,23 +41,20 @@ internal class JoinSuccessHandler : IHandler { private readonly RagonListenerList _listenerList; private readonly RagonPlayerCache _playerCache; - private readonly RagonEntityCache _entityCache; private readonly RagonClient _client; public JoinSuccessHandler( RagonClient client, RagonListenerList listenerList, - RagonPlayerCache playerCache, - RagonEntityCache entityCache + RagonPlayerCache playerCache ) { _client = client; _listenerList = listenerList; - _entityCache = entityCache; _playerCache = playerCache; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var roomId = reader.ReadString(); var min = reader.ReadUShort(); @@ -68,9 +65,8 @@ internal class JoinSuccessHandler : IHandler _playerCache.SetOwnerAndLocal(ownerId, localId); - var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName); var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max); - var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene); + var room = new RagonRoom(_client, _playerCache, roomInfo); room.UserData.Read(reader); @@ -93,6 +89,5 @@ internal class JoinSuccessHandler : IHandler _client.SetStatus(RagonStatus.ROOM); _listenerList.OnJoined(); - _listenerList.OnSceneRequest(sceneName); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/LeaveRoomHandler.cs b/Ragon.Client/Sources/Handler/LeaveRoomHandler.cs index 2aa0c4e..eec119c 100644 --- a/Ragon.Client/Sources/Handler/LeaveRoomHandler.cs +++ b/Ragon.Client/Sources/Handler/LeaveRoomHandler.cs @@ -23,22 +23,18 @@ internal class LeaveRoomHandler : IHandler { private readonly RagonClient _client; private readonly RagonListenerList _listenerList; - private readonly RagonEntityCache _entityCache; public LeaveRoomHandler( RagonClient client, - RagonListenerList listenerList, - RagonEntityCache entityCache) + RagonListenerList listenerList) { _client = client; _listenerList = listenerList; - _entityCache = entityCache; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { _listenerList.OnLeft(); - _entityCache.Cleanup(); _client.Room.Cleanup(); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs b/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs index e22aa63..08e7126 100644 --- a/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs +++ b/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs @@ -23,19 +23,16 @@ internal class OwnershipRoomHandler : IHandler { private readonly RagonListenerList _listenerList; private readonly RagonPlayerCache _playerCache; - private readonly RagonEntityCache _entityCache; public OwnershipRoomHandler( RagonListenerList listenerList, - RagonPlayerCache playerCache, - RagonEntityCache entityCache) + RagonPlayerCache playerCache) { _listenerList = listenerList; _playerCache = playerCache; - _entityCache = entityCache; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var newOwnerId = reader.ReadUShort(); var player = _playerCache.GetPlayerByPeer(newOwnerId); diff --git a/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs b/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs index c3e634b..ca681b6 100644 --- a/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs +++ b/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs @@ -33,7 +33,7 @@ internal class PlayerJoinHandler : IHandler _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var playerPeerId = reader.ReadUShort(); var playerId = reader.ReadString(); diff --git a/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs b/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs index fb44206..49e588a 100644 --- a/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs +++ b/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs @@ -22,21 +22,18 @@ namespace Ragon.Client; internal class PlayerLeftHandler : IHandler { private RagonPlayerCache _playerCache; - private RagonEntityCache _entityCache; private RagonListenerList _listenerList; public PlayerLeftHandler( - RagonEntityCache entityCache, RagonPlayerCache playerCache, RagonListenerList listenerList ) { - _entityCache = entityCache; _playerCache = playerCache; _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var playerId = reader.ReadString(); var player = _playerCache.GetPlayerById(playerId); @@ -53,9 +50,9 @@ internal class PlayerLeftHandler : IHandler toDeleteIds[i] = entityId; } - var emptyPayload = new RagonPayload(0); - foreach (var id in toDeleteIds) - _entityCache.OnDestroy(id, emptyPayload); + // var emptyPayload = new RagonPayload(0); + // foreach (var id in toDeleteIds) + // _entityCache.OnDestroy(id, emptyPayload); } else { diff --git a/Ragon.Client/Sources/Handler/PlayerUserDataHandler.cs b/Ragon.Client/Sources/Handler/PlayerUserDataHandler.cs index dbe44bc..f452e63 100644 --- a/Ragon.Client/Sources/Handler/PlayerUserDataHandler.cs +++ b/Ragon.Client/Sources/Handler/PlayerUserDataHandler.cs @@ -32,7 +32,7 @@ namespace Ragon.Client _playerCache = playerCache; _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var playerPeerId = reader.ReadUShort(); var player = _playerCache.GetPlayerByPeer(playerPeerId); diff --git a/Ragon.Client/Sources/Handler/RoomDataHandler.cs b/Ragon.Client/Sources/Handler/RoomDataHandler.cs index bd35aa4..a025197 100644 --- a/Ragon.Client/Sources/Handler/RoomDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomDataHandler.cs @@ -32,9 +32,9 @@ internal class RoomDataHandler: IHandler _listeners = listeners; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { - var rawData = reader.RawData; + var rawData = reader.ReadBinary(reader.Lenght); var peerId = (ushort)(rawData[1] + (rawData[2] << 8)); RagonPlayer player = null; diff --git a/Ragon.Client/Sources/Handler/RoomEventHandler.cs b/Ragon.Client/Sources/Handler/RoomEventHandler.cs index 74751a8..b51591f 100644 --- a/Ragon.Client/Sources/Handler/RoomEventHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomEventHandler.cs @@ -32,7 +32,7 @@ public class RoomEventHandler : IHandler _playerCache = playerCache; } - public void Handle(RagonBuffer buffer) + public void Handle(RagonStream buffer) { var eventCode = buffer.ReadUShort(); var peerId = buffer.ReadUShort(); diff --git a/Ragon.Client/Sources/Handler/RoomListHandler.cs b/Ragon.Client/Sources/Handler/RoomListHandler.cs index 3150b1e..d1995be 100644 --- a/Ragon.Client/Sources/Handler/RoomListHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomListHandler.cs @@ -13,7 +13,7 @@ internal class RoomListHandler: IHandler _listenerList = list; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var roomCount = reader.ReadUShort(); var roomList = new RagonRoomInformation[roomCount]; diff --git a/Ragon.Client/Sources/Handler/RoomUserDataHandler.cs b/Ragon.Client/Sources/Handler/RoomUserDataHandler.cs index 18f9501..46795bf 100644 --- a/Ragon.Client/Sources/Handler/RoomUserDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomUserDataHandler.cs @@ -29,7 +29,7 @@ namespace Ragon.Client _listenerList = listenerList; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var changes = _client.Room?.UserData.Read(reader); _listenerList.OnRoomUserData(changes); diff --git a/Ragon.Client/Sources/Handler/TimestampHandler.cs b/Ragon.Client/Sources/Handler/TimestampHandler.cs index 53d40f4..11ee79f 100644 --- a/Ragon.Client/Sources/Handler/TimestampHandler.cs +++ b/Ragon.Client/Sources/Handler/TimestampHandler.cs @@ -10,10 +10,10 @@ public class TimestampHandler: IHandler _client = client; } - public void Handle(RagonBuffer buffer) + public void Handle(RagonStream buffer) { - var timestamp0 = buffer.Read(32); - var timestamp1 = buffer.Read(32); + var timestamp0 = (uint)buffer.ReadInt(); + var timestamp1 = (uint)buffer.ReadInt(); var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 }; _client.SetTimestamp(value.Double); diff --git a/Ragon.Client/Sources/IUserData.cs b/Ragon.Client/Sources/IUserData.cs index 07a6070..1d2f8e2 100644 --- a/Ragon.Client/Sources/IUserData.cs +++ b/Ragon.Client/Sources/IUserData.cs @@ -22,6 +22,6 @@ public interface IUserData { public byte[] this[string key] { get; set; } bool Dirty { get; } - IReadOnlyList Read(RagonBuffer buffer); - void Write(RagonBuffer buffer); + IReadOnlyList Read(RagonStream buffer); + void Write(RagonStream buffer); } \ No newline at end of file diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index a48962d..7c25f09 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -22,16 +22,14 @@ namespace Ragon.Client { private readonly INetworkConnection _connection; private readonly NetworkStatistics _stats; - private IRagonEntityListener _entityListener; - private IRagonSceneCollector _sceneCollector; + private IHandler[] _handlers; - private RagonBuffer _readBuffer; - private RagonBuffer _writeBuffer; + private RagonStream _readBuffer; + private RagonStream _writeBuffer; private RagonRoom _room; private RagonSession _session; private RagonListenerList _listeners; private RagonPlayerCache _playerCache; - private RagonEntityCache _entityCache; private RagonEventCache _eventCache; private RagonStatus _status; @@ -44,11 +42,10 @@ namespace Ragon.Client public RagonStatus Status => _status; public RagonSession Session => _session; public RagonEventCache Event => _eventCache; - public RagonEntityCache Entity => _entityCache; public NetworkStatistics Statistics => _stats; public RagonRoom Room => _room; - internal RagonBuffer Buffer => _writeBuffer; + internal RagonStream Buffer => _writeBuffer; internal INetworkChannel Reliable => _connection.Reliable; internal INetworkChannel Unreliable => _connection.Unreliable; @@ -72,58 +69,23 @@ namespace Ragon.Client } - public void Configure(IRagonSceneCollector sceneCollector) - { - _sceneCollector = sceneCollector; - } - - public void Configure(IRagonEntityListener listener) - { - _entityListener = listener; - } - public void Connect(string address, ushort port, string protocol) { - if (_sceneCollector == null) - { - RagonLog.Error("Scene collector is not defined!"); - return; - } - - if (_entityListener == null) - { - RagonLog.Error("Entity Listener is not defined!"); - return; - } - - _writeBuffer = new RagonBuffer(); - _readBuffer = new RagonBuffer(); + _writeBuffer = new RagonStream(); + _readBuffer = new RagonStream(); _playerCache = new RagonPlayerCache(); _session = new RagonSession(this, _writeBuffer); - _entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector); _handlers = new IHandler[byte.MaxValue]; _handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners); _handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners); - _handlers[(byte)RagonOperation.JOIN_SUCCESS] = - new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache); + _handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _listeners, _playerCache); _handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners); - _handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache); - _handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = - new OwnershipRoomHandler(_listeners, _playerCache, _entityCache); - _handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = - new EntityOwnershipHandler(_listeners, _playerCache, _entityCache); + _handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners); + _handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache); _handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners); - _handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners); - _handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners); - _handlers[(byte)RagonOperation.CREATE_ENTITY] = - new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener); - _handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache); - _handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache); - _handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache); + _handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_playerCache, _listeners); _handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache); - _handlers[(byte)RagonOperation.SNAPSHOT] = - new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener); _handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this); _handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners); _handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners); @@ -151,7 +113,7 @@ namespace Ragon.Client if (_replicationTime >= _replicationRate) { _replicationTime = 0; - _entityCache.WriteState(_writeBuffer); + // _entityCache.WriteState(_writeBuffer); SendTimestamp(); SendRoomUserData(); @@ -185,8 +147,6 @@ namespace Ragon.Client public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener); public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener); public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener); - public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener); - public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener); public void AddListener(IRagonDataListener listener) => _listeners.Add(listener); public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener); public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener); @@ -200,8 +160,6 @@ namespace Ragon.Client public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener); public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener); public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener); - public void RemoveListener(IRagonSceneListener listener) => _listeners.Remove(listener); - public void RemoveListener(IRagonSceneRequestListener listener) => _listeners.Remove(listener); public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener); public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener); public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener); @@ -241,8 +199,8 @@ namespace Ragon.Client _writeBuffer.Clear(); _writeBuffer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION); - _writeBuffer.Write(value.Int0, 32); - _writeBuffer.Write(value.Int1, 32); + _writeBuffer.WriteInt((int)value.Int0); + _writeBuffer.WriteInt((int)value.Int1); } private void SendRoomUserData() diff --git a/Ragon.Client/Sources/RagonListenerList.cs b/Ragon.Client/Sources/RagonListenerList.cs index 1cdf217..566d3b0 100644 --- a/Ragon.Client/Sources/RagonListenerList.cs +++ b/Ragon.Client/Sources/RagonListenerList.cs @@ -26,8 +26,6 @@ namespace Ragon.Client private readonly List _failedListeners = new(); private readonly List _joinListeners = new(); private readonly List _leftListeners = new(); - private readonly List _sceneListeners = new(); - private readonly List _sceneRequestListeners = new(); private readonly List _ownershipChangedListeners = new(); private readonly List _playerJoinListeners = new(); private readonly List _playerLeftListeners = new(); @@ -49,7 +47,6 @@ namespace Ragon.Client _failedListeners.Add(listener); _joinListeners.Add(listener); _leftListeners.Add(listener); - _sceneListeners.Add(listener); _ownershipChangedListeners.Add(listener); _playerJoinListeners.Add(listener); _playerLeftListeners.Add(listener); @@ -66,7 +63,6 @@ namespace Ragon.Client _failedListeners.Remove(listener); _joinListeners.Remove(listener); _leftListeners.Remove(listener); - _sceneListeners.Remove(listener); _ownershipChangedListeners.Remove(listener); _playerJoinListeners.Remove(listener); _playerLeftListeners.Remove(listener); @@ -93,11 +89,6 @@ namespace Ragon.Client _authorizationListeners.Add(listener); } - public void Add(IRagonSceneRequestListener listener) - { - _sceneRequestListeners.Add(listener); - } - public void Add(IRagonConnectionListener listener) { _connectionListeners.Add(listener); @@ -118,11 +109,6 @@ namespace Ragon.Client _leftListeners.Add(listener); } - public void Add(IRagonSceneListener listener) - { - _sceneListeners.Add(listener); - } - public void Add(IRagonOwnershipChangedListener listener) { _ownershipChangedListeners.Add(listener); @@ -158,11 +144,6 @@ namespace Ragon.Client _delayedActions.Add(() => _dataListeners.Remove(listener)); } - public void Remove(IRagonSceneRequestListener listener) - { - _delayedActions.Add(() => _sceneRequestListeners.Remove(listener)); - } - public void Remove(IRagonAuthorizationListener listener) { _delayedActions.Add(() => _authorizationListeners.Remove(listener)); @@ -188,11 +169,6 @@ namespace Ragon.Client _delayedActions.Add(() => _leftListeners.Remove(listener)); } - public void Remove(IRagonSceneListener listener) - { - _delayedActions.Add(() => _sceneListeners.Remove(listener)); - } - public void Remove(IRagonOwnershipChangedListener listener) { _delayedActions.Add(() => _ownershipChangedListeners.Remove(listener)); @@ -265,18 +241,6 @@ namespace Ragon.Client listener.OnPlayerJoined(_client, player); } - public void OnSceneLoaded() - { - foreach (var listener in _sceneListeners) - listener.OnSceneLoaded(_client); - } - - public void OnSceneRequest(string sceneName) - { - foreach (var listener in _sceneRequestListeners) - listener.OnRequestScene(_client, sceneName); - } - public void OnJoined() { foreach (var listener in _joinListeners) diff --git a/Ragon.Client/Sources/RagonRoom.cs b/Ragon.Client/Sources/RagonRoom.cs index ed19329..86e831c 100644 --- a/Ragon.Client/Sources/RagonRoom.cs +++ b/Ragon.Client/Sources/RagonRoom.cs @@ -47,11 +47,9 @@ namespace Ragon.Client } } - private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer); + private delegate void OnEventDelegate(RagonPlayer player, RagonStream serializer); private readonly RagonClient _client; - private readonly RagonScene _scene; - private readonly RagonEntityCache _entityCache; private readonly RagonPlayerCache _playerCache; private readonly RoomParameters _parameters; private readonly RagonUserData _userData; @@ -59,47 +57,40 @@ namespace Ragon.Client public string Id => _parameters.RoomId; public int MinPlayers => _parameters.Min; public int MaxPlayers => _parameters.Max; - public string Scene => _scene.Name; + public string Scene => "none"; public IReadOnlyList Players => _playerCache.Players; public RagonPlayer Local => _playerCache.Local; public RagonPlayer Owner => _playerCache.Owner; public RagonUserData UserData => _userData; - private readonly Dictionary _events = new Dictionary(); + private readonly Dictionary _events = new(); - private readonly Dictionary>> _localListeners = - new Dictionary>>(); + private readonly Dictionary>> _localListeners = new(); - private readonly Dictionary>> _listeners = - new Dictionary>>(); + private readonly Dictionary>> _listeners = new(); public RagonRoom(RagonClient client, - RagonEntityCache entityCache, RagonPlayerCache playerCache, - RoomParameters parameters, - RagonScene scene) + RoomParameters parameters) { _client = client; _parameters = parameters; - _entityCache = entityCache; _playerCache = playerCache; - _scene = scene; _userData = new RagonUserData(); } internal void Cleanup() { - _entityCache.Cleanup(); _playerCache.Cleanup(); } internal void Update(string sceneName) { - _scene.Update(sceneName); + // _scene.Update(sceneName); } - internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonBuffer buffer) + internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonStream buffer) { if (_events.TryGetValue(eventCode, out var evnt)) evnt?.Invoke(caller, buffer); @@ -107,11 +98,11 @@ namespace Ragon.Client RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined"); } - internal void HandleUserData(RagonBuffer buffer) + internal void HandleUserData(RagonStream buffer) { _userData.Read(buffer); } - + public IDisposable OnEvent(Action callback) where TEvent : IRagonEvent, new() { var t = new TEvent(); @@ -147,23 +138,73 @@ namespace Ragon.Client return new EventSubscription(callbacks, localCallbacks, action); } - public void LoadScene(string sceneName) => _scene.Load(sceneName); - public void SceneLoaded() => _scene.SceneLoaded(); + public void ReplicateEvent(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode) + where TEvent : IRagonEvent, new() + { + var evntId = _client.Event.GetEventCode(evnt); + var buffer = _client.Buffer; - public void ReplicateEvent(TEvent evnt, RagonTarget target, RagonReplicationMode mode) - where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode); + { + if (replicationMode == RagonReplicationMode.Local && + _localListeners.TryGetValue(evntId, out var localListeners)) + { + foreach (var listener in localListeners) + listener.Invoke(_client.Room.Local, evnt); + return; + } + } - public void ReplicateEvent(TEvent evnt, RagonPlayer target, RagonReplicationMode mode) - where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode); + { + if (replicationMode == RagonReplicationMode.LocalAndServer && + _localListeners.TryGetValue(evntId, out var localListeners)) + { + foreach (var listener in localListeners) + listener.Invoke(_client.Room.Local, evnt); + } + } - public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable); + buffer.Clear(); + buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT); + buffer.WriteUShort(evntId); + buffer.WriteByte((byte)replicationMode); + buffer.WriteByte((byte)target); - public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null); - public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload); - public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player); + evnt.Serialize(buffer); - public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null); - public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload); + var sendData = buffer.ToArray(); + _client.Reliable.Send(sendData); + } + + public void ReplicateEvent(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode) + where TEvent : IRagonEvent, new() + { + var evntId = _client.Event.GetEventCode(evnt); + var buffer = _client.Buffer; + + buffer.Clear(); + buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT); + buffer.WriteUShort(evntId); + buffer.WriteByte((byte)replicationMode); + buffer.WriteByte((byte)RagonTarget.Player); + buffer.WriteUShort(target.PeerId); + + evnt.Serialize(buffer); + + var sendData = buffer.ToArray(); + _client.Reliable.Send(sendData); + } + + public void ReplicateData(byte[] data, bool reliable) + { + var sendData = new byte[data.Length + 1]; + sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA; + Array.Copy(data, 0, sendData, 1, data.Length); + + if (reliable) + _client.Reliable.Send(sendData); + else + _client.Unreliable.Send(sendData); + } public void Dispose() { diff --git a/Ragon.Client/Sources/RagonSession.cs b/Ragon.Client/Sources/RagonSession.cs index a4a242b..9c61168 100644 --- a/Ragon.Client/Sources/RagonSession.cs +++ b/Ragon.Client/Sources/RagonSession.cs @@ -21,9 +21,9 @@ namespace Ragon.Client public class RagonSession { private readonly RagonClient _client; - private readonly RagonBuffer _buffer; + private readonly RagonStream _buffer; - public RagonSession(RagonClient client, RagonBuffer buffer) + public RagonSession(RagonClient client, RagonStream buffer) { _client = client; _buffer = buffer; diff --git a/Ragon.Client/Sources/RagonUserData.cs b/Ragon.Client/Sources/RagonUserData.cs index 3468a5f..c00a479 100644 --- a/Ragon.Client/Sources/RagonUserData.cs +++ b/Ragon.Client/Sources/RagonUserData.cs @@ -57,7 +57,7 @@ namespace Ragon.Client { } - public IReadOnlyList Read(RagonBuffer buffer) + public IReadOnlyList Read(RagonStream buffer) { var len = buffer.ReadUShort(); var changes = new List(len); @@ -67,7 +67,7 @@ namespace Ragon.Client var valueSize = buffer.ReadUShort(); if (valueSize > 0) { - var value = buffer.ReadBytes(valueSize); + var value = buffer.ReadBinary(valueSize); _properties[key] = value; } else @@ -81,7 +81,7 @@ namespace Ragon.Client return changes; } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { buffer.WriteUShort((ushort)_localChanges.Count); foreach (var propertyChanged in _localChanges) @@ -90,7 +90,7 @@ namespace Ragon.Client if (_properties.TryGetValue(propertyChanged, out var property)) { buffer.WriteUShort((ushort)property.Length); - buffer.WriteBytes(property); + buffer.WriteBinary(property); } else { diff --git a/Ragon.Client/Sources/RagonUserDataReadOnly.cs b/Ragon.Client/Sources/RagonUserDataReadOnly.cs index c09f617..2958342 100644 --- a/Ragon.Client/Sources/RagonUserDataReadOnly.cs +++ b/Ragon.Client/Sources/RagonUserDataReadOnly.cs @@ -35,12 +35,12 @@ public class RagonUserDataReadOnly : IUserData { } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { } - public IReadOnlyList Read(RagonBuffer buffer) + public IReadOnlyList Read(RagonStream buffer) { var len = buffer.ReadUShort(); var changes = new List(len); @@ -50,7 +50,7 @@ public class RagonUserDataReadOnly : IUserData var valueSize = buffer.ReadUShort(); if (valueSize > 0) { - var value = buffer.ReadBytes(valueSize); + var value = buffer.ReadBinary(valueSize); _properties[key] = value; } diff --git a/Ragon.Client/Sources/Utils/CompressorExtension.cs b/Ragon.Client/Sources/Replication/Compressor/Extensions.cs similarity index 100% rename from Ragon.Client/Sources/Utils/CompressorExtension.cs rename to Ragon.Client/Sources/Replication/Compressor/Extensions.cs diff --git a/Ragon.Client/Sources/Compressor/FloatCompressor.cs b/Ragon.Client/Sources/Replication/Compressor/FloatCompressor.cs similarity index 100% rename from Ragon.Client/Sources/Compressor/FloatCompressor.cs rename to Ragon.Client/Sources/Replication/Compressor/FloatCompressor.cs diff --git a/Ragon.Client/Sources/Compressor/IntCompressor.cs b/Ragon.Client/Sources/Replication/Compressor/IntCompressor.cs similarity index 100% rename from Ragon.Client/Sources/Compressor/IntCompressor.cs rename to Ragon.Client/Sources/Replication/Compressor/IntCompressor.cs diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Replication/Entity/RagonEntity.cs similarity index 98% rename from Ragon.Client/Sources/Entity/RagonEntity.cs rename to Ragon.Client/Sources/Replication/Entity/RagonEntity.cs index 43d5bcb..b37e891 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Replication/Entity/RagonEntity.cs @@ -17,7 +17,7 @@ using Ragon.Protocol; -namespace Ragon.Client +namespace Ragon.Client.Replication { public sealed class RagonEntity : IDisposable { @@ -118,9 +118,9 @@ namespace Ragon.Client var buffer = new RagonBuffer(); - data.Write(buffer); + // data.Write(buffer); - payload.Deserialize(buffer); + // payload.Deserialize(buffer); return payload; } @@ -251,7 +251,7 @@ namespace Ragon.Client { _events.Add(eventCode, (player, serializer) => { - t.Deserialize(serializer); + // t.Deserialize(serializer); foreach (var callbackListener in callbacks) callbackListener.Invoke(player, t); diff --git a/Ragon.Client/Sources/Entity/RagonEntityState.cs b/Ragon.Client/Sources/Replication/Entity/RagonEntityState.cs similarity index 98% rename from Ragon.Client/Sources/Entity/RagonEntityState.cs rename to Ragon.Client/Sources/Replication/Entity/RagonEntityState.cs index 53000de..64d17de 100644 --- a/Ragon.Client/Sources/Entity/RagonEntityState.cs +++ b/Ragon.Client/Sources/Replication/Entity/RagonEntityState.cs @@ -17,7 +17,7 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; public sealed class RagonEntityState { diff --git a/Ragon.Client/Sources/Entity/RagonPayload.cs b/Ragon.Client/Sources/Replication/Entity/RagonPayload.cs similarity index 81% rename from Ragon.Client/Sources/Entity/RagonPayload.cs rename to Ragon.Client/Sources/Replication/Entity/RagonPayload.cs index 1efb817..d98c86e 100644 --- a/Ragon.Client/Sources/Entity/RagonPayload.cs +++ b/Ragon.Client/Sources/Replication/Entity/RagonPayload.cs @@ -17,13 +17,13 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; public class RagonPayload { public static RagonPayload Empty = new RagonPayload(0); - private readonly uint[] _data = new uint[128]; + private byte[] _data = new byte[128]; private readonly int _size = 0; public RagonPayload(int capacity) @@ -32,14 +32,14 @@ public class RagonPayload } public int Size => _size; - public void Read(RagonBuffer buffer) + public void Read(RagonStream buffer) { - buffer.ReadArray(_data, _size); + _data = buffer.ReadBinary(_size); } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { - buffer.WriteArray(_data, _size); + buffer.WriteBinary(_data); } public override string ToString() diff --git a/Ragon.Client/Sources/Entity/RagonProperty.cs b/Ragon.Client/Sources/Replication/Entity/RagonProperty.cs similarity index 98% rename from Ragon.Client/Sources/Entity/RagonProperty.cs rename to Ragon.Client/Sources/Replication/Entity/RagonProperty.cs index 7f78a9a..c2016ca 100644 --- a/Ragon.Client/Sources/Entity/RagonProperty.cs +++ b/Ragon.Client/Sources/Replication/Entity/RagonProperty.cs @@ -16,7 +16,7 @@ using Ragon.Protocol; -namespace Ragon.Client +namespace Ragon.Client.Replication { [Serializable] public class RagonProperty diff --git a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs b/Ragon.Client/Sources/Replication/Handler/EntityCreateHandler.cs similarity index 86% rename from Ragon.Client/Sources/Handler/EntityCreateHandler.cs rename to Ragon.Client/Sources/Replication/Handler/EntityCreateHandler.cs index f1d5d23..595cf5a 100644 --- a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/EntityCreateHandler.cs @@ -16,9 +16,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class EntityCreateHandler : IHandler +internal class EntityCreateHandler { private readonly RagonClient _client; private readonly RagonPlayerCache _playerCache; @@ -38,15 +38,15 @@ internal class EntityCreateHandler : IHandler _entityListener = entityListener; } - public void Handle(RagonBuffer reader) + public void Handle(RagonStream reader) { var attachId = reader.ReadUShort(); var entityType = reader.ReadUShort(); var entityId = reader.ReadUShort(); var ownerId = reader.ReadUShort(); var player = _playerCache.GetPlayerByPeer(ownerId); - var payload = new RagonPayload(reader.Capacity); - payload.Read(reader); + // var payload = new RagonPayload(reader.Capacity); + // payload.Read(reader); if (player == null) { @@ -59,7 +59,7 @@ internal class EntityCreateHandler : IHandler var hasAuthority = _playerCache.Local.Id == player.Id; var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated); - entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload); + // entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload); if (hasCreated) _entityListener.OnEntityCreated(entity); diff --git a/Ragon.Client/Sources/Handler/EntityEventHandler.cs b/Ragon.Client/Sources/Replication/Handler/EntityEventHandler.cs similarity index 95% rename from Ragon.Client/Sources/Handler/EntityEventHandler.cs rename to Ragon.Client/Sources/Replication/Handler/EntityEventHandler.cs index 2dd0937..4008260 100644 --- a/Ragon.Client/Sources/Handler/EntityEventHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/EntityEventHandler.cs @@ -16,9 +16,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class EntityEventHandler : IHandler +internal class EntityEventHandler { private readonly RagonPlayerCache _playerCache; private readonly RagonEntityCache _entityCache; diff --git a/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs b/Ragon.Client/Sources/Replication/Handler/EntityOwnershipHandler.cs similarity index 95% rename from Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs rename to Ragon.Client/Sources/Replication/Handler/EntityOwnershipHandler.cs index cb810e5..8b0da29 100644 --- a/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/EntityOwnershipHandler.cs @@ -17,9 +17,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class EntityOwnershipHandler: IHandler +internal class EntityOwnershipHandler { private readonly RagonListenerList _listenerList; private readonly RagonPlayerCache _playerCache; diff --git a/Ragon.Client/Sources/Handler/EntityRemoveHandler.cs b/Ragon.Client/Sources/Replication/Handler/EntityRemoveHandler.cs similarity index 81% rename from Ragon.Client/Sources/Handler/EntityRemoveHandler.cs rename to Ragon.Client/Sources/Replication/Handler/EntityRemoveHandler.cs index a10b6bb..5219066 100644 --- a/Ragon.Client/Sources/Handler/EntityRemoveHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/EntityRemoveHandler.cs @@ -17,9 +17,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class EntityRemoveHandler: IHandler +internal class EntityRemoveHandler { private readonly RagonEntityCache _entityCache; @@ -31,9 +31,9 @@ internal class EntityRemoveHandler: IHandler public void Handle(RagonBuffer reader) { var entityId = reader.ReadUShort(); - var payload = new RagonPayload(reader.Capacity); - payload.Read(reader); + // var payload = new RagonPayload(reader.Capacity); + // payload.Read(reader); - _entityCache.OnDestroy(entityId, payload); + // _entityCache.OnDestroy(entityId, payload); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/EntityStateHandler.cs b/Ragon.Client/Sources/Replication/Handler/EntityStateHandler.cs similarity index 93% rename from Ragon.Client/Sources/Handler/EntityStateHandler.cs rename to Ragon.Client/Sources/Replication/Handler/EntityStateHandler.cs index cc864ac..3a78941 100644 --- a/Ragon.Client/Sources/Handler/EntityStateHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/EntityStateHandler.cs @@ -16,9 +16,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class StateEntityHandler: IHandler +internal class StateEntityHandler { private readonly RagonEntityCache _entityCache; diff --git a/Ragon.Client/Sources/Handler/LoadSceneHandler.cs b/Ragon.Client/Sources/Replication/Handler/LoadSceneHandler.cs similarity index 90% rename from Ragon.Client/Sources/Handler/LoadSceneHandler.cs rename to Ragon.Client/Sources/Replication/Handler/LoadSceneHandler.cs index 739f9db..05a87e2 100644 --- a/Ragon.Client/Sources/Handler/LoadSceneHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/LoadSceneHandler.cs @@ -16,9 +16,9 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class SceneLoadHandler: IHandler +internal class SceneLoadHandler { private readonly RagonClient _client; private readonly RagonListenerList _listenerList; @@ -40,6 +40,6 @@ internal class SceneLoadHandler: IHandler room.Cleanup(); room.Update(sceneName); - _listenerList.OnSceneRequest(sceneName); + // _listenerList.OnSceneRequest(sceneName); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/SnapshotHandler.cs b/Ragon.Client/Sources/Replication/Handler/SnapshotHandler.cs similarity index 94% rename from Ragon.Client/Sources/Handler/SnapshotHandler.cs rename to Ragon.Client/Sources/Replication/Handler/SnapshotHandler.cs index 3a6cd13..e6a31fd 100644 --- a/Ragon.Client/Sources/Handler/SnapshotHandler.cs +++ b/Ragon.Client/Sources/Replication/Handler/SnapshotHandler.cs @@ -14,13 +14,11 @@ * limitations under the License. */ - -using System.Diagnostics; using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; -internal class SnapshotHandler : IHandler +internal class SnapshotHandler { private readonly IRagonEntityListener _entityListener; private readonly RagonClient _client; @@ -69,8 +67,8 @@ internal class SnapshotHandler : IHandler var payload = RagonPayload.Empty; if (payloadSize > 0) { - payload = new RagonPayload(payloadSize); - payload.Read(buffer); + // payload = new RagonPayload(payloadSize); + // payload.Read(buffer); } entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload); @@ -113,6 +111,6 @@ internal class SnapshotHandler : IHandler foreach (var entity in entities) entity.Attach(); - _listenerList.OnSceneLoaded(); + // _listenerList.OnSceneLoaded(); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/IRagonEntityListener.cs b/Ragon.Client/Sources/Replication/IRagonEntityListener.cs similarity index 95% rename from Ragon.Client/Sources/IRagonEntityListener.cs rename to Ragon.Client/Sources/Replication/IRagonEntityListener.cs index 906b5f5..ade945f 100644 --- a/Ragon.Client/Sources/IRagonEntityListener.cs +++ b/Ragon.Client/Sources/Replication/IRagonEntityListener.cs @@ -14,7 +14,7 @@ * limitations under the License. */ -namespace Ragon.Client; +namespace Ragon.Client.Replication; public interface IRagonEntityListener { diff --git a/Ragon.Client/Sources/IRagonPayload.cs b/Ragon.Client/Sources/Replication/IRagonPayload.cs similarity index 84% rename from Ragon.Client/Sources/IRagonPayload.cs rename to Ragon.Client/Sources/Replication/IRagonPayload.cs index a8fe72d..83ec452 100644 --- a/Ragon.Client/Sources/IRagonPayload.cs +++ b/Ragon.Client/Sources/Replication/IRagonPayload.cs @@ -18,8 +18,9 @@ using Ragon.Protocol; namespace Ragon.Client { - public interface IRagonPayload: IRagonSerializable + public interface IRagonPayload { - + public void Serialize(RagonStream buffer); + public void Deserialize(RagonStream buffer); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/IRagonSceneCollector.cs b/Ragon.Client/Sources/Replication/IRagonSceneCollector.cs similarity index 95% rename from Ragon.Client/Sources/IRagonSceneCollector.cs rename to Ragon.Client/Sources/Replication/IRagonSceneCollector.cs index ef657de..7ed453d 100644 --- a/Ragon.Client/Sources/IRagonSceneCollector.cs +++ b/Ragon.Client/Sources/Replication/IRagonSceneCollector.cs @@ -14,7 +14,7 @@ * limitations under the License. */ -namespace Ragon.Client; +namespace Ragon.Client.Replication; public interface IRagonSceneCollector { diff --git a/Ragon.Client/Sources/RagonEntityCache.cs b/Ragon.Client/Sources/Replication/RagonEntityCache.cs similarity index 98% rename from Ragon.Client/Sources/RagonEntityCache.cs rename to Ragon.Client/Sources/Replication/RagonEntityCache.cs index 13b3925..1ee2662 100644 --- a/Ragon.Client/Sources/RagonEntityCache.cs +++ b/Ragon.Client/Sources/Replication/RagonEntityCache.cs @@ -16,7 +16,7 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; public sealed class RagonEntityCache { @@ -58,7 +58,7 @@ public sealed class RagonEntityCache buffer.WriteUShort(entity.Type); buffer.WriteByte((byte)entity.Authority); - entity.State.WriteInfo(buffer); + // entity.State.WriteInfo(buffer); spawnPayload?.Write(buffer); diff --git a/Ragon.Client/Sources/RagonScene.cs b/Ragon.Client/Sources/Replication/RagonScene.cs similarity index 85% rename from Ragon.Client/Sources/RagonScene.cs rename to Ragon.Client/Sources/Replication/RagonScene.cs index f4fefb6..40a2327 100644 --- a/Ragon.Client/Sources/RagonScene.cs +++ b/Ragon.Client/Sources/Replication/RagonScene.cs @@ -16,20 +16,20 @@ using Ragon.Protocol; -namespace Ragon.Client; +namespace Ragon.Client.Replication; public class RagonScene { public string Name { get; private set; } - + private readonly RagonClient _client; private readonly RagonEntityCache _entityCache; private readonly RagonPlayerCache _playerCache; - + public RagonScene(RagonClient client, RagonPlayerCache playerCache, RagonEntityCache entityCache, string sceneName) { Name = sceneName; - + _client = client; _playerCache = playerCache; _entityCache = entityCache; @@ -39,11 +39,11 @@ public class RagonScene { Name = scene; } - + internal void Load(string sceneName) { var buffer = _client.Buffer; - + buffer.Clear(); buffer.WriteOperation(RagonOperation.LOAD_SCENE); buffer.WriteString(sceneName); @@ -55,38 +55,38 @@ public class RagonScene internal void SceneLoaded() { var buffer = _client.Buffer; - + buffer.Clear(); buffer.WriteOperation(RagonOperation.SCENE_LOADED); - if (_playerCache.IsRoomOwner) - _entityCache.WriteScene(buffer); - else - _entityCache.CacheScene(); - + // if (_playerCache.IsRoomOwner) + // _entityCache.WriteScene(buffer); + // else + // _entityCache.CacheScene(); + var sendData = buffer.ToArray(); _client.Reliable.Send(sendData); } - internal void ReplicateEvent(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode) + public void ReplicateEvent(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode) where TEvent : IRagonEvent, new() { var evntId = _client.Event.GetEventCode(evnt); var buffer = _client.Buffer; - + buffer.Clear(); buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT); buffer.WriteUShort(evntId); buffer.WriteByte((byte)replicationMode); buffer.WriteByte((byte)target); - + evnt.Serialize(buffer); - + var sendData = buffer.ToArray(); _client.Reliable.Send(sendData); } - - internal void ReplicateEvent(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode) + + public void ReplicateEvent(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode) where TEvent : IRagonEvent, new() { var evntId = _client.Event.GetEventCode(evnt); @@ -108,9 +108,9 @@ public class RagonScene public void ReplicateData(byte[] data, bool reliable) { var sendData = new byte[data.Length + 1]; - sendData[0] = (byte) RagonOperation.REPLICATE_RAW_DATA; + sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA; Array.Copy(data, 0, sendData, 1, data.Length); - + if (reliable) _client.Reliable.Send(sendData); else diff --git a/Ragon.Protocol/Sources/RagonRoomParameters.cs b/Ragon.Protocol/Sources/RagonRoomParameters.cs index b397f4a..614d2d2 100644 --- a/Ragon.Protocol/Sources/RagonRoomParameters.cs +++ b/Ragon.Protocol/Sources/RagonRoomParameters.cs @@ -17,24 +17,24 @@ namespace Ragon.Protocol { - public class RagonRoomParameters: IRagonSerializable + public class RagonRoomParameters { public string Scene { get; set; } public int Min { get; set; } public int Max { get; set; } - public void Serialize(RagonBuffer buffer) + public void Serialize(RagonStream buffer) { buffer.WriteString(Scene); - buffer.WriteInt(Min, 1, 32); - buffer.WriteInt(Max, 1, 32); + buffer.WriteInt(Min); + buffer.WriteInt(Max); } - public void Deserialize(RagonBuffer buffer) + public void Deserialize(RagonStream buffer) { Scene = buffer.ReadString(); - Min = buffer.ReadInt(1, 32); - Max = buffer.ReadInt(1, 32); + Min = buffer.ReadInt(); + Max = buffer.ReadInt(); } } } \ No newline at end of file diff --git a/Ragon.Protocol/Sources/RagonSerializable.cs b/Ragon.Protocol/Sources/RagonSerializable.cs index ed9f56a..e76fa9d 100644 --- a/Ragon.Protocol/Sources/RagonSerializable.cs +++ b/Ragon.Protocol/Sources/RagonSerializable.cs @@ -19,7 +19,7 @@ namespace Ragon.Protocol { public interface IRagonSerializable { - public void Serialize(RagonBuffer buffer); - public void Deserialize(RagonBuffer buffer); + public void Serialize(RagonStream buffer); + public void Deserialize(RagonStream buffer); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Entity/IRagonEntity.cs b/Ragon.Relay/Sources/Replication/Entity/IRagonEntity.cs similarity index 100% rename from Ragon.Server/Sources/Entity/IRagonEntity.cs rename to Ragon.Relay/Sources/Replication/Entity/IRagonEntity.cs diff --git a/Ragon.Server/Sources/Entity/IRagonEntityState.cs b/Ragon.Relay/Sources/Replication/Entity/IRagonEntityState.cs similarity index 100% rename from Ragon.Server/Sources/Entity/IRagonEntityState.cs rename to Ragon.Relay/Sources/Replication/Entity/IRagonEntityState.cs diff --git a/Ragon.Server/Sources/Entity/RagonEntity.cs b/Ragon.Relay/Sources/Replication/Entity/RagonEntity.cs similarity index 97% rename from Ragon.Server/Sources/Entity/RagonEntity.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonEntity.cs index be41ccc..75b7838 100644 --- a/Ragon.Server/Sources/Entity/RagonEntity.cs +++ b/Ragon.Relay/Sources/Replication/Entity/RagonEntity.cs @@ -14,7 +14,7 @@ * limitations under the License. */ - +using System.Collections.Generic; using Ragon.Protocol; using Ragon.Server.Event; using Ragon.Server.Room; @@ -61,7 +61,7 @@ public class RagonEntity : IRagonEntity { } - public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer) + public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonStream writer) { foreach (var evnt in _bufferedEvents) { @@ -114,7 +114,7 @@ public class RagonEntity : IRagonEntity player.Connection.Reliable.Send(sendData); } - public void Snapshot(RagonBuffer buffer) + public void Snapshot(RagonStream buffer) { buffer.WriteUShort(Type); buffer.WriteUShort(Id); @@ -229,7 +229,7 @@ public class RagonEntity : IRagonEntity _state.AddProperty(property); } - public void WriteState(RagonBuffer writer) + public void WriteState(RagonStream writer) { _state.Write(writer); } diff --git a/Ragon.Server/Sources/RagonEntityCache.cs b/Ragon.Relay/Sources/Replication/Entity/RagonEntityCache.cs similarity index 98% rename from Ragon.Server/Sources/RagonEntityCache.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonEntityCache.cs index 8dfbabe..18b1af0 100644 --- a/Ragon.Server/Sources/RagonEntityCache.cs +++ b/Ragon.Relay/Sources/Replication/Entity/RagonEntityCache.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using System.Collections.Generic; using Ragon.Server.Entity; namespace Ragon.Server; diff --git a/Ragon.Server/Sources/Entity/RagonEntityParameters.cs b/Ragon.Relay/Sources/Replication/Entity/RagonEntityParameters.cs similarity index 100% rename from Ragon.Server/Sources/Entity/RagonEntityParameters.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonEntityParameters.cs diff --git a/Ragon.Server/Sources/Entity/RagonEntityState.cs b/Ragon.Relay/Sources/Replication/Entity/RagonEntityState.cs similarity index 93% rename from Ragon.Server/Sources/Entity/RagonEntityState.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonEntityState.cs index 1782c28..959d9ea 100644 --- a/Ragon.Server/Sources/Entity/RagonEntityState.cs +++ b/Ragon.Relay/Sources/Replication/Entity/RagonEntityState.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using System.Collections.Generic; using Ragon.Protocol; namespace Ragon.Server.Entity; @@ -34,7 +35,7 @@ public class RagonEntityState: IRagonEntityState _properties.Add(property); } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { buffer.WriteUShort(_entity.Id); foreach (var property in _properties) @@ -60,7 +61,7 @@ public class RagonEntityState: IRagonEntityState } } - public void Snapshot(RagonBuffer buffer) + public void Snapshot(RagonStream buffer) { foreach (var property in _properties) { diff --git a/Ragon.Server/Sources/Entity/RagonPayload.cs b/Ragon.Relay/Sources/Replication/Entity/RagonPayload.cs similarity index 79% rename from Ragon.Server/Sources/Entity/RagonPayload.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonPayload.cs index c5a193f..bd7f862 100644 --- a/Ragon.Server/Sources/Entity/RagonPayload.cs +++ b/Ragon.Relay/Sources/Replication/Entity/RagonPayload.cs @@ -26,15 +26,15 @@ public class RagonPayload public ushort Size => (ushort) _size; - public void Read(RagonBuffer buffer) + public void Read(RagonStream buffer) { - _size = buffer.Capacity; - buffer.ReadArray(_data, _size); + // _size = buffer.Capacity; + // buffer.ReadArray(_data, _size); } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { - if (_size == 0) return; - buffer.WriteArray(_data, _size); + // if (_size == 0) return; + // buffer.WriteArray(_data, _size); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Entity/RagonProperty.cs b/Ragon.Relay/Sources/Replication/Entity/RagonProperty.cs similarity index 98% rename from Ragon.Server/Sources/Entity/RagonProperty.cs rename to Ragon.Relay/Sources/Replication/Entity/RagonProperty.cs index a92474a..b161b82 100644 --- a/Ragon.Server/Sources/Entity/RagonProperty.cs +++ b/Ragon.Relay/Sources/Replication/Entity/RagonProperty.cs @@ -14,6 +14,8 @@ * limitations under the License. */ +using System; +using System.Linq; using Ragon.Protocol; namespace Ragon.Server.Entity; diff --git a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs b/Ragon.Relay/Sources/Replication/Handler/EntityCreateOperation.cs similarity index 76% rename from Ragon.Server/Sources/Handler/EntityCreateOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/EntityCreateOperation.cs index ed6c6a9..5489d01 100644 --- a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/EntityCreateOperation.cs @@ -26,8 +26,10 @@ public sealed class EntityCreateOperation : BaseOperation private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityCreateOperation)); private RagonServerConfiguration _configuration; - public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration configuration) : - base(reader, writer) + public EntityCreateOperation( + RagonStream reader, + RagonStream writer, + RagonServerConfiguration configuration) : base(reader, writer) { _configuration = configuration; } @@ -38,7 +40,7 @@ public sealed class EntityCreateOperation : BaseOperation var room = context.Room; var attachId = Reader.ReadUShort(); var entityType = Reader.ReadUShort(); - var eventAuthority = (RagonAuthority) Reader.ReadByte(); + var eventAuthority = (RagonAuthority)Reader.ReadByte(); var propertiesCount = Reader.ReadUShort(); var entityParameters = new RagonEntityParameters() @@ -49,7 +51,7 @@ public sealed class EntityCreateOperation : BaseOperation StaticId = 0, BufferedEvents = context.LimitBufferedEvents, }; - + var entity = new RagonEntity(entityParameters); for (var i = 0; i < propertiesCount; i++) { @@ -58,22 +60,21 @@ public sealed class EntityCreateOperation : BaseOperation entity.AddProperty(new RagonProperty(propertySize, propertyType, _configuration.LimitPropertySize)); } - - if (Reader.Capacity > 0) - entity.Payload.Read(Reader); - - var plugin = room.Plugin; - if (!plugin.OnEntityCreate(player, entity)) - return; - + + // if (Reader.Capacity > 0) + // entity.Payload.Read(Reader); + + // var plugin = room.Plugin; + // if (!plugin.OnEntityCreate(player, entity)) + // return; + entity.Attach(player); - room.AttachEntity(entity); - player.AttachEntity(entity); - + // room.AttachEntity(entity); + // player.AttachEntity(entity); + entity.Create(); - - _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}"); + + _logger.Trace( + $"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}"); } - - } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/EntityEventOperation.cs b/Ragon.Relay/Sources/Replication/Handler/EntityEventOperation.cs similarity index 74% rename from Ragon.Server/Sources/Handler/EntityEventOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/EntityEventOperation.cs index 2135674..948ccce 100644 --- a/Ragon.Server/Sources/Handler/EntityEventOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/EntityEventOperation.cs @@ -25,7 +25,7 @@ public sealed class EntityEventOperation : BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityEventOperation)); - public EntityEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public EntityEventOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -35,11 +35,11 @@ public sealed class EntityEventOperation : BaseOperation var room = context.Room; var entityId = Reader.ReadUShort(); - if (!room.Entities.TryGetValue(entityId, out var ent)) - { - _logger.Warning($"Entity not found for event with Id {entityId}"); - return; - } + // if (!room.Entities.TryGetValue(entityId, out var ent)) + // { + // _logger.Warning($"Entity not found for event with Id {entityId}"); + // return; + // } var eventId = Reader.ReadUShort(); var eventMode = (RagonReplicationMode)Reader.ReadByte(); @@ -52,12 +52,12 @@ public sealed class EntityEventOperation : BaseOperation var @event = new RagonEvent(player, eventId); @event.Read(Reader); - if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer)) - { - ent.ReplicateEvent(player, @event, eventMode, targetPlayer); - return; - } - - ent.ReplicateEvent(player, @event, eventMode, targetMode); + // if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer)) + // { + // ent.ReplicateEvent(player, @event, eventMode, targetPlayer); + // return; + // } + // + // ent.ReplicateEvent(player, @event, eventMode, targetMode); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/EntityOwnershipOperation.cs b/Ragon.Relay/Sources/Replication/Handler/EntityOwnershipOperation.cs similarity index 56% rename from Ragon.Server/Sources/Handler/EntityOwnershipOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/EntityOwnershipOperation.cs index dff84e7..50e6610 100644 --- a/Ragon.Server/Sources/Handler/EntityOwnershipOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/EntityOwnershipOperation.cs @@ -25,7 +25,7 @@ public sealed class EntityOwnershipOperation : BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityOwnershipOperation)); - public EntityOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public EntityOwnershipOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -37,17 +37,17 @@ public sealed class EntityOwnershipOperation : BaseOperation var entityId = Reader.ReadUShort(); var playerPeerId = Reader.ReadUShort(); - if (!room.Entities.TryGetValue(entityId, out var entity)) - { - _logger.Error($"Entity not found with id {entityId}"); - return; - } + // if (!room.Entities.TryGetValue(entityId, out var entity)) + // { + // _logger.Error($"Entity not found with id {entityId}"); + // return; + // } - if (entity.Owner.Connection.Id != currentOwner.Connection.Id) - { - _logger.Error($"Player not owner of entity with id {entityId}"); - return; - } + // if (entity.Owner.Connection.Id != currentOwner.Connection.Id) + // { + // _logger.Error($"Player not owner of entity with id {entityId}"); + // return; + // } if (!room.Players.TryGetValue(playerPeerId, out var nextOwner)) { @@ -55,21 +55,21 @@ public sealed class EntityOwnershipOperation : BaseOperation return; } - currentOwner.Entities.Remove(entity); - nextOwner.Entities.Add(entity); - - entity.Attach(nextOwner); - - _logger.Trace($"Entity {entity.Id} next owner {nextOwner.Connection.Id}"); - - Writer.Clear(); - Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED); - Writer.WriteUShort(playerPeerId); - Writer.WriteUShort(1); - Writer.WriteUShort(entity.Id); - - var sendData = Writer.ToArray(); - foreach (var player in room.PlayerList) - player.Connection.Reliable.Send(sendData); + // currentOwner.Entities.Remove(entity); + // nextOwner.Entities.Add(entity); + // + // entity.Attach(nextOwner); + // + // _logger.Trace($"Entity {entity.Id} next owner {nextOwner.Connection.Id}"); + // + // Writer.Clear(); + // Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED); + // Writer.WriteUShort(playerPeerId); + // Writer.WriteUShort(1); + // Writer.WriteUShort(entity.Id); + // + // var sendData = Writer.ToArray(); + // foreach (var player in room.PlayerList) + // player.Connection.Reliable.Send(sendData); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/EntityRemoveOperation.cs b/Ragon.Relay/Sources/Replication/Handler/EntityRemoveOperation.cs similarity index 62% rename from Ragon.Server/Sources/Handler/EntityRemoveOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/EntityRemoveOperation.cs index f340736..51a000b 100644 --- a/Ragon.Server/Sources/Handler/EntityRemoveOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/EntityRemoveOperation.cs @@ -25,7 +25,7 @@ public sealed class EntityDestroyOperation: BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityDestroyOperation)); - public EntityDestroyOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public EntityDestroyOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -35,21 +35,21 @@ public sealed class EntityDestroyOperation: BaseOperation var room = context.Room; var entityId = Reader.ReadUShort(); - if (room.Entities.TryGetValue(entityId, out var entity) && entity.Owner.Connection.Id == player.Connection.Id) - { - var payload = new RagonPayload(); - payload.Read(Reader); - - room.DetachEntity(entity); - player.DetachEntity(entity); - - entity.Destroy(); - - _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}"); - } - else - { - _logger.Trace($"Entity {entity.Id} not found or Player {context.Connection.Id}|{context.LobbyPlayer.Name} have not authority"); - } + // if (room.Entities.TryGetValue(entityId, out var entity) && entity.Owner.Connection.Id == player.Connection.Id) + // { + // var payload = new RagonPayload(); + // payload.Read(Reader); + // + // room.DetachEntity(entity); + // player.DetachEntity(entity); + // + // entity.Destroy(); + // + // _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}"); + // } + // else + // { + // _logger.Trace($"Entity {entity.Id} not found or Player {context.Connection.Id}|{context.LobbyPlayer.Name} have not authority"); + // } } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/EntityStateOperation.cs b/Ragon.Relay/Sources/Replication/Handler/EntityStateOperation.cs similarity index 75% rename from Ragon.Server/Sources/Handler/EntityStateOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/EntityStateOperation.cs index 5fe4746..f509bcb 100644 --- a/Ragon.Server/Sources/Handler/EntityStateOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/EntityStateOperation.cs @@ -24,7 +24,7 @@ public sealed class EntityStateOperation: BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityStateOperation)); - public EntityStateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public EntityStateOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -36,15 +36,15 @@ public sealed class EntityStateOperation: BaseOperation for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++) { - var entityId = Reader.ReadUShort(); - if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, Reader)) - { - room.Track(entity); - } - else - { - _logger.Error($"Entity with Id {entityId} not found, replication interrupted"); - } + // var entityId = Reader.ReadUShort(); + // if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, Reader)) + // { + // room.Track(entity); + // } + // else + // { + // _logger.Error($"Entity with Id {entityId} not found, replication interrupted"); + // } } } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/SceneLoadOperation.cs b/Ragon.Relay/Sources/Replication/Handler/SceneLoadOperation.cs similarity index 96% rename from Ragon.Server/Sources/Handler/SceneLoadOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/SceneLoadOperation.cs index 8a41975..01c9906 100644 --- a/Ragon.Server/Sources/Handler/SceneLoadOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/SceneLoadOperation.cs @@ -24,7 +24,7 @@ public class SceneLoadOperation: BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadOperation)); - public SceneLoadOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) {} + public SceneLoadOperation(RagonStream reader, RagonStream writer) : base(reader, writer) {} public override void Handle(RagonContext context, NetworkChannel channel) { diff --git a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs b/Ragon.Relay/Sources/Replication/Handler/SceneLoadedOperation.cs similarity index 68% rename from Ragon.Server/Sources/Handler/SceneLoadedOperation.cs rename to Ragon.Relay/Sources/Replication/Handler/SceneLoadedOperation.cs index 82ca96c..184cd43 100644 --- a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs +++ b/Ragon.Relay/Sources/Replication/Handler/SceneLoadedOperation.cs @@ -14,7 +14,6 @@ * limitations under the License. */ - using Ragon.Protocol; using Ragon.Server.Entity; using Ragon.Server.IO; @@ -29,7 +28,7 @@ namespace Ragon.Server.Handler private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadedOperation)); private RagonServerConfiguration _configuration; - public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration serverConfiguration) : base(reader, writer) + public SceneLoadedOperation(RagonStream reader, RagonStream writer, RagonServerConfiguration serverConfiguration) : base(reader, writer) { _configuration = serverConfiguration; } @@ -78,16 +77,16 @@ namespace Ragon.Server.Handler var roomPlugin = room.Plugin; - if (!roomPlugin.OnEntityCreate(player, entity)) continue; - - var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}"; - var entityInfo = $"{entity.Id}:{entity.Type}"; - - _logger.Trace($"{playerInfo} created static entity {entityInfo}"); - - entity.Attach(player); - room.AttachEntity(entity); - player.AttachEntity(entity); + // if (!roomPlugin.OnEntityCreate(player, entity)) continue; + // + // var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}"; + // var entityInfo = $"{entity.Id}:{entity.Type}"; + // + // _logger.Trace($"{playerInfo} created static entity {entityInfo}"); + // + // entity.Attach(player); + // room.AttachEntity(entity); + // player.AttachEntity(entity); } _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded"); @@ -98,27 +97,27 @@ namespace Ragon.Server.Handler { DispatchPlayerJoinExcludePlayer(room, roomPlayer, Writer); - roomPlayer.SetReady(); + // roomPlayer.SetReady(); } room.UpdateReadyPlayerList(); - DispatchSnapshot(room, room.WaitPlayersList, Writer); + // DispatchSnapshot(room, room.WaitPlayersList, Writer); room.WaitPlayersList.Clear(); } else if (owner.IsLoaded) { - player.SetReady(); + // player.SetReady(); DispatchPlayerJoinExcludePlayer(room, player, Writer); room.UpdateReadyPlayerList(); - DispatchSnapshot(room, new List() { player }, Writer); - - foreach (var entity in room.EntityList) - entity.RestoreBufferedEvents(player, Writer); + // DispatchSnapshot(room, new List() { player }, Writer); + // + // foreach (var entity in room.EntityList) + // entity.RestoreBufferedEvents(player, Writer); } else { @@ -127,7 +126,7 @@ namespace Ragon.Server.Handler } } - private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer) + private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.PLAYER_JOINED); @@ -143,26 +142,26 @@ namespace Ragon.Server.Handler } } - private void DispatchSnapshot(RagonRoom room, List receviersList, RagonBuffer writer) - { - writer.Clear(); - writer.WriteOperation(RagonOperation.SNAPSHOT); - - var dynamicEntities = room.DynamicEntitiesList; - var dynamicEntitiesCount = (ushort)dynamicEntities.Count; - writer.WriteUShort(dynamicEntitiesCount); - foreach (var entity in dynamicEntities) - entity.Snapshot(writer); - - var staticEntities = room.StaticEntitiesList; - var staticEntitiesCount = (ushort)staticEntities.Count; - writer.WriteUShort(staticEntitiesCount); - foreach (var entity in staticEntities) - entity.Snapshot(writer); - - var sendData = writer.ToArray(); - foreach (var player in receviersList) - player.Connection.Reliable.Send(sendData); - } + // private void DispatchSnapshot(RagonRoom room, List receviersList, RagonBuffer writer) + // { + // writer.Clear(); + // writer.WriteOperation(RagonOperation.SNAPSHOT); + // + // var dynamicEntities = room.DynamicEntitiesList; + // var dynamicEntitiesCount = (ushort)dynamicEntities.Count; + // writer.WriteUShort(dynamicEntitiesCount); + // foreach (var entity in dynamicEntities) + // entity.Snapshot(writer); + // + // var staticEntities = room.StaticEntitiesList; + // var staticEntitiesCount = (ushort)staticEntities.Count; + // writer.WriteUShort(staticEntitiesCount); + // foreach (var entity in staticEntities) + // entity.Snapshot(writer); + // + // var sendData = writer.ToArray(); + // foreach (var player in receviersList) + // player.Connection.Reliable.Send(sendData); + // } } } \ No newline at end of file diff --git a/Ragon.Relay/Sources/Replication/ReplicationRoom.cs b/Ragon.Relay/Sources/Replication/ReplicationRoom.cs new file mode 100644 index 0000000..5c8e30f --- /dev/null +++ b/Ragon.Relay/Sources/Replication/ReplicationRoom.cs @@ -0,0 +1,136 @@ +using System.Collections.Generic; +using System.Linq; +using Ragon.Protocol; +using Ragon.Server; +using Ragon.Server.Entity; +using Ragon.Server.Plugin; +using Ragon.Server.Room; + +namespace Ragon.Relay.Entity; + +public class RelayRoom: RagonRoom +{ + public Dictionary Entities { get; private set; } + public List DynamicEntitiesList { get; private set; } + public List StaticEntitiesList { get; private set; } + public List EntityList { get; private set; } + + private readonly HashSet _entitiesDirtySet; + + public RelayRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin) : base(roomId, info, roomPlugin) + { + Entities = new Dictionary(); + DynamicEntitiesList = new List(); + StaticEntitiesList = new List(); + EntityList = new List(); + + _entitiesDirtySet = new HashSet(); + } + + public void AttachEntity(RagonEntity entity) + { + Entities.Add(entity.Id, entity); + EntityList.Add(entity); + + if (entity.StaticId == 0) + DynamicEntitiesList.Add(entity); + else + StaticEntitiesList.Add(entity); + } + + public void DetachEntity(RagonEntity entity) + { + Entities.Remove(entity.Id); + EntityList.Remove(entity); + StaticEntitiesList.Remove(entity); + DynamicEntitiesList.Remove(entity); + + _entitiesDirtySet.Remove(entity); + } + + + public void Track(RagonEntity entity) + { + _entitiesDirtySet.Add(entity); + } + + + public void OnLeaved(RagonRoomPlayer player) + { + // var entitiesToDelete = player.Entities.DynamicList; + // Writer.WriteUShort((ushort)entitiesToDelete.Count); + // foreach (var entity in entitiesToDelete) + // { + // Writer.WriteUShort(entity.Id); + // DetachEntity(entity); + // } + // + // var sendData = Writer.ToArray(); + // Broadcast(sendData); + } + + public void Tick(float dt) + { + var entities = (ushort)_entitiesDirtySet.Count; + if (entities > 0) + { + Writer.Clear(); + Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE); + Writer.WriteUShort(entities); + + foreach (var entity in _entitiesDirtySet) + entity.WriteState(Writer); + + _entitiesDirtySet.Clear(); + + var sendData = Writer.ToArray(); + foreach (var roomPlayer in ReadyPlayersList) + roomPlayer.Connection.Unreliable.Send(sendData); + } + } + + public IRagonEntity? GetEntityById(ushort id) + { + return Entities.TryGetValue(id, out var entity) ? entity : null; + } + + public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player) + { + return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray(); + } + + void Deatach() + { + Entities.Clear(); + DynamicEntitiesList.Clear(); + StaticEntitiesList.Clear(); + EntityList.Clear(); + + _entitiesDirtySet.Clear(); + + // if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0) + // { + // var nextOwner = PlayerList[0]; + // + // Owner = nextOwner; + // + // var entitiesToUpdate = roomPlayer.Entities.StaticList; + // + // Writer.Clear(); + // Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED); + // Writer.WriteUShort(Owner.Connection.Id); + // Writer.WriteUShort((ushort)entitiesToUpdate.Count); + // + // foreach (var entity in entitiesToUpdate) + // { + // Writer.WriteUShort(entity.Id); + // + // entity.Attach(nextOwner); + // nextOwner.Entities.Add(entity); + // } + // + // var sendData = Writer.ToArray(); + // Broadcast(sendData); + // } + } +} \ No newline at end of file diff --git a/Ragon.Server/Sources/Data/RagonData.cs b/Ragon.Server/Sources/Data/RagonData.cs index 992f15f..c451fd2 100644 --- a/Ragon.Server/Sources/Data/RagonData.cs +++ b/Ragon.Server/Sources/Data/RagonData.cs @@ -12,7 +12,7 @@ public class RagonData { } - public void Read(RagonBuffer buffer) + public void Read(RagonStream buffer) { var len = buffer.ReadUShort(); for (int i = 0; i < len; i++) @@ -21,7 +21,7 @@ public class RagonData var valueSize = buffer.ReadUShort(); if (valueSize > 0) { - var value = buffer.ReadBytes(valueSize); + var value = buffer.ReadBinary(valueSize); _data[key] = value; } else @@ -31,14 +31,14 @@ public class RagonData } } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { buffer.WriteUShort((ushort)_data.Count); foreach (var prop in _data) { buffer.WriteString(prop.Key); buffer.WriteUShort((ushort)prop.Value.Length); - buffer.WriteBytes(prop.Value); + buffer.WriteBinary(prop.Value); } var toDelete = _data @@ -51,14 +51,14 @@ public class RagonData IsDirty = false; } - public void Snapshot(RagonBuffer buffer) + public void Snapshot(RagonStream buffer) { buffer.WriteUShort((ushort)_data.Count); foreach (var prop in _data) { buffer.WriteString(prop.Key); buffer.WriteUShort((ushort)prop.Value.Length); - buffer.WriteBytes(prop.Value); + buffer.WriteBinary(prop.Value); Console.WriteLine($"Key: {prop.Key} Value: {prop.Value.Length}"); } diff --git a/Ragon.Server/Sources/Event/RagonEvent.cs b/Ragon.Server/Sources/Event/RagonEvent.cs index 9bd591d..92d6fc6 100644 --- a/Ragon.Server/Sources/Event/RagonEvent.cs +++ b/Ragon.Server/Sources/Event/RagonEvent.cs @@ -37,15 +37,15 @@ public class RagonEvent EventCode = eventCode; } - public void Read(RagonBuffer buffer) + public void Read(RagonStream buffer) { - _size = buffer.Capacity; - buffer.ReadArray(_data, _size); + // _size = buffer.Capacity; + // buffer.ReadArray(_data, _size); } - public void Write(RagonBuffer buffer) + public void Write(RagonStream buffer) { - if (_size <= 0) return; - buffer.WriteArray(_data, _size); + // if (_size <= 0) return; + // buffer.WriteArray(_data, _size); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs index 27cdc7d..fc6d7cf 100644 --- a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs +++ b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs @@ -29,10 +29,10 @@ namespace Ragon.Server.Handler private readonly IRagonServer _server; private readonly RagonContextObserver _observer; private readonly RagonServerConfiguration _configuration; - private readonly RagonBuffer _writer; + private readonly RagonStream _writer; - public AuthorizationOperation(RagonBuffer reader, - RagonBuffer writer, + public AuthorizationOperation(RagonStream reader, + RagonStream writer, IRagonServer server, IServerPlugin serverPlugin, RagonContextObserver observer, diff --git a/Ragon.Server/Sources/Handler/BaseOperation.cs b/Ragon.Server/Sources/Handler/BaseOperation.cs index 3500f09..45a1d68 100644 --- a/Ragon.Server/Sources/Handler/BaseOperation.cs +++ b/Ragon.Server/Sources/Handler/BaseOperation.cs @@ -21,10 +21,10 @@ namespace Ragon.Server.Handler; public abstract class BaseOperation { - protected readonly RagonBuffer Reader; - protected readonly RagonBuffer Writer; + protected readonly RagonStream Reader; + protected readonly RagonStream Writer; - public BaseOperation(RagonBuffer reader, RagonBuffer writer) + public BaseOperation(RagonStream reader, RagonStream writer) { Reader = reader; Writer = writer; diff --git a/Ragon.Server/Sources/Handler/PlayerUserDataOperation.cs b/Ragon.Server/Sources/Handler/PlayerUserDataOperation.cs index 36d84d8..a384eb2 100644 --- a/Ragon.Server/Sources/Handler/PlayerUserDataOperation.cs +++ b/Ragon.Server/Sources/Handler/PlayerUserDataOperation.cs @@ -11,8 +11,8 @@ namespace Ragon.Server.Handler private readonly int _userDataLimit; public PlayerUserDataOperation( - RagonBuffer reader, - RagonBuffer writer, + RagonStream reader, + RagonStream writer, int userDataLimit ) : base(reader, writer) { diff --git a/Ragon.Server/Sources/Handler/RoomCreateOperation.cs b/Ragon.Server/Sources/Handler/RoomCreateOperation.cs index c1b09f0..28f417e 100644 --- a/Ragon.Server/Sources/Handler/RoomCreateOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomCreateOperation.cs @@ -32,8 +32,8 @@ namespace Ragon.Server.Handler private readonly RagonServerConfiguration _configuration; public RoomCreateOperation( - RagonBuffer reader, - RagonBuffer writer, + RagonStream reader, + RagonStream writer, IServerPlugin serverPlugin, RagonServerConfiguration configuration ) : base(reader, @@ -107,7 +107,7 @@ namespace Ragon.Server.Handler _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}"); } - private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer) + private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.JOIN_SUCCESS); diff --git a/Ragon.Server/Sources/Handler/RoomDataOperation.cs b/Ragon.Server/Sources/Handler/RoomDataOperation.cs index 90c49e7..8a477a9 100644 --- a/Ragon.Server/Sources/Handler/RoomDataOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomDataOperation.cs @@ -21,7 +21,7 @@ namespace Ragon.Server.Handler; public sealed class RoomDataOperation : BaseOperation { - public RoomDataOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public RoomDataOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -29,8 +29,8 @@ public sealed class RoomDataOperation : BaseOperation { var player = context.RoomPlayer; var room = context.Room; - - var data = Reader.RawData; + + var data = Reader.ReadBinary(Reader.Lenght); var dataSize = data.Length - 1; var headerSize = 3; var size = headerSize + dataSize; diff --git a/Ragon.Server/Sources/Handler/RoomEventOperation.cs b/Ragon.Server/Sources/Handler/RoomEventOperation.cs index 85082e7..1bcde79 100644 --- a/Ragon.Server/Sources/Handler/RoomEventOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomEventOperation.cs @@ -10,7 +10,7 @@ public class RoomEventOperation : BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomEventOperation)); - public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public RoomEventOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } diff --git a/Ragon.Server/Sources/Handler/RoomJoinOperation.cs b/Ragon.Server/Sources/Handler/RoomJoinOperation.cs index 2e38022..48b6b42 100644 --- a/Ragon.Server/Sources/Handler/RoomJoinOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomJoinOperation.cs @@ -26,7 +26,7 @@ public sealed class RoomJoinOperation : BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomJoinOperation)); - public RoomJoinOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public RoomJoinOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } @@ -54,7 +54,7 @@ public sealed class RoomJoinOperation : BaseOperation _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}"); } - private void JoinSuccess(RagonContext context, RagonRoom room, RagonBuffer writer) + private void JoinSuccess(RagonContext context, RagonRoom room, RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.JOIN_SUCCESS); @@ -81,7 +81,7 @@ public sealed class RoomJoinOperation : BaseOperation context.Connection.Reliable.Send(sendData); } - private void JoinFailed(RagonContext context, RagonBuffer writer) + private void JoinFailed(RagonContext context, RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.JOIN_FAILED); diff --git a/Ragon.Server/Sources/Handler/RoomJoinOrCreateOperation.cs b/Ragon.Server/Sources/Handler/RoomJoinOrCreateOperation.cs index 3cba271..e213760 100644 --- a/Ragon.Server/Sources/Handler/RoomJoinOrCreateOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomJoinOrCreateOperation.cs @@ -32,8 +32,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation private readonly RagonServerConfiguration _configuration; public RoomJoinOrCreateOperation( - RagonBuffer reader, - RagonBuffer writer, + RagonStream reader, + RagonStream writer, IServerPlugin serverPlugin, RagonServerConfiguration configuration ) : base(reader, writer) @@ -100,7 +100,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation } } - private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer) + private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.JOIN_SUCCESS); diff --git a/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs b/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs index 0ecb771..842f0ab 100644 --- a/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs @@ -24,7 +24,7 @@ public sealed class RoomLeaveOperation: BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomLeaveOperation)); - public RoomLeaveOperation(RagonBuffer reader, RagonBuffer writer): base(reader, writer) + public RoomLeaveOperation(RagonStream reader, RagonStream writer): base(reader, writer) { } diff --git a/Ragon.Server/Sources/Handler/RoomOwnershipOperation.cs b/Ragon.Server/Sources/Handler/RoomOwnershipOperation.cs index 0d06285..67070a1 100644 --- a/Ragon.Server/Sources/Handler/RoomOwnershipOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomOwnershipOperation.cs @@ -23,7 +23,7 @@ public sealed class RoomOwnershipOperation : BaseOperation { private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomOwnershipOperation)); - public RoomOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public RoomOwnershipOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } diff --git a/Ragon.Server/Sources/Handler/RoomUserDataOperation.cs b/Ragon.Server/Sources/Handler/RoomUserDataOperation.cs index 7a020db..7789f28 100644 --- a/Ragon.Server/Sources/Handler/RoomUserDataOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomUserDataOperation.cs @@ -27,8 +27,8 @@ public sealed class RoomUserDataOperation : BaseOperation private readonly int _userDataLimit; public RoomUserDataOperation( - RagonBuffer reader, - RagonBuffer writer, + RagonStream reader, + RagonStream writer, int userDataLimit ) : base(reader, writer) { diff --git a/Ragon.Server/Sources/Handler/TimestampSyncOperation.cs b/Ragon.Server/Sources/Handler/TimestampSyncOperation.cs index 8097e84..b8ece32 100644 --- a/Ragon.Server/Sources/Handler/TimestampSyncOperation.cs +++ b/Ragon.Server/Sources/Handler/TimestampSyncOperation.cs @@ -19,18 +19,18 @@ using Ragon.Server.IO; namespace Ragon.Server.Handler; -public class TimestampSyncOperation: BaseOperation +public class TimestampSyncOperation : BaseOperation { - public TimestampSyncOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + public TimestampSyncOperation(RagonStream reader, RagonStream writer) : base(reader, writer) { } public override void Handle(RagonContext context, NetworkChannel channel) { - var timestamp0 = Reader.Read(32); - var timestamp1 = Reader.Read(32); + var timestamp0 = (uint)Reader.ReadInt(); + var timestamp1 = (uint)Reader.ReadInt(); var value = new DoubleToUInt() { Int0 = timestamp0, Int1 = timestamp1 }; - + context.RoomPlayer?.SetTimestamp(value.Double); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Lobby/RagonLobbyDispatcher.cs b/Ragon.Server/Sources/Lobby/RagonLobbyDispatcher.cs index 1bc0b5d..8593bf6 100644 --- a/Ragon.Server/Sources/Lobby/RagonLobbyDispatcher.cs +++ b/Ragon.Server/Sources/Lobby/RagonLobbyDispatcher.cs @@ -12,7 +12,7 @@ public class RagonLobbyDispatcher _lobby = lobby; } - public void Write(RagonBuffer writer) + public void Write(RagonStream writer) { writer.Clear(); writer.WriteOperation(RagonOperation.ROOM_LIST_UPDATED); diff --git a/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs b/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs index 9291f7b..5622cab 100644 --- a/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs +++ b/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs @@ -81,7 +81,7 @@ public class LobbyInMemory : IRagonLobby _logger.Trace($"New room: {room.Id}"); foreach (var r in _rooms) - _logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count} Entities: {r.Entities.Count}"); + _logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count}"); } public bool RemoveIfEmpty(RagonRoom room) @@ -98,7 +98,7 @@ public class LobbyInMemory : IRagonLobby } foreach (var r in _rooms) - _logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count} Entities: {r.Entities.Count}"); + _logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count}"); return result; } diff --git a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs index c7f0a10..c9b48bb 100644 --- a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs @@ -14,8 +14,6 @@ * limitations under the License. */ -using Ragon.Server.Entity; -using Ragon.Server.IO; using Ragon.Server.Room; namespace Ragon.Server.Plugin; @@ -50,18 +48,6 @@ public class BaseRoomPlugin: IRoomPlugin { } - - public virtual bool OnEntityCreate(RagonRoomPlayer creator, IRagonEntity entity) - { - - return true; - } - - public virtual bool OnEntityRemove(RagonRoomPlayer remover, IRagonEntity entity) - { - - return true; - } public virtual bool OnData(RagonRoomPlayer player, byte[] data) { diff --git a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs index bbb0469..cd8326f 100644 --- a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs @@ -14,7 +14,6 @@ * limitations under the License. */ -using Ragon.Server.Entity; using Ragon.Server.Room; namespace Ragon.Server.Plugin; @@ -26,7 +25,5 @@ public interface IRoomPlugin void OnDetached(IRagonRoom room); bool OnPlayerJoined(RagonRoomPlayer player); bool OnPlayerLeaved(RagonRoomPlayer player); - bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity); - bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity); bool OnData(RagonRoomPlayer player, byte[] data); } \ No newline at end of file diff --git a/Ragon.Server/Sources/RagonServer.cs b/Ragon.Server/Sources/RagonServer.cs index 358eadc..661346b 100644 --- a/Ragon.Server/Sources/RagonServer.cs +++ b/Ragon.Server/Sources/RagonServer.cs @@ -35,8 +35,8 @@ public class RagonServer : IRagonServer, INetworkListener private readonly IRagonLobby _lobby; private readonly IServerPlugin _serverPlugin; private readonly RagonServerConfiguration _configuration; - private readonly RagonBuffer _reader; - private readonly RagonBuffer _writer; + private readonly RagonStream _reader; + private readonly RagonStream _writer; private readonly RagonScheduler _scheduler; private readonly Dictionary _contextsByConnection; private readonly Dictionary _contextsByPlayerId; @@ -60,8 +60,8 @@ public class RagonServer : IRagonServer, INetworkListener _lobby = new LobbyInMemory(); _lobbySerializer = new RagonLobbyDispatcher(_lobby); _scheduler = new RagonScheduler(); - _reader = new RagonBuffer(); - _writer = new RagonBuffer(); + _reader = new RagonStream(); + _writer = new RagonStream(); _tickRate = 1000 / _configuration.ServerTickRate; _timer = new Stopwatch(); @@ -78,14 +78,6 @@ public class RagonServer : IRagonServer, INetworkListener _handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _configuration); _handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer); _handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer); - _handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadOperation(_reader, _writer); - _handlers[(byte)RagonOperation.SCENE_LOADED] = new SceneLoadedOperation(_reader, _writer, _configuration); - _handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateOperation(_reader, _writer, _configuration); - _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); @@ -101,11 +93,13 @@ public class RagonServer : IRagonServer, INetworkListener if (_timer.ElapsedMilliseconds > _tickRate) { + + + _scheduler.Update(_timer.ElapsedMilliseconds); _timer.Restart(); - _scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f); - - SendTimestamp(); } + + SendTimestamp(); _server.Update(); } @@ -219,8 +213,8 @@ public class RagonServer : IRagonServer, INetworkListener _writer.Clear(); _writer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION); - _writer.Write(value.Int0, 32); - _writer.Write(value.Int1, 32); + _writer.WriteInt((int)value.Int0); + _writer.WriteInt((int)value.Int1); var sendData = _writer.ToArray(); _server.Broadcast(sendData, NetworkChannel.UNRELIABLE); diff --git a/Ragon.Server/Sources/Room/IRagonRoom.cs b/Ragon.Server/Sources/Room/IRagonRoom.cs index cd5197e..dcd29b6 100644 --- a/Ragon.Server/Sources/Room/IRagonRoom.cs +++ b/Ragon.Server/Sources/Room/IRagonRoom.cs @@ -15,7 +15,6 @@ */ using Ragon.Server.Data; -using Ragon.Server.Entity; using Ragon.Server.IO; namespace Ragon.Server.Room; @@ -36,6 +35,4 @@ public interface IRagonRoom RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection); RagonRoomPlayer GetPlayerById(string id); - IRagonEntity GetEntityById(ushort id); - IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer id); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Room/RagonRoom.cs b/Ragon.Server/Sources/Room/RagonRoom.cs index 7c40f3c..363db1e 100644 --- a/Ragon.Server/Sources/Room/RagonRoom.cs +++ b/Ragon.Server/Sources/Room/RagonRoom.cs @@ -16,7 +16,6 @@ using Ragon.Protocol; using Ragon.Server.Data; -using Ragon.Server.Entity; using Ragon.Server.Event; using Ragon.Server.IO; using Ragon.Server.Plugin; @@ -36,7 +35,7 @@ public class RagonRoom : IRagonRoom, IRagonAction public RagonData UserData { get; set; } public RagonRoomPlayer Owner { get; private set; } - public RagonBuffer Writer { get; } + public RagonStream Writer { get; } public IRoomPlugin Plugin { get; private set; } public Dictionary Players { get; private set; } @@ -44,12 +43,6 @@ public class RagonRoom : IRagonRoom, IRagonAction public List ReadyPlayersList { get; private set; } public List PlayerList { get; private set; } - public Dictionary Entities { get; private set; } - public List DynamicEntitiesList { get; private set; } - public List StaticEntitiesList { get; private set; } - public List EntityList { get; private set; } - - private readonly HashSet _entitiesDirtySet; private readonly List _bufferedEvents; private readonly int _limitBufferedEvents; @@ -66,39 +59,14 @@ public class RagonRoom : IRagonRoom, IRagonAction ReadyPlayersList = new List(info.Max); PlayerList = new List(info.Max); - Entities = new Dictionary(); - DynamicEntitiesList = new List(); - StaticEntitiesList = new List(); - EntityList = new List(); - - _entitiesDirtySet = new HashSet(); _bufferedEvents = new List(); _limitBufferedEvents = 1000; UserData = new RagonData(); - Writer = new RagonBuffer(); + Writer = new RagonStream(); } - public void AttachEntity(RagonEntity entity) - { - Entities.Add(entity.Id, entity); - EntityList.Add(entity); - - if (entity.StaticId == 0) - DynamicEntitiesList.Add(entity); - else - StaticEntitiesList.Add(entity); - } - - public void DetachEntity(RagonEntity entity) - { - Entities.Remove(entity.Id); - EntityList.Remove(entity); - StaticEntitiesList.Remove(entity); - DynamicEntitiesList.Remove(entity); - - _entitiesDirtySet.Remove(entity); - } + public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer) { @@ -238,22 +206,7 @@ public class RagonRoom : IRagonRoom, IRagonAction public void Tick(float dt) { - var entities = (ushort)_entitiesDirtySet.Count; - if (entities > 0) - { - Writer.Clear(); - Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE); - Writer.WriteUShort(entities); - - foreach (var entity in _entitiesDirtySet) - entity.WriteState(Writer); - - _entitiesDirtySet.Clear(); - - var sendData = Writer.ToArray(); - foreach (var roomPlayer in ReadyPlayersList) - roomPlayer.Connection.Unreliable.Send(sendData); - } + } public void AttachPlayer(RagonRoomPlayer player) @@ -277,43 +230,9 @@ public class RagonRoom : IRagonRoom, IRagonAction Writer.Clear(); Writer.WriteOperation(RagonOperation.PLAYER_LEAVED); Writer.WriteString(player.Id); - - var entitiesToDelete = player.Entities.DynamicList; - Writer.WriteUShort((ushort)entitiesToDelete.Count); - foreach (var entity in entitiesToDelete) - { - Writer.WriteUShort(entity.Id); - DetachEntity(entity); - } - - var sendData = Writer.ToArray(); - Broadcast(sendData); } - if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0) - { - var nextOwner = PlayerList[0]; - - Owner = nextOwner; - - var entitiesToUpdate = roomPlayer.Entities.StaticList; - - Writer.Clear(); - Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED); - Writer.WriteUShort(Owner.Connection.Id); - Writer.WriteUShort((ushort)entitiesToUpdate.Count); - - foreach (var entity in entitiesToUpdate) - { - Writer.WriteUShort(entity.Id); - - entity.Attach(nextOwner); - nextOwner.Entities.Add(entity); - } - - var sendData = Writer.ToArray(); - Broadcast(sendData); - } + player.OnDetached(); @@ -331,23 +250,13 @@ public class RagonRoom : IRagonRoom, IRagonAction public void UpdateMap(string sceneName) { Scene = sceneName; - - DynamicEntitiesList.Clear(); - StaticEntitiesList.Clear(); - Entities.Clear(); - EntityList.Clear(); - + foreach (var player in PlayerList) player.UnsetReady(); UpdateReadyPlayerList(); } - public void Track(RagonEntity entity) - { - _entitiesDirtySet.Add(entity); - } - public void Broadcast(byte[] data, NetworkChannel channel = NetworkChannel.RELIABLE) { if (channel == NetworkChannel.RELIABLE) @@ -386,16 +295,7 @@ public class RagonRoom : IRagonRoom, IRagonAction return PlayerList.FirstOrDefault(p => p.Id == id); } - public IRagonEntity? GetEntityById(ushort id) - { - return Entities.TryGetValue(id, out var entity) ? entity : null; - } - - public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player) - { - return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray(); - } - + public void Attach() { Plugin.OnAttached(this); @@ -410,12 +310,6 @@ public class RagonRoom : IRagonRoom, IRagonAction ReadyPlayersList.Clear(); PlayerList.Clear(); - Entities.Clear(); - DynamicEntitiesList.Clear(); - StaticEntitiesList.Clear(); - EntityList.Clear(); - - _entitiesDirtySet.Clear(); _bufferedEvents.Clear(); IsDone = true; diff --git a/Ragon.Server/Sources/Room/RagonRoomPlayer.cs b/Ragon.Server/Sources/Room/RagonRoomPlayer.cs index 61bcae9..aad1079 100644 --- a/Ragon.Server/Sources/Room/RagonRoomPlayer.cs +++ b/Ragon.Server/Sources/Room/RagonRoomPlayer.cs @@ -14,8 +14,6 @@ * limitations under the License. */ -using Ragon.Server.Data; -using Ragon.Server.Entity; using Ragon.Server.IO; namespace Ragon.Server.Room; @@ -29,7 +27,6 @@ public class RagonRoomPlayer public bool IsLoaded { get; private set; } public double Timestamp { get; private set; } public RagonRoom Room { get; private set; } - public RagonEntityCache Entities { get; private set; } public RagonRoomPlayer(RagonContext context, string id, string name) { @@ -37,19 +34,8 @@ public class RagonRoomPlayer Name = name; Context = context; Connection = context.Connection; - Entities = new RagonEntityCache(); } - public void AttachEntity(RagonEntity entity) - { - Entities.Add(entity); - } - - public void DetachEntity(RagonEntity entity) - { - Entities.Remove(entity); - } - internal void OnAttached(RagonRoom room) { Room = room; diff --git a/Ragon.Tests/Connection.cs b/Ragon.Tests/Connection.cs new file mode 100644 index 0000000..d540690 --- /dev/null +++ b/Ragon.Tests/Connection.cs @@ -0,0 +1,45 @@ +using NUnit.Mocks; +using Ragon.Client; +using Ragon.Relay; +using Ragon.Server; +using Ragon.Server.Logging; +using Ragon.Server.Plugin; + +namespace Ragon.Tests; + +public class Tests +{ + private RagonClient _client; + private RagonServer _server; + + [SetUp] + public void Setup() + { + LoggerManager.SetLoggerFactory(new RelayLoggerFactory()); + + var fakeNetwork = new FakeNetwork(); + var serverConfiguration = new RagonServerConfiguration() + { + LimitConnections = 100, + LimitRooms = 10, + LimitBufferedEvents = 500, + LimitPlayersPerRoom = 10, + LimitUserDataSize = 512, + LimitPropertySize = 512, + Port = 5000, + Protocol = "udp", + ServerKey = "defaultkey", + ServerTickRate = 30, + ServerAddress = "0.0.0.0", + }; + + _client = new RagonClient(fakeNetwork.ClientNetwork, 30); + _server = new RagonServer(fakeNetwork.ServerNetwork, new BaseServerPlugin(), serverConfiguration); + } + + [Test] + public void Test1() + { + + } +} \ No newline at end of file diff --git a/Ragon.Tests/Ragon.Tests.csproj b/Ragon.Tests/Ragon.Tests.csproj new file mode 100644 index 0000000..ba22d08 --- /dev/null +++ b/Ragon.Tests/Ragon.Tests.csproj @@ -0,0 +1,31 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + + diff --git a/Ragon.Tests/Utilities/FakeClientNetwork.cs b/Ragon.Tests/Utilities/FakeClientNetwork.cs new file mode 100644 index 0000000..fb346a3 --- /dev/null +++ b/Ragon.Tests/Utilities/FakeClientNetwork.cs @@ -0,0 +1,45 @@ +using Ragon.Client; +using Ragon.Protocol; + +namespace Ragon.Tests; + +public class FakeClientNetwork: INetworkConnection +{ + public void Close() + { + throw new NotImplementedException(); + } + + public INetworkChannel Reliable { get; } + public INetworkChannel Unreliable { get; } + public Action OnData { get; set; } + public Action OnConnected { get; set; } + public Action OnDisconnected { get; set; } + public ulong BytesSent { get; } + public ulong BytesReceived { get; } + public int Ping { get; } + public void Prepare() + { + throw new NotImplementedException(); + } + + public void Connect(string address, ushort port, uint protocol) + { + + } + + public void Disconnect() + { + + } + + public void Update() + { + + } + + public void Dispose() + { + + } +} \ No newline at end of file diff --git a/Ragon.Tests/Utilities/FakeClientNetworkChannel.cs b/Ragon.Tests/Utilities/FakeClientNetworkChannel.cs new file mode 100644 index 0000000..03e2e86 --- /dev/null +++ b/Ragon.Tests/Utilities/FakeClientNetworkChannel.cs @@ -0,0 +1,11 @@ +using Ragon.Client; + +namespace Ragon.Tests; + +public class FakeClientNetworkChannel: INetworkChannel +{ + public void Send(byte[] data) + { + + } +} \ No newline at end of file diff --git a/Ragon.Tests/Utilities/FakeNetwork.cs b/Ragon.Tests/Utilities/FakeNetwork.cs new file mode 100644 index 0000000..a8a288f --- /dev/null +++ b/Ragon.Tests/Utilities/FakeNetwork.cs @@ -0,0 +1,15 @@ + + +namespace Ragon.Tests; + +public class FakeNetwork +{ + public FakeClientNetwork ClientNetwork; + public FakeServerNetwork ServerNetwork; + + public FakeNetwork() + { + ClientNetwork = new FakeClientNetwork(); + ServerNetwork = new FakeServerNetwork(); + } +} \ No newline at end of file diff --git a/Ragon.Tests/Utilities/FakeServerNetwork.cs b/Ragon.Tests/Utilities/FakeServerNetwork.cs new file mode 100644 index 0000000..81b6d05 --- /dev/null +++ b/Ragon.Tests/Utilities/FakeServerNetwork.cs @@ -0,0 +1,28 @@ +using Ragon.Server.IO; + +namespace Ragon.Tests; + +public class FakeServerNetwork: INetworkServer +{ + public void Close() + { + + } + + public void Stop() + { + + } + + public void Update() + { + } + + public void Broadcast(byte[] data, NetworkChannel channel) + { + } + + public void Listen(INetworkListener listener, NetworkConfiguration configuration) + { + } +} \ No newline at end of file diff --git a/Ragon.Tests/Utilities/FakeServerNetworkChannel.cs b/Ragon.Tests/Utilities/FakeServerNetworkChannel.cs new file mode 100644 index 0000000..5ec7d4b --- /dev/null +++ b/Ragon.Tests/Utilities/FakeServerNetworkChannel.cs @@ -0,0 +1,18 @@ +using Ragon.Protocol; +using Ragon.Server.IO; + +namespace Ragon.Tests; + +public class FakeServerNetworkChannel: INetworkChannel +{ + public void Send(byte[] data) + { + + } + + public void Send(RagonBuffer buffer) + { + + } +} + diff --git a/Ragon.sln b/Ragon.sln index c62f46d..75bc476 100644 --- a/Ragon.sln +++ b/Ragon.sln @@ -12,7 +12,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Server.ENetServer", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Client", "Ragon.Client\Ragon.Client.csproj", "{C82D65BF-6D80-4263-ADFE-CB9ED990B6C3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Client.Property", "Ragon.Client.Property\Ragon.Client.Property.csproj", "{46A60DAB-F854-4BB6-A119-BD4C5B2B0D29}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ragon.Tests", "Ragon.Tests\Ragon.Tests.csproj", "{5833194D-CAD0-4517-BBC6-4DB0D7F1BEF1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -44,9 +44,9 @@ Global {C82D65BF-6D80-4263-ADFE-CB9ED990B6C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {C82D65BF-6D80-4263-ADFE-CB9ED990B6C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {C82D65BF-6D80-4263-ADFE-CB9ED990B6C3}.Release|Any CPU.Build.0 = Release|Any CPU - {46A60DAB-F854-4BB6-A119-BD4C5B2B0D29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {46A60DAB-F854-4BB6-A119-BD4C5B2B0D29}.Debug|Any CPU.Build.0 = Debug|Any CPU - {46A60DAB-F854-4BB6-A119-BD4C5B2B0D29}.Release|Any CPU.ActiveCfg = Release|Any CPU - {46A60DAB-F854-4BB6-A119-BD4C5B2B0D29}.Release|Any CPU.Build.0 = Release|Any CPU + {5833194D-CAD0-4517-BBC6-4DB0D7F1BEF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5833194D-CAD0-4517-BBC6-4DB0D7F1BEF1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5833194D-CAD0-4517-BBC6-4DB0D7F1BEF1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5833194D-CAD0-4517-BBC6-4DB0D7F1BEF1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal