This commit is contained in:
2025-01-18 17:30:03 +03:00
parent edf90b39c4
commit 2f919e4fd8
45 changed files with 1403 additions and 466 deletions
@@ -38,7 +38,7 @@ internal class AuthorizeSuccessHandler: IHandler
var playerName = reader.ReadString();
var playerPayload = reader.ReadString();
_client.SetStatus(RagonStatus.LOBBY);
_client.UpdateState(RagonState.LOBBY);
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
}
}
@@ -42,16 +42,15 @@ internal class JoinSuccessHandler : IHandler
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonClient _client;
private readonly RagonRoom _room;
public JoinSuccessHandler(
RagonClient client,
RagonListenerList listenerList,
RagonPlayerCache playerCache
RagonRoom room
)
{
_client = client;
_listenerList = listenerList;
_playerCache = playerCache;
_room = room;
}
public void Handle(RagonStream reader)
@@ -59,16 +58,13 @@ internal class JoinSuccessHandler : IHandler
var roomId = reader.ReadString();
var min = reader.ReadUShort();
var max = reader.ReadUShort();
var sceneName = reader.ReadString();
var localId = reader.ReadString();
var ownerId = reader.ReadString();
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
_playerCache.SetOwnerAndLocal(ownerId, localId);
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
var room = new RagonRoom(_client, _playerCache, roomInfo);
room.UserData.Read(reader);
_room.Reset(roomInfo);
_room.UserData.Read(reader);
var playersCount = reader.ReadUShort();
RagonLog.Trace("Players: " + playersCount);
@@ -85,8 +81,7 @@ internal class JoinSuccessHandler : IHandler
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
}
_client.AssignRoom(room);
_client.SetStatus(RagonStatus.ROOM);
_client.UpdateState(RagonState.ROOM);
_listenerList.OnJoined();
}
@@ -35,6 +35,7 @@ internal class LeaveRoomHandler : IHandler
public void Handle(RagonStream reader)
{
_listenerList.OnLeft();
_client.Room.Cleanup();
_client.Room.Clear();
}
}
@@ -1,60 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client;
internal class RoomDataHandler: IHandler
{
private readonly RagonListenerList _listeners;
private readonly RagonPlayerCache _playerCache;
public RoomDataHandler(
RagonPlayerCache playerCache,
RagonListenerList listeners)
{
_playerCache = playerCache;
_listeners = listeners;
}
public void Handle(RagonStream reader)
{
var rawData = reader.ReadBinary(reader.Lenght);
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
RagonPlayer player = null;
if (peerId != 10000)
{
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);
}
}
@@ -36,7 +36,7 @@ public class RoomEventHandler : IHandler
{
var eventCode = buffer.ReadUShort();
var peerId = buffer.ReadUShort();
var executionMode = (RagonReplicationMode)buffer.ReadByte();
var executionMode = (RagonReplicationMode) buffer.ReadByte();
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
@@ -16,6 +16,6 @@ public class TimestampHandler: IHandler
var timestamp1 = (uint)buffer.ReadInt();
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
_client.SetTimestamp(value.Double);
_client.UpdateTimestamp(value.Double);
}
}
-1
View File
@@ -20,6 +20,5 @@ namespace Ragon.Client
{
public interface IRagonEvent: IRagonSerializable
{
}
}
@@ -22,7 +22,6 @@ namespace Ragon.Client
IRagonFailedListener,
IRagonJoinListener,
IRagonLeftListener,
IRagonSceneListener,
IRagonOwnershipChangedListener,
IRagonPlayerJoinListener,
IRagonPlayerLeftListener,
@@ -1,22 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Ragon.Client;
public interface IRagonSceneListener
{
void OnSceneLoaded(RagonClient client);
}
@@ -1,6 +0,0 @@
namespace Ragon.Client;
public interface IRagonSceneRequestListener
{
void OnRequestScene(RagonClient client, string sceneName);
}
+20 -30
View File
@@ -31,7 +31,7 @@ namespace Ragon.Client
private RagonListenerList _listeners;
private RagonPlayerCache _playerCache;
private RagonEventCache _eventCache;
private RagonStatus _status;
private RagonState _state;
private double _serverTimestamp;
private float _replicationRate = 0;
@@ -39,7 +39,7 @@ namespace Ragon.Client
public double ServerTimestamp => _serverTimestamp;
public IRagonConnection Connection => _connection;
public RagonStatus Status => _status;
public RagonState State => _state;
public RagonSession Session => _session;
public RagonEventCache Event => _eventCache;
public NetworkStatistics Statistics => _stats;
@@ -64,22 +64,18 @@ namespace Ragon.Client
_replicationTime = 0;
_eventCache = new RagonEventCache();
_stats = new NetworkStatistics();
_status = RagonStatus.DISCONNECTED;
}
public void Connect(string address, ushort port, string protocol)
{
_writeBuffer = new RagonStream();
_readBuffer = new RagonStream();
_playerCache = new RagonPlayerCache();
_session = new RagonSession(this, _writeBuffer);
_room = new RagonRoom(this, _playerCache);
_stats = new NetworkStatistics();
_state = RagonState.DISCONNECTED;
_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);
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _room);
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache);
@@ -87,19 +83,22 @@ namespace Ragon.Client
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
_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);
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
}
public void Connect(string address, ushort port, string protocol)
{
var protocolRaw = RagonVersion.Parse(protocol);
_connection.Connect(address, port, protocolRaw);
}
public void Disconnect()
{
_status = RagonStatus.DISCONNECTED;
_room?.Cleanup();
_state = RagonState.DISCONNECTED;
_room.Clear();
_connection.Disconnect();
OnDisconnected(RagonDisconnect.MANUAL);
@@ -107,13 +106,12 @@ namespace Ragon.Client
public void Update(float dt)
{
if (_status != RagonStatus.DISCONNECTED)
if (_state != RagonState.DISCONNECTED)
{
_replicationTime += dt;
if (_replicationTime >= _replicationRate)
{
_replicationTime = 0;
// _entityCache.WriteState(_writeBuffer);
SendTimestamp();
SendRoomUserData();
@@ -129,9 +127,9 @@ namespace Ragon.Client
public void Dispose()
{
if (_status != RagonStatus.DISCONNECTED)
if (_state != RagonState.DISCONNECTED)
{
_status = RagonStatus.DISCONNECTED;
_state = RagonState.DISCONNECTED;
_connection.Disconnect();
}
@@ -147,7 +145,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(IRagonDataListener listener) => _listeners.Add(listener);
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
@@ -160,7 +157,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(IRagonDataListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonPlayerUserDataListener listener) => _listeners.Remove(listener);
@@ -169,18 +165,12 @@ namespace Ragon.Client
#region INTERNAL
internal void AssignRoom(RagonRoom room)
internal void UpdateState(RagonState state)
{
_room?.Dispose();
_room = room;
_state = state;
}
internal void SetStatus(RagonStatus status)
{
_status = status;
}
internal void SetTimestamp(double time)
internal void UpdateTimestamp(double time)
{
_serverTimestamp = time;
}
@@ -240,7 +230,7 @@ namespace Ragon.Client
RagonLog.Trace("Connected");
_listeners.OnConnected();
_status = RagonStatus.CONNECTED;
_state = RagonState.CONNECTED;
}
private void OnDisconnected(RagonDisconnect reason)
@@ -248,7 +238,7 @@ namespace Ragon.Client
RagonLog.Trace($"Disconnected: {reason}");
_listeners.OnDisconnected(reason);
_status = RagonStatus.DISCONNECTED;
_state = RagonState.DISCONNECTED;
}
private void OnData(byte[] data)
-17
View File
@@ -29,7 +29,6 @@ namespace Ragon.Client
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
private readonly List<IRagonDataListener> _dataListeners = new();
private readonly List<IRagonRoomListListener> _roomListListeners = new();
private readonly List<IRagonRoomUserDataListener> _roomUserDataListeners = new();
private readonly List<IRagonPlayerUserDataListener> _playerUserDataListeners = new();
@@ -79,11 +78,6 @@ namespace Ragon.Client
_delayedActions.Clear();
}
public void Add(IRagonDataListener dataListener)
{
_dataListeners.Add(dataListener);
}
public void Add(IRagonAuthorizationListener listener)
{
_authorizationListeners.Add(listener);
@@ -139,11 +133,6 @@ namespace Ragon.Client
_playerUserDataListeners.Add(listener);
}
public void Remove(IRagonDataListener listener)
{
_delayedActions.Add(() => _dataListeners.Remove(listener));
}
public void Remove(IRagonAuthorizationListener listener)
{
_delayedActions.Add(() => _authorizationListeners.Remove(listener));
@@ -259,12 +248,6 @@ namespace Ragon.Client
listener.OnDisconnected(_client, disconnect);
}
public void OnData(RagonPlayer player, byte[] data)
{
foreach (var listener in _dataListeners)
listener.OnData(_client, player, data);
}
public void OnRoomList(RagonRoomInformation[] roomInfos)
{
foreach (var listListener in _roomListListeners)
+13 -32
View File
@@ -18,7 +18,7 @@ using Ragon.Protocol;
namespace Ragon.Client
{
public class RagonRoom : IDisposable
public class RagonRoom
{
private class EventSubscription : IDisposable
{
@@ -51,13 +51,12 @@ namespace Ragon.Client
private readonly RagonClient _client;
private readonly RagonPlayerCache _playerCache;
private readonly RoomParameters _parameters;
private readonly RagonUserData _userData;
private RoomParameters _parameters;
private RagonUserData _userData;
public string Id => _parameters.RoomId;
public int MinPlayers => _parameters.Min;
public int MaxPlayers => _parameters.Max;
public string Scene => "none";
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
public RagonPlayer Local => _playerCache.Local;
@@ -70,26 +69,22 @@ namespace Ragon.Client
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
public RagonRoom(RagonClient client,
RagonPlayerCache playerCache,
RoomParameters parameters)
public RagonRoom(RagonClient client, RagonPlayerCache playerCache)
{
_client = client;
_parameters = parameters;
_playerCache = playerCache;
}
public void Reset(RoomParameters parameters)
{
Clear();
_userData = new RagonUserData();
}
internal void Cleanup()
{
_parameters = parameters;
_playerCache.Cleanup();
}
internal void Update(string sceneName)
{
// _scene.Update(sceneName);
}
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonStream buffer)
{
if (_events.TryGetValue(eventCode, out var evnt))
@@ -194,22 +189,8 @@ namespace Ragon.Client
_client.Reliable.Send(sendData);
}
public void ReplicateData(byte[] data, bool reliable)
public void Clear()
{
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()
{
Cleanup();
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
+8 -8
View File
@@ -21,6 +21,7 @@ namespace Ragon.Client
public class RagonSession
{
private readonly RagonClient _client;
private readonly RagonStream _buffer;
public RagonSession(RagonClient client, RagonStream buffer)
@@ -29,9 +30,9 @@ namespace Ragon.Client
_buffer = buffer;
}
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
public void CreateOrJoin(string sessionName, int minPlayers, int maxPlayers)
{
var parameters = new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
var parameters = new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers };
CreateOrJoin(parameters);
}
@@ -46,14 +47,14 @@ namespace Ragon.Client
_client.Reliable.Send(sendData);
}
public void Create(string sceneName, int minPlayers, int maxPlayers)
public void Create(string sessionName, int minPlayers, int maxPlayers)
{
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
Create(null, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers });
}
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
public void Create(string roomId, string sessionName, int minPlayers, int maxPlayers)
{
Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
Create(roomId, new RagonRoomParameters() { Min = minPlayers, Max = maxPlayers });
}
public void Create(string roomId, RagonRoomParameters parameters)
@@ -79,7 +80,7 @@ namespace Ragon.Client
public void Leave()
{
var sendData = new[] {(byte) RagonOperation.LEAVE_ROOM};
var sendData = new[] { (byte)RagonOperation.LEAVE_ROOM };
_client.Reliable.Send(sendData);
}
@@ -104,6 +105,5 @@ namespace Ragon.Client
var sendData = _buffer.ToArray();
_client.Reliable.Send(sendData);
}
}
}
@@ -16,7 +16,7 @@
namespace Ragon.Client
{
public enum RagonStatus
public enum RagonState
{
DISCONNECTED,
CONNECTED,
+2 -12
View File
@@ -17,7 +17,7 @@
namespace Ragon.Protocol
{
public enum RagonOperation: byte
public enum RagonOperation : byte
{
AUTHORIZE = 1,
AUTHORIZED_SUCCESS = 2,
@@ -26,23 +26,13 @@ namespace Ragon.Protocol
CREATE_ROOM = 5,
JOIN_ROOM = 6,
LEAVE_ROOM = 7,
OWNERSHIP_ENTITY_CHANGED = 8,
OWNERSHIP_ROOM_CHANGED= 9,
OWNERSHIP_ROOM_CHANGED = 9,
JOIN_SUCCESS = 10,
JOIN_FAILED = 11,
LOAD_SCENE = 12,
SCENE_LOADED = 13,
PLAYER_JOINED = 14,
PLAYER_LEAVED = 15,
CREATE_ENTITY = 16,
REMOVE_ENTITY = 17,
SNAPSHOT = 18,
REPLICATE_ENTITY_STATE = 19,
REPLICATE_ENTITY_EVENT = 20,
REPLICATE_RAW_DATA = 21,
REPLICATE_ROOM_EVENT = 22,
TRANSFER_ROOM_OWNERSHIP = 23,
TRANSFER_ENTITY_OWNERSHIP = 24,
TIMESTAMP_SYNCHRONIZATION = 25,
ROOM_LIST_UPDATED = 26,
PLAYER_DATA_UPDATED = 27,
@@ -19,20 +19,17 @@ namespace Ragon.Protocol
{
public class RagonRoomParameters
{
public string Scene { get; set; }
public int Min { get; set; }
public int Max { get; set; }
public void Serialize(RagonStream buffer)
{
buffer.WriteString(Scene);
buffer.WriteInt(Min);
buffer.WriteInt(Max);
}
public void Deserialize(RagonStream buffer)
{
Scene = buffer.ReadString();
Min = buffer.ReadInt();
Max = buffer.ReadInt();
}
+2
View File
@@ -28,6 +28,8 @@
<ItemGroup>
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
<PackageReference Include="Google.Protobuf" Version="3.29.0" />
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.2" />
</ItemGroup>
@@ -9,10 +9,11 @@ public class RelayRoomPlugin: BaseRoomPlugin
{
}
public void OnAttached()
{
Console.WriteLine("Room attached");
}
public void OnDetached()
@@ -1,10 +1,35 @@
using Ragon.Server;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin;
using Ragon.Server.Time;
namespace Ragon.Relay
{
public class RelayServerPlugin : BaseServerPlugin
{
private RelayConfiguration _relayConfiguration;
private RagonScheduler _scheduler;
private RagonConnectionRegistry _connectionRegistry;
private IRagonLobby _lobby;
private Reporter _reporter;
public RelayServerPlugin(RelayConfiguration config)
{
_relayConfiguration = config;
}
public override void OnAttached(IRagonServer server)
{
base.OnAttached(server);
_lobby = server.Lobby;
_connectionRegistry = server.ConnectionRegistry;
_scheduler = server.Scheduler;
_reporter = new Reporter(_relayConfiguration, server, "127.0.0.1", 5000);
server.Scheduler.Run(new RagonActionTimer(() => _reporter.Done(), 1, -1));
}
public override bool OnCommand(string command, string payload)
{
return true;
+44
View File
@@ -0,0 +1,44 @@
using System;
using System.Net;
using System.Net.Sockets;
using Ragon.Server.Logging;
namespace Ragon.Relay;
public class Client
{
private readonly UdpClient _udpClient;
private readonly IPEndPoint _endpoint;
private readonly IRagonLogger _logger;
public Client(string host, int port)
{
_logger = LoggerManager.GetLogger("Client");
_udpClient = new UdpClient();
_endpoint = new IPEndPoint(IPAddress.Parse(host), port);
}
public void Send(byte[] data)
{
try
{
_udpClient.BeginSend(data, data.Length, _endpoint, SendCallback, null);
}
catch (Exception ex)
{
_logger.Error(ex.Message);
}
}
private void SendCallback(IAsyncResult ar)
{
try
{
_udpClient.EndSend(ar);
}
catch (Exception ex)
{
_logger.Error(ex.Message);
}
}
}
File diff suppressed because it is too large Load Diff
+56
View File
@@ -0,0 +1,56 @@
using Google.Protobuf;
using Google.Protobuf.Collections;
using Ragon.Server;
namespace Ragon.Relay;
public class Reporter
{
private readonly Client _client;
private readonly IRagonServer _server;
private readonly RelayConfiguration _configuration;
public Reporter(
RelayConfiguration relayConfiguration,
IRagonServer server,
string host,
int port
)
{
_client = new Client(host, port);
_server = server;
_configuration = relayConfiguration;
}
public void Done()
{
for (var i = 0; i < 10; i++)
{
var message = new Data();
message.Statistics = new Statistics()
{
Connections = _server.ConnectionRegistry.Contexts.Count,
ConnectionsLimit = _configuration.LimitConnections,
Rooms = _server.Lobby.Rooms.Count,
RoomsLimit = _configuration.LimitRooms,
};
var room = new Room()
{
Id = $"Room ID {i}",
};
for (var j = 0; j < 10; j++)
{
room.Players.Add(new Player()
{
Id = $"Player ID {i}",
});
}
message.Room = room;
_client.Send(message.ToByteArray());
}
}
}
@@ -163,8 +163,6 @@ public class WebSocketServer : INetworkServer
_httpListener.Prefixes.Add($"http://{configuration.Address}:{configuration.Port}/");
_httpListener.Start();
// _executor.Run(() => StartAccept(_cancellationTokenSource.Token));
var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
_logger.Info($"Listen at http://{configuration.Address}:{configuration.Port}/");
_logger.Info($"Protocol: {protocolDecoded}");
@@ -76,7 +76,6 @@ namespace Ragon.Server.Handler
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
};
@@ -97,8 +96,7 @@ namespace Ragon.Server.Handler
context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
_logger.Trace(
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id}");
JoinSuccess(roomPlayer, room, Writer);
@@ -114,7 +112,6 @@ namespace Ragon.Server.Handler
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
@@ -1,52 +0,0 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
using Ragon.Server.IO;
namespace Ragon.Server.Handler;
public sealed class RoomDataOperation : BaseOperation
{
public RoomDataOperation(RagonStream reader, RagonStream writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var room = context.Room;
var data = Reader.ReadBinary(Reader.Lenght);
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);
var pluginData = new byte[dataSize];
Array.Copy(data, 1, pluginData, 0, dataSize);
room.Plugin.OnData(player, pluginData);
Array.Copy(data, 1, sendData, headerSize, dataSize);
room.Broadcast(sendData, room.ReadyPlayersList, NetworkChannel.RELIABLE);
}
}
@@ -61,7 +61,6 @@ public sealed class RoomJoinOperation : BaseOperation
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(context.RoomPlayer.Id);
writer.WriteString(room.Owner.Id);
@@ -55,23 +55,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
_roomParameters.Deserialize(Reader);
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
{
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
JoinSuccess(player, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
existsRoom.Plugin.OnPlayerJoined(player);
}
else
{
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
};
@@ -91,14 +76,12 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
context.Scheduler.Run(room);
context.SetRoom(room, roomPlayer);
_logger.Trace(
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id}");
JoinSuccess(roomPlayer, room, Writer);
room.Plugin.OnPlayerJoined(roomPlayer);
}
}
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonStream writer)
{
@@ -107,7 +90,6 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
@@ -18,14 +18,15 @@ namespace Ragon.Server;
public class RagonContextObserver
{
private Dictionary<string, RagonContext> _contexts;
public RagonContextObserver(Dictionary<string, RagonContext> contexts)
private readonly RagonConnectionRegistry _registry;
public RagonContextObserver(RagonConnectionRegistry registry)
{
_contexts = contexts;
_registry = registry;
}
public void OnAuthorized(RagonContext context)
{
_contexts.Add(context.LobbyPlayer.Id, context);
_registry.Add(context.LobbyPlayer.Id, context);
}
}
+4 -3
View File
@@ -16,14 +16,15 @@
using Ragon.Protocol;
using Ragon.Server.Handler;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Time;
namespace Ragon.Server;
public interface IRagonServer
{
BaseOperation ResolveHandler(RagonOperation operation);
public RagonContext? GetContextByConnectionId(ushort peerId);
public RagonContext? GetContextById(string playerId);
public RagonConnectionRegistry ConnectionRegistry { get; }
public RagonScheduler Scheduler { get; }
public IRagonLobby Lobby { get; }
}
@@ -23,7 +23,6 @@ public interface IRagonLobby
{
public IReadOnlyList<IRagonRoom> Rooms { get; }
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room);
public void Persist(RagonRoom room);
public bool RemoveIfEmpty(RagonRoom room);
}
@@ -24,7 +24,6 @@ public class RagonLobbyDispatcher
var room = rooms[i];
writer.WriteString(room.Id);
writer.WriteString(room.Scene);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerCount);
@@ -50,34 +50,6 @@ public class LobbyInMemory : IRagonLobby
return false;
}
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room)
{
foreach (var existsRoom in _rooms)
{
if (existsRoom.Scene == sceneName)
{
if (existsRoom.PlayerCount >= existsRoom.PlayerMax)
{
_logger.Warning($"Room with scene {sceneName} fulfilled");
room = default;
return false;
}
room = existsRoom;
return true;
}
}
room = default;
return false;
}
public bool FindRoomByProperties(Dictionary<string, object> props)
{
return true;
}
public void Persist(RagonRoom room)
{
room.Attach();
@@ -86,7 +58,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}");
_logger.Trace($"Room: {r.Id} ID: {r.Id} Players: {r.Players.Count}");
}
public bool RemoveIfEmpty(RagonRoom room)
@@ -103,7 +75,7 @@ public class LobbyInMemory : IRagonLobby
}
foreach (var r in _rooms)
_logger.Trace($"Room: {r.Id} Scene: {r.Scene} Players: {r.Players.Count}");
_logger.Trace($"Room: {r.Id} ID: {r.Id} Players: {r.Players.Count}");
return result;
}
@@ -16,7 +16,6 @@
using Ragon.Protocol;
using Ragon.Server.Handler;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Room;
@@ -42,7 +41,7 @@ namespace Ragon.Server.Plugin
public void Approve(string id, string name, string payload)
{
var ctx = _server.GetContextByConnectionId(PeerID);
var ctx = _server.ConnectionRegistry.GetContextByConnectionId(PeerID);
if (ctx == null)
return;
@@ -52,7 +51,7 @@ namespace Ragon.Server.Plugin
public void Reject()
{
var ctx = _server.GetContextByConnectionId(PeerID);
var ctx = _server.ConnectionRegistry.GetContextByConnectionId(PeerID);
if (ctx == null)
return;
@@ -80,7 +79,9 @@ namespace Ragon.Server.Plugin
{
public IRagonServer Server { get; protected set; }
public virtual void OnAttached(IRagonServer server)
public virtual void OnAttached(
IRagonServer server
)
{
Server = server;
}
@@ -16,6 +16,7 @@
using Ragon.Server.Lobby;
using Ragon.Server.Room;
using Ragon.Server.Time;
namespace Ragon.Server.Plugin
{
@@ -0,0 +1,60 @@
namespace Ragon.Server;
public class RagonConnectionRegistry
{
private readonly Dictionary<ushort, RagonContext> _contextsByConnection = new();
private readonly Dictionary<string, RagonContext> _contextsByPlayerId = new();
private readonly List<RagonContext> _contexts = new();
private readonly List<RagonContext> _playerContexts = new();
public void Add(string playerId, RagonContext context)
{
_contextsByPlayerId.Add(playerId, context);
_playerContexts.Add(context);
}
public void Add(ushort connectionId, RagonContext context)
{
_contextsByConnection.Add(connectionId, context);
_contexts.Add(context);
}
public bool Remove(ushort connectionId, out RagonContext o)
{
if (_contextsByConnection.Remove(connectionId, out var context))
{
_contexts.Remove(context);
o = context;
return true;
}
o = null;
return false;
}
public bool Remove(string playerId)
{
if (_contextsByPlayerId.Remove(playerId, out var context))
{
_playerContexts.Remove(context);
return true;
}
return false;
}
public bool TryGetValue(ushort connectionId, out RagonContext o)
{
return _contextsByConnection.TryGetValue(connectionId, out o);
}
public IReadOnlyList<RagonContext> Contexts => _contexts;
public IReadOnlyList<RagonContext> PlayerContexts => _playerContexts;
public RagonContext? GetContextByConnectionId(ushort peerId) => _contextsByConnection.GetValueOrDefault(peerId);
public RagonContext? GetContextById(string playerId) => _contextsByPlayerId.GetValueOrDefault(playerId);
}
+20 -29
View File
@@ -38,8 +38,7 @@ public class RagonServer : IRagonServer, INetworkListener
private readonly RagonStream _reader;
private readonly RagonStream _writer;
private readonly RagonScheduler _scheduler;
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
private readonly RagonConnectionRegistry _connectionRegistry;
private readonly Stopwatch _timer;
private readonly RagonLobbyDispatcher _lobbySerializer;
private readonly long _tickRate = 0;
@@ -55,8 +54,7 @@ public class RagonServer : IRagonServer, INetworkListener
_server = server;
_configuration = configuration;
_serverPlugin = plugin;
_contextsByConnection = new Dictionary<ushort, RagonContext>();
_contextsByPlayerId = new Dictionary<string, RagonContext>();
_connectionRegistry = new RagonConnectionRegistry();
_lobby = new LobbyInMemory();
_lobbySerializer = new RagonLobbyDispatcher(_lobby);
_scheduler = new RagonScheduler();
@@ -65,13 +63,11 @@ public class RagonServer : IRagonServer, INetworkListener
_tickRate = 1000 / _configuration.ServerTickRate;
_timer = new Stopwatch();
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
var contextObserver = new RagonContextObserver(_connectionRegistry);
_scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f));
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f));
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f));
_serverPlugin.OnAttached(this);
_handlers = new BaseOperation[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, this, _serverPlugin, contextObserver, configuration);
_handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _configuration);
@@ -80,7 +76,6 @@ public class RagonServer : IRagonServer, INetworkListener
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_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);
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
}
@@ -133,12 +128,12 @@ public class RagonServer : IRagonServer, INetworkListener
var context = new RagonContext(connection, _lobby, _scheduler, _configuration.LimitBufferedEvents);
_logger.Trace($"Connected: {connection.Id}");
_contextsByConnection.Add(connection.Id, context);
_connectionRegistry.Add(connection.Id, context);
}
public void OnDisconnected(INetworkConnection connection)
{
if (_contextsByConnection.Remove(connection.Id, out var context))
if (_connectionRegistry.Remove(connection.Id, out var context))
{
var room = context.Room;
if (room != null)
@@ -149,7 +144,7 @@ public class RagonServer : IRagonServer, INetworkListener
}
if (context.ConnectionStatus == ConnectionStatus.Authorized)
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_connectionRegistry.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Disconnected: {connection.Id}");
}
@@ -161,7 +156,7 @@ public class RagonServer : IRagonServer, INetworkListener
public void OnTimeout(INetworkConnection connection)
{
if (_contextsByConnection.Remove(connection.Id, out var context) && context.ConnectionStatus == ConnectionStatus.Authorized)
if (_connectionRegistry.Remove(connection.Id, out var context) && context.ConnectionStatus == ConnectionStatus.Authorized)
{
var room = context.Room;
if (room != null)
@@ -171,7 +166,7 @@ public class RagonServer : IRagonServer, INetworkListener
}
if (context.ConnectionStatus == ConnectionStatus.Authorized)
_contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_connectionRegistry.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
}
@@ -185,7 +180,7 @@ public class RagonServer : IRagonServer, INetworkListener
{
try
{
if (_contextsByConnection.TryGetValue(connection.Id, out var context))
if (_connectionRegistry.TryGetValue(connection.Id, out var context))
{
_writer.Clear();
_reader.Clear();
@@ -223,24 +218,24 @@ public class RagonServer : IRagonServer, INetworkListener
_lobbySerializer.Write(_writer);
var sendData = _writer.ToArray();
foreach (var (_, value) in _contextsByPlayerId)
foreach (var ctx in _connectionRegistry.Contexts)
{
if (value.Room == null) // If only in lobby, then send room list data
value.Connection.Reliable.Send(sendData);
if (ctx.Room == null) // If only in lobby, then send room list data
ctx.Connection.Reliable.Send(sendData);
}
}
public void SendPlayerUserData()
{
foreach (var (_, value) in _contextsByPlayerId)
foreach (var playerContext in _connectionRegistry.PlayerContexts)
{
if (value.UserData.IsDirty)
if (playerContext.UserData.IsDirty)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
_writer.WriteUShort(value.Connection.Id);
_writer.WriteUShort(playerContext.Connection.Id);
value.UserData.Write(_writer);
playerContext.UserData.Write(_writer);
var sendData = _writer.ToArray();
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
@@ -270,15 +265,11 @@ public class RagonServer : IRagonServer, INetworkListener
return _handlers[(byte)operation];
}
public RagonContext? GetContextByConnectionId(ushort peerId)
{
return _contextsByConnection.TryGetValue(peerId, out var context) ? context : null;
}
public RagonConnectionRegistry ConnectionRegistry => _connectionRegistry;
public RagonContext? GetContextById(string playerId)
{
return _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null;
}
public RagonScheduler Scheduler => _scheduler;
public IRagonLobby Lobby => _lobby;
private void CopyrightInfo()
{
+1 -6
View File
@@ -22,17 +22,12 @@ namespace Ragon.Server.Room;
public interface IRagonRoom
{
public string Id { get; }
public string Scene { get; }
public int PlayerMin { get; }
public int PlayerMax { get; }
public int PlayerCount { get; }
public RagonData UserData { get; }
public void ReplicateData(byte[] data, NetworkChannel channel);
public void ReplicateData(byte[] data, List<RagonRoomPlayer> player, NetworkChannel channel);
public void ReplicateData(RagonRoomPlayer invoker, byte[] data, List<RagonRoomPlayer> receivers,
NetworkChannel channel = NetworkChannel.RELIABLE);
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
RagonRoomPlayer GetPlayerById(string id);
IReadOnlyList<RagonRoomPlayer> GetPlayers();
}
+1 -54
View File
@@ -26,10 +26,10 @@ namespace Ragon.Server.Room;
public class RagonRoom : IRagonRoom, IRagonAction
{
public string Id { get; private set; }
public string Scene { get; private set; }
public int PlayerMax { get; private set; }
public int PlayerMin { get; private set; }
public int PlayerCount => WaitPlayersList.Count;
public IReadOnlyList<RagonRoomPlayer> GetPlayers() => PlayerList;
public bool IsDone { get; private set; }
@@ -49,7 +49,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
{
Id = roomId;
Scene = info.Scene;
PlayerMax = info.Max;
PlayerMin = info.Min;
Plugin = roomPlugin;
@@ -66,8 +65,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
Writer = new RagonStream();
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer)
{
foreach (var evnt in _bufferedEvents)
@@ -163,46 +160,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
}
}
}
public void ReplicateData(byte[] data, NetworkChannel channel)
{
ReplicateData(data, ReadyPlayersList, channel);
}
public void ReplicateData(byte[] data, List<RagonRoomPlayer> receivers,
NetworkChannel channel = NetworkChannel.RELIABLE)
{
var dataSize = data.Length;
var headerSize = 3;
var size = headerSize + dataSize;
var sendData = new byte[size];
var peerId = 10000; // Server Peer
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
sendData[1] = (byte)peerId;
sendData[2] = (byte)(peerId >> 8);
Array.Copy(data, 0, sendData, headerSize, dataSize);
Broadcast(sendData, receivers, channel);
}
public void ReplicateData(RagonRoomPlayer invoker, byte[] data, List<RagonRoomPlayer> receivers,
NetworkChannel channel = NetworkChannel.RELIABLE)
{
var dataSize = data.Length;
var headerSize = 3;
var size = headerSize + dataSize;
var sendData = new byte[size];
var peerId = invoker.Connection.Id;
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
sendData[1] = (byte)peerId;
sendData[2] = (byte)(peerId >> 8);
Array.Copy(data, 0, sendData, headerSize, dataSize);
Broadcast(sendData, receivers, channel);
}
public void Tick(float dt)
{
@@ -247,16 +204,6 @@ public class RagonRoom : IRagonRoom, IRagonAction
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
}
public void UpdateMap(string sceneName)
{
Scene = sceneName;
foreach (var player in PlayerList)
player.UnsetReady();
UpdateReadyPlayerList();
}
public void Broadcast(byte[] data, NetworkChannel channel = NetworkChannel.RELIABLE)
{
if (channel == NetworkChannel.RELIABLE)
@@ -18,7 +18,6 @@ namespace Ragon.Server;
public ref struct RoomInformation
{
public string Scene;
public int Min;
public int Max;
public int BufferedEventsLimit;