Compare commits

...

4 Commits

Author SHA1 Message Date
edmand46 5136f08dab feat: config for size of property
fix: websocket server now can receive large messages
fix: buffer resize on read array
2024-08-17 10:29:27 +03:00
edmand46 f7719e1bca chore: update server version 2024-08-15 23:27:53 +03:00
edmand46 211b24fe2b feat: added server address in relay.config.json 2024-08-15 23:20:23 +03:00
edmand46 bdf7d4f94a fix: connection key is invalid 2024-07-21 09:46:01 +03:00
12 changed files with 76 additions and 41 deletions
+3
View File
@@ -331,6 +331,9 @@ namespace Ragon.Protocol
var limit = (size + 32 - 1) / 32; var limit = (size + 32 - 1) / 32;
var capacity = size; var capacity = size;
if (index + limit >= _buckets.Length)
Resize(size);
for (int i = 0; i < limit; i++) for (int i = 0; i < limit; i++)
{ {
var dataSize = capacity > 32 ? 32 : capacity; var dataSize = capacity > 32 ? 32 : capacity;
+6 -3
View File
@@ -56,13 +56,16 @@ namespace Ragon.Relay
var serverConfiguration = new RagonServerConfiguration() var serverConfiguration = new RagonServerConfiguration()
{ {
LimitConnections = configuration.LimitConnections, LimitConnections = configuration.LimitConnections,
LimitRooms = configuration.LimitConnections, LimitRooms = configuration.LimitRooms,
LimitBufferedEvents = configuration.LimitConnections, LimitBufferedEvents = configuration.LimitBufferedEvents,
LimitPlayersPerRoom = configuration.LimitConnections, LimitPlayersPerRoom = configuration.LimitPlayersPerRoom,
LimitUserDataSize = configuration.LimitUserDataSize,
LimitPropertySize = configuration.LimitPropertySize,
Port = configuration.Port, Port = configuration.Port,
Protocol = configuration.Protocol, Protocol = configuration.Protocol,
ServerKey = configuration.ServerKey, ServerKey = configuration.ServerKey,
ServerTickRate = configuration.ServerTickRate, ServerTickRate = configuration.ServerTickRate,
ServerAddress = configuration.ServerAddress,
}; };
var relay = new RagonServer(networkServer, plugin, serverConfiguration); var relay = new RagonServer(networkServer, plugin, serverConfiguration);
+3 -1
View File
@@ -7,6 +7,7 @@ namespace Ragon.Relay
{ {
public string ServerKey; public string ServerKey;
public string ServerType; public string ServerType;
public string ServerAddress;
public ushort ServerTickRate; public ushort ServerTickRate;
public string Protocol; public string Protocol;
public ushort Port; public ushort Port;
@@ -14,6 +15,7 @@ namespace Ragon.Relay
public int LimitPlayersPerRoom; public int LimitPlayersPerRoom;
public int LimitRooms; public int LimitRooms;
public int LimitBufferedEvents; public int LimitBufferedEvents;
public int LimitUserData; public int LimitUserDataSize;
public int LimitPropertySize;
} }
} }
+4 -2
View File
@@ -1,12 +1,14 @@
{ {
"serverKey": "defaultkey", "serverKey": "defaultkey",
"serverType": "enet", "serverType": "enet",
"serverAddress": "*",
"serverTickRate": 30, "serverTickRate": 30,
"protocol": "1.0.0", "protocol": "1.0.0",
"port": 5000, "port": 8000,
"limitConnections": 4095, "limitConnections": 4095,
"limitPlayersPerRoom": 20, "limitPlayersPerRoom": 20,
"limitRooms": 200, "limitRooms": 200,
"limitBufferedEvents": 50, "limitBufferedEvents": 50,
"limitUserData": 1024 "limitUserDataSize": 1024,
"limitPropertySize": 512
} }
@@ -49,7 +49,7 @@ namespace Ragon.Server.ENetServer
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024); _host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
var protocolDecoded = RagonVersion.Parse(_protocol); var protocolDecoded = RagonVersion.Parse(_protocol);
_logger.Info($"Listen at 127.0.0.1:{configuration.Port}"); _logger.Info($"Listen at {configuration.Address}:{configuration.Port}");
_logger.Info($"Protocol: {protocolDecoded}"); _logger.Info($"Protocol: {protocolDecoded}");
} }
@@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
using System.Buffers;
using System.Net; using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using Ragon.Protocol; using Ragon.Protocol;
@@ -82,20 +83,30 @@ public class WebSocketServer : INetworkServer
_networkListener.OnConnected(connection); _networkListener.OnConnected(connection);
var webSocket = connection.Socket; var webSocket = connection.Socket;
var bytes = new byte[2048]; var rawData = new byte[2048];
var buffer = new Memory<byte>(bytes); var rawDataBuffer = new Memory<byte>(rawData);
var data = new ArrayBufferWriter<byte>();
while ( while (webSocket.State == WebSocketState.Open || !cancellationToken.IsCancellationRequested)
webSocket.State == WebSocketState.Open ||
!cancellationToken.IsCancellationRequested)
{ {
try try
{ {
var result = await webSocket.ReceiveAsync(buffer, cancellationToken); while (true)
if (result.Count > 0)
{ {
var payload = buffer.Slice(0, result.Count); var result = await webSocket.ReceiveAsync(rawDataBuffer, cancellationToken);
_networkListener.OnData(connection, NetworkChannel.RELIABLE, payload.ToArray()); var payload = rawDataBuffer.Slice(0, result.Count);
data.Write(payload.Span);
if (result.EndOfMessage)
break;
}
if (data.WrittenCount > 0)
{
_networkListener.OnData(connection, NetworkChannel.RELIABLE, data.WrittenMemory.ToArray());
data.Clear();
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -146,13 +157,13 @@ public class WebSocketServer : INetworkServer
_connections = new WebSocketConnection[configuration.LimitConnections]; _connections = new WebSocketConnection[configuration.LimitConnections];
_httpListener = new HttpListener(); _httpListener = new HttpListener();
_httpListener.Prefixes.Add($"http://+:{configuration.Port}/"); _httpListener.Prefixes.Add($"http://{configuration.Address}:{configuration.Port}/");
_httpListener.Start(); _httpListener.Start();
_executor.Run(() => StartAccept(_cancellationTokenSource.Token)); _executor.Run(() => StartAccept(_cancellationTokenSource.Token));
var protocolDecoded = RagonVersion.Parse(configuration.Protocol); var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
_logger.Info($"Listen at http://0.0.0.0:{configuration.Port}/"); _logger.Info($"Listen at http://{configuration.Address}:{configuration.Port}/");
_logger.Info($"Protocol: {protocolDecoded}"); _logger.Info($"Protocol: {protocolDecoded}");
} }
+2 -2
View File
@@ -27,13 +27,13 @@ public class RagonProperty : RagonPayload
private uint[] _data; private uint[] _data;
public RagonProperty(int size, bool isFixed) public RagonProperty(int size, bool isFixed, int limit)
{ {
Size = size; Size = size;
IsFixed = isFixed; IsFixed = isFixed;
IsDirty = false; IsDirty = false;
_data = new uint[128]; _data = new uint[limit / 4 + 1];
} }
public void Read(RagonBuffer buffer) public void Read(RagonBuffer buffer)
@@ -75,6 +75,8 @@ namespace Ragon.Server.Handler
} }
else else
{ {
_logger.Warning($"Invalid key for connection {context.Connection.Id}");
Reject(context); Reject(context);
} }
} }
@@ -100,7 +102,7 @@ namespace Ragon.Server.Handler
var sendData = _writer.ToArray(); var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized"); _logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name}");
} }
public void Reject(RagonContext context) public void Reject(RagonContext context)
@@ -113,7 +115,7 @@ namespace Ragon.Server.Handler
context.Connection.Reliable.Send(sendData); context.Connection.Reliable.Send(sendData);
context.Connection.Close(); context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}"); _logger.Trace($"Rejected Connectin:{context.Connection.Id}");
} }
} }
} }
@@ -24,9 +24,12 @@ namespace Ragon.Server.Handler;
public sealed class EntityCreateOperation : BaseOperation public sealed class EntityCreateOperation : BaseOperation
{ {
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityCreateOperation)); private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityCreateOperation));
private RagonServerConfiguration _configuration;
public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration configuration) :
base(reader, writer)
{ {
_configuration = configuration;
} }
public override void Handle(RagonContext context, NetworkChannel channel) public override void Handle(RagonContext context, NetworkChannel channel)
@@ -53,7 +56,7 @@ public sealed class EntityCreateOperation : BaseOperation
var propertyType = Reader.ReadBool(); var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort(); var propertySize = Reader.ReadUShort();
entity.AddProperty(new RagonProperty(propertySize, propertyType)); entity.AddProperty(new RagonProperty(propertySize, propertyType, _configuration.LimitPropertySize));
} }
if (Reader.Capacity > 0) if (Reader.Capacity > 0)
@@ -27,9 +27,11 @@ namespace Ragon.Server.Handler
public sealed class SceneLoadedOperation : BaseOperation public sealed class SceneLoadedOperation : BaseOperation
{ {
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadedOperation)); private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadedOperation));
private RagonServerConfiguration _configuration;
public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration serverConfiguration) : base(reader, writer)
{ {
_configuration = serverConfiguration;
} }
public override void Handle(RagonContext context, NetworkChannel channel) public override void Handle(RagonContext context, NetworkChannel channel)
@@ -40,6 +42,7 @@ namespace Ragon.Server.Handler
var owner = context.Room.Owner; var owner = context.Room.Owner;
var player = context.RoomPlayer; var player = context.RoomPlayer;
var room = context.Room; var room = context.Room;
if (player.IsLoaded) if (player.IsLoaded)
{ {
_logger.Warning($"Player {player.Name}:{player.Connection.Id} already ready"); _logger.Warning($"Player {player.Name}:{player.Connection.Id} already ready");
@@ -70,10 +73,11 @@ namespace Ragon.Server.Handler
{ {
var propertyType = Reader.ReadBool(); var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort(); var propertySize = Reader.ReadUShort();
entity.AddProperty(new RagonProperty(propertySize, propertyType)); entity.AddProperty(new RagonProperty(propertySize, propertyType, _configuration.LimitPropertySize));
} }
var roomPlugin = room.Plugin; var roomPlugin = room.Plugin;
if (!roomPlugin.OnEntityCreate(player, entity)) continue; if (!roomPlugin.OnEntityCreate(player, entity)) continue;
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}"; var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
+11 -8
View File
@@ -27,7 +27,7 @@ namespace Ragon.Server;
public class RagonServer : IRagonServer, INetworkListener public class RagonServer : IRagonServer, INetworkListener
{ {
private const string ServerVersion = "1.4.0"; private const string ServerVersion = "1.4.1";
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RagonServer)); private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RagonServer));
private readonly INetworkServer _server; private readonly INetworkServer _server;
@@ -66,7 +66,6 @@ public class RagonServer : IRagonServer, INetworkListener
_timer = new Stopwatch(); _timer = new Stopwatch();
var contextObserver = new RagonContextObserver(_contextsByPlayerId); var contextObserver = new RagonContextObserver(_contextsByPlayerId);
_scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f)); _scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f));
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f)); _scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f));
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f)); _scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f));
@@ -80,8 +79,8 @@ public class RagonServer : IRagonServer, INetworkListener
_handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer); _handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer); _handlers[(byte)RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_reader, _writer);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadOperation(_reader, _writer); _handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadOperation(_reader, _writer);
_handlers[(byte)RagonOperation.SCENE_LOADED] = new SceneLoadedOperation(_reader, _writer); _handlers[(byte)RagonOperation.SCENE_LOADED] = new SceneLoadedOperation(_reader, _writer, _configuration);
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateOperation(_reader, _writer); _handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateOperation(_reader, _writer, _configuration);
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityDestroyOperation(_reader, _writer); _handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityDestroyOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventOperation(_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.REPLICATE_ENTITY_STATE] = new EntityStateOperation(_reader, _writer);
@@ -90,8 +89,8 @@ public class RagonServer : IRagonServer, INetworkListener
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_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_ROOM_EVENT] = new RoomEventOperation(_reader, _writer);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer); _handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer);
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserData); _handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserData); _handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataOperation(_reader, _writer, _configuration.LimitUserDataSize);
} }
public void Tick() public void Tick()
{ {
@@ -119,7 +118,7 @@ public class RagonServer : IRagonServer, INetworkListener
{ {
LimitConnections = _configuration.LimitConnections, LimitConnections = _configuration.LimitConnections,
Protocol = RagonVersion.Parse(_configuration.Protocol), Protocol = RagonVersion.Parse(_configuration.Protocol),
Address = "0.0.0.0", Address = _configuration.ServerAddress,
Port = _configuration.Port, Port = _configuration.Port,
}; };
@@ -156,6 +155,8 @@ public class RagonServer : IRagonServer, INetworkListener
_lobby.RemoveIfEmpty(room); _lobby.RemoveIfEmpty(room);
} }
if (context.ConnectionStatus == ConnectionStatus.Authorized)
_contextsByPlayerId.Remove(context.LobbyPlayer.Id); _contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Disconnected: {connection.Id}"); _logger.Trace($"Disconnected: {connection.Id}");
@@ -176,6 +177,8 @@ public class RagonServer : IRagonServer, INetworkListener
room.DetachPlayer(context.RoomPlayer); room.DetachPlayer(context.RoomPlayer);
_lobby.RemoveIfEmpty(room); _lobby.RemoveIfEmpty(room);
} }
if (context.ConnectionStatus == ConnectionStatus.Authorized)
_contextsByPlayerId.Remove(context.LobbyPlayer.Id); _contextsByPlayerId.Remove(context.LobbyPlayer.Id);
_logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}"); _logger.Trace($"Timeout: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
@@ -287,7 +290,7 @@ public class RagonServer : IRagonServer, INetworkListener
private void CopyrightInfo() private void CopyrightInfo()
{ {
_logger.Info($"Ragon Server Version: {ServerVersion}"); _logger.Info($"Server Version: {ServerVersion}");
_logger.Info($"Machine Name: {Environment.MachineName}"); _logger.Info($"Machine Name: {Environment.MachineName}");
_logger.Info($"OS: {Environment.OSVersion}"); _logger.Info($"OS: {Environment.OSVersion}");
_logger.Info($"Processors: {Environment.ProcessorCount}"); _logger.Info($"Processors: {Environment.ProcessorCount}");
@@ -26,6 +26,7 @@ public enum ServerType
public struct RagonServerConfiguration public struct RagonServerConfiguration
{ {
public string ServerKey; public string ServerKey;
public string ServerAddress;
public ushort ServerTickRate; public ushort ServerTickRate;
public string Protocol; public string Protocol;
public ushort Port; public ushort Port;
@@ -33,7 +34,8 @@ public struct RagonServerConfiguration
public int LimitPlayersPerRoom; public int LimitPlayersPerRoom;
public int LimitRooms; public int LimitRooms;
public int LimitBufferedEvents; public int LimitBufferedEvents;
public int LimitUserData; public int LimitUserDataSize;
public int LimitPropertySize;
private static Dictionary<string, ServerType> _serverTypes = new Dictionary<string, ServerType>() private static Dictionary<string, ServerType> _serverTypes = new Dictionary<string, ServerType>()
{ {