diff --git a/Ragon.Client/Sources/Handler/PlayerDataHandler.cs b/Ragon.Client/Sources/Handler/PlayerDataHandler.cs new file mode 100644 index 0000000..8031337 --- /dev/null +++ b/Ragon.Client/Sources/Handler/PlayerDataHandler.cs @@ -0,0 +1,13 @@ +using Ragon.Protocol; + +namespace Ragon.Client +{ + + public class PlayerDataHandler: IHandler + { + public void Handle(RagonBuffer reader) + { + + } + } +} \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/RawDataHandler.cs b/Ragon.Client/Sources/Handler/RawDataHandler.cs new file mode 100644 index 0000000..93188e1 --- /dev/null +++ b/Ragon.Client/Sources/Handler/RawDataHandler.cs @@ -0,0 +1,56 @@ +/* + * Copyright 2023 Eduard Kargin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +using Ragon.Protocol; + +namespace Ragon.Client; + +internal class RawDataHandler: IHandler +{ + private readonly RagonListenerList _listeners; + private readonly RagonPlayerCache _playerCache; + + public RawDataHandler( + RagonPlayerCache playerCache, + RagonListenerList listeners) + { + _playerCache = playerCache; + _listeners = listeners; + } + + public void Handle(RagonBuffer reader) + { + var rawData = reader.RawData; + var peerId = (ushort)(rawData[1] + (rawData[2] << 8)); + var player = _playerCache.GetPlayerByPeer(peerId); + + if (player == null) + { + RagonLog.Error($"Player with peerId:{peerId} not found"); + + _playerCache.Dump(); + return; + } + + var headerSize = 3; + var payload = new byte[rawData.Length - headerSize]; + + Array.Copy(rawData, headerSize, payload, 0, payload.Length); + + _listeners.OnData(player, payload); + } +} \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/RoomDataHandler.cs b/Ragon.Client/Sources/Handler/RoomDataHandler.cs index 21eb0c7..8aadbef 100644 --- a/Ragon.Client/Sources/Handler/RoomDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomDataHandler.cs @@ -1,56 +1,12 @@ -/* - * Copyright 2023 Eduard Kargin - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - using Ragon.Protocol; -namespace Ragon.Client; - -internal class RoomDataHandler: IHandler +namespace Ragon.Client { - private readonly RagonListenerList _listeners; - private readonly RagonPlayerCache _playerCache; - - public RoomDataHandler( - RagonPlayerCache playerCache, - RagonListenerList listeners) + public class RoomDataHandler: IHandler { - _playerCache = playerCache; - _listeners = listeners; - } - - public void Handle(RagonBuffer reader) - { - var rawData = reader.RawData; - var peerId = (ushort)(rawData[1] + (rawData[2] << 8)); - var player = _playerCache.GetPlayerByPeer(peerId); - - if (player == null) + public void Handle(RagonBuffer reader) { - RagonLog.Error($"Player with peerId:{peerId} not found"); - _playerCache.Dump(); - return; } - - var headerSize = 3; - var payload = new byte[rawData.Length - headerSize]; - - Array.Copy(rawData, headerSize, payload, 0, payload.Length); - - _listeners.OnData(player, payload); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index 8144981..4ec10bd 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -121,7 +121,7 @@ namespace Ragon.Client _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.REPLICATE_RAW_DATA] = new RawDataHandler(_playerCache, _listeners); _handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners); var protocolRaw = RagonVersion.Parse(protocol); diff --git a/Ragon.Server/Sources/Data/RagonData.cs b/Ragon.Server/Sources/Data/RagonData.cs index cc726e9..c81a350 100644 --- a/Ragon.Server/Sources/Data/RagonData.cs +++ b/Ragon.Server/Sources/Data/RagonData.cs @@ -4,7 +4,7 @@ namespace Ragon.Server.Data; public class RagonData { - private byte[] _data = Array.Empty(); + private byte[] _data; public bool IsDirty { get; set; } public byte[] Data { diff --git a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs index 14fa24d..791944f 100644 --- a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs +++ b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs @@ -86,7 +86,6 @@ public sealed class AuthorizationOperation: BaseOperation var playerId = context.LobbyPlayer.Id; var playerName = context.LobbyPlayer.Name; - var playerPayload = context.LobbyPlayer.Payload; _writer.Clear(); diff --git a/Ragon.Server/Sources/Handler/PlayerDataOperation.cs b/Ragon.Server/Sources/Handler/PlayerDataOperation.cs index e28c0b7..b9229b6 100644 --- a/Ragon.Server/Sources/Handler/PlayerDataOperation.cs +++ b/Ragon.Server/Sources/Handler/PlayerDataOperation.cs @@ -5,7 +5,6 @@ using Ragon.Server.Lobby; namespace Ragon.Server.Handler { - public class PlayerDataOperation : BaseOperation { private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); @@ -24,20 +23,8 @@ namespace Ragon.Server.Handler var playerDataLen = Reader.ReadUShort(); var playerData = Reader.ReadBytes(playerDataLen); - - var roomPlayer = context.RoomPlayer; - if (roomPlayer != null) - { - roomPlayer.UserData.Data = playerData; - return; - } - - var lobbyPlayer = context.RoomPlayer; - if (lobbyPlayer != null) - { - lobbyPlayer.UserData.Data = playerData; - } - + + context.UserData.Data = playerData; } } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/RoomDataOperation.cs b/Ragon.Server/Sources/Handler/RoomDataOperation.cs index 9d35986..4efc960 100644 --- a/Ragon.Server/Sources/Handler/RoomDataOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomDataOperation.cs @@ -14,7 +14,6 @@ * limitations under the License. */ -using NLog; using Ragon.Protocol; using Ragon.Server.IO; @@ -28,22 +27,11 @@ public sealed class RoomDataOperation : BaseOperation public override void Handle(RagonContext context, NetworkChannel channel) { - var player = context.RoomPlayer; + var playerDataLen = Reader.ReadUShort(); + var playerData = Reader.ReadBytes(playerDataLen); + var room = context.Room; - - var data = Reader.RawData; - var dataSize = data.Length - 1; - var headerSize = 3; - var size = headerSize + dataSize; - var sendData = new byte[size]; - var peerId = player.Connection.Id; - - sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA; - sendData[1] = (byte)peerId; - sendData[2] = (byte)(peerId >> 8); - - Array.Copy(data, 1, sendData, headerSize, dataSize); - - room.Broadcast(sendData, channel); + if (room != null) + room.UserData.Data = playerData; } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Lobby/RagonLobbyPlayer.cs b/Ragon.Server/Sources/Lobby/RagonLobbyPlayer.cs index e2ff1a0..60aea4b 100644 --- a/Ragon.Server/Sources/Lobby/RagonLobbyPlayer.cs +++ b/Ragon.Server/Sources/Lobby/RagonLobbyPlayer.cs @@ -31,14 +31,13 @@ public class RagonLobbyPlayer public INetworkConnection Connection { get; } public string Id { get; private set; } public string Name { get; private set; } + public string Payload { get; private set; } - public RagonData UserData { get; private set; } - - public RagonLobbyPlayer(INetworkConnection connection, string id, string name, byte[] payload) + public RagonLobbyPlayer(INetworkConnection connection, string id, string name, string payload) { Id = id; Name = name; Connection = connection; - UserData = new RagonData(payload); + Payload = payload; } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs index 9c23c2e..d6c88fc 100644 --- a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs +++ b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs @@ -59,8 +59,7 @@ public class RagonWebHookPlugin var authorizationResponse = JsonConvert.DeserializeObject(content); if (authorizationResponse != null) { - var bytes = Encoding.UTF8.GetBytes(authorizationResponse.Payload); - var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, bytes); + var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload); context.SetPlayer(lobbyPlayer); authorizationOperation.Approve(context); diff --git a/Ragon.Server/Sources/RagonContext.cs b/Ragon.Server/Sources/RagonContext.cs index 7badabf..3559002 100644 --- a/Ragon.Server/Sources/RagonContext.cs +++ b/Ragon.Server/Sources/RagonContext.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using Ragon.Server.Data; using Ragon.Server.IO; using Ragon.Server.Lobby; using Ragon.Server.Time; @@ -29,10 +30,11 @@ public class RagonContext public int LimitBufferedEvents { get; private set; } public IRagonLobby Lobby { get; private set; } public RagonLobbyPlayer? LobbyPlayer { get; private set; } - public RagonRoom? Room { get; private set; } public RagonRoomPlayer? RoomPlayer { get; private set; } + public RagonData UserData { get; private set; } + public RagonScheduler Scheduler { get; private set; } public RagonContext( @@ -48,6 +50,7 @@ public class RagonContext Executor = executor; Lobby = lobby; Scheduler = scheduler; + UserData = new RagonData(Array.Empty()); } internal void SetPlayer(RagonLobbyPlayer player) diff --git a/Ragon.Server/Sources/RagonServer.cs b/Ragon.Server/Sources/RagonServer.cs index 671db1f..f387552 100644 --- a/Ragon.Server/Sources/RagonServer.cs +++ b/Ragon.Server/Sources/RagonServer.cs @@ -74,6 +74,7 @@ public class RagonServer : IRagonServer, INetworkListener var contextObserver = new RagonContextObserver(_contextsByPlayerId); _scheduler.Run(new RagonActionTimer(SendRoomList, 1.0f)); + _scheduler.Run(new RagonActionTimer(SendUserData, 0.2f)); _serverPlugin.OnAttached(this); @@ -246,6 +247,23 @@ public class RagonServer : IRagonServer, INetworkListener } } + public void SendUserData() + { + foreach (var (_, value) in _contextsByPlayerId) + { + if (value.UserData.IsDirty) + { + _writer.Clear(); + _writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED); + _writer.WriteUShort(value.Connection.Id); + _writer.WriteBytes(value.UserData.Data); + + var sendData = _writer.ToArray(); + _server.Broadcast(sendData, NetworkChannel.RELIABLE); + } + } + } + public BaseOperation ResolveHandler(RagonOperation operation) { return _handlers[(byte)operation]; diff --git a/Ragon.Server/Sources/Room/RagonRoom.cs b/Ragon.Server/Sources/Room/RagonRoom.cs index 8edfb2c..ce66157 100644 --- a/Ragon.Server/Sources/Room/RagonRoom.cs +++ b/Ragon.Server/Sources/Room/RagonRoom.cs @@ -15,6 +15,7 @@ */ using Ragon.Protocol; +using Ragon.Server.Data; using Ragon.Server.Entity; using Ragon.Server.IO; using Ragon.Server.Plugin; @@ -29,7 +30,8 @@ public class RagonRoom : IRagonRoom, IRagonAction public int PlayerMax { get; private set; } public int PlayerMin { get; private set; } public int PlayerCount => WaitPlayersList.Count; - + + public RagonData UserData { get; set; } public RagonRoomPlayer Owner { get; private set; } public RagonBuffer Writer { get; } public IRoomPlugin Plugin { get; private set; } diff --git a/Ragon.Server/Sources/Room/RagonRoomPlayer.cs b/Ragon.Server/Sources/Room/RagonRoomPlayer.cs index b69ec89..054d4c9 100644 --- a/Ragon.Server/Sources/Room/RagonRoomPlayer.cs +++ b/Ragon.Server/Sources/Room/RagonRoomPlayer.cs @@ -30,15 +30,12 @@ public class RagonRoomPlayer public RagonRoom Room { get; private set; } public RagonEntityCache Entities { get; private set; } - public RagonData UserData { get; private set; } - public RagonRoomPlayer(INetworkConnection connection, string id, string name) { Id = id; Name = name; Connection = connection; Entities = new RagonEntityCache(); - UserData = new RagonData(Array.Empty()); } public void AttachEntity(RagonEntity entity)