Compare commits

...

12 Commits

Author SHA1 Message Date
edmand46 f55634877a 📝 update readme.md 2023-10-21 23:10:41 +03:00
edmand46 8b3a29a750 📝 update readme.md 2023-10-21 23:09:49 +03:00
edmand46 6a50bffe1e 📝 update readme.md 2023-10-21 23:03:55 +03:00
edmand46 9144ad58ef 📝 update readme.md 2023-10-21 23:03:37 +03:00
edmand46 ee9f3fbe3a 🐛 crash on abnormal disconnecting from websocket server 2023-10-19 20:20:40 +03:00
edmand46 893c73512a feat: allow control replication via code, fix null owner on OnEntityCreated 2023-10-16 23:37:35 +03:00
edmand46 e7dd693147 🐛 Event Payload 2023-10-14 23:50:25 +03:00
edmand46 fef1007c8c 🔖 update version 2023-10-14 21:48:33 +03:00
edmand46 e33c442a18 🐛 WebSocket buffer size 2023-10-14 21:35:51 +03:00
edmand46 09b185a3ad 🐛 checking size event 2023-10-13 12:10:46 +03:00
edmand46 7d154ea4d4 🎨 cleanup 2023-10-13 11:02:56 +03:00
edmand46 d2577e5d1f remove allocation on raw data replication 2023-10-13 10:58:15 +03:00
23 changed files with 171 additions and 99 deletions
+25
View File
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
+1
View File
@@ -1,6 +1,7 @@
.DS_Store
.idea
.vs
.vscode
obj
bin
*.user
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

+1 -1
View File
@@ -1,5 +1,5 @@
<p align="center">
<img src="Images/ragon-logo.png" width="200" >
<img src="Images/logo.png">
</p>
## Ragon Server
+62 -43
View File
@@ -19,20 +19,49 @@ using Ragon.Protocol;
namespace Ragon.Client
{
public sealed class RagonEntity: IDisposable
public sealed class RagonEntity : IDisposable
{
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks.Remove(_callback);
_localCallbacks.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private RagonClient _client;
public ushort Id { get; private set; }
public ushort Type { get; private set; }
public bool Replication { get; private set; }
public RagonAuthority Authority { get; private set; }
public RagonPlayer Owner { get; private set; }
public RagonEntityState State { get; private set; }
public bool IsStatic => SceneId > 0;
public bool IsReplicated { get; private set; }
public bool IsAttached { get; private set; }
public bool HasAuthority { get; private set; }
@@ -53,39 +82,33 @@ namespace Ragon.Client
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
public RagonEntity(ushort type = 0, ushort sceneId = 0)
public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
{
State = new RagonEntityState(this);
Type = type;
IsReplicated = replicated;
_spawnPayload = new RagonPayload(0);
_destroyPayload = new RagonPayload(0);
_sceneId = sceneId;
}
internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner)
internal void Attach()
{
Type = entityType;
Id = entityId;
Owner = owner;
IsAttached = true;
Replication = true;
HasAuthority = hasAuthority;
_client = client;
Attached?.Invoke(this);
}
internal void SetReplication(bool enabled)
public void SetReplication(bool enabled)
{
Replication = enabled;
IsReplicated = enabled;
}
internal void Detach(RagonPayload payload)
{
_destroyPayload = payload;
Detached?.Invoke();
}
@@ -93,20 +116,27 @@ namespace Ragon.Client
{
var payload = new T();
if (data.Size <= 0) return payload;
var buffer = new RagonBuffer();
data.Write(buffer);
payload.Deserialize(buffer);
return payload;
}
public void AttachPayload(RagonPayload payload)
public void Prepare(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer player, RagonPayload payload)
{
Type = entityType;
Id = entityId;
HasAuthority = hasAuthority;
_client = client;
_spawnPayload = payload;
}
Owner = player;
}
public T GetAttachPayload<T>() where T : IRagonPayload, new()
{
@@ -166,6 +196,7 @@ namespace Ragon.Client
listener.Invoke(_client.Room.Local, evnt);
return;
}
if (replicationMode == RagonReplicationMode.LocalAndServer)
{
var localListeners = _localListeners[eventCode];
@@ -175,7 +206,7 @@ namespace Ragon.Client
}
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id);
@@ -189,19 +220,18 @@ namespace Ragon.Client
_client.Reliable.Send(sendData);
}
public Action<RagonPlayer, IRagonEvent> OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
if (!_listeners.TryGetValue(eventCode, out var callbacks))
{
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_listeners.Add(eventCode, callbacks);
}
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
{
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
@@ -222,19 +252,7 @@ namespace Ragon.Client
});
}
return action;
}
public void OffEvent<TEvent>(Action<RagonPlayer, IRagonEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
if (_listeners.TryGetValue(eventCode, out var callbacks))
callbacks.Remove(callback);
if (_localListeners.TryGetValue(eventCode, out var localCallbacks))
localCallbacks.Remove(callback);
return new EventSubscription(callbacks, localCallbacks, action);
}
internal void Write(RagonBuffer buffer)
@@ -245,8 +263,7 @@ namespace Ragon.Client
_propertiesChanged = false;
}
internal void Read(RagonBuffer buffer)
{
State.ReadState(buffer);
@@ -254,6 +271,8 @@ namespace Ragon.Client
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{
if (!IsReplicated) return;
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
@@ -21,6 +21,8 @@ namespace Ragon.Client;
public class RagonPayload
{
public static RagonPayload Empty = new RagonPayload(0);
private readonly uint[] _data = new uint[128];
private readonly int _size = 0;
@@ -57,11 +57,11 @@ internal class EntityCreateHandler : IHandler
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
entity.AttachPayload(payload);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
if (hasCreated)
_entityListener.OnEntityCreated(entity);
entity.Attach(_client, entityId, entityType, hasAuthority, player);
entity.Attach();
}
}
@@ -27,7 +27,7 @@ internal class SnapshotHandler : IHandler
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
public SnapshotHandler(
RagonClient ragonClient,
RagonListenerList listenerList,
@@ -76,19 +76,19 @@ internal class SnapshotHandler : IHandler
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
var payload = RagonPayload.Empty;
if (payloadSize > 0)
{
var payload = new RagonPayload(payloadSize);
payload = new RagonPayload(payloadSize);
payload.Read(buffer);
entity.AttachPayload(payload);
}
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
_entityListener.OnEntityCreated(entity);
entity.Read(buffer);
entity.Attach(_client, entityId, entityType, hasAuthority, player);
entity.Attach();
}
var staticEntities = buffer.ReadUShort();
@@ -111,8 +111,10 @@ internal class SnapshotHandler : IHandler
var hasAuthority = _playerCache.Local.Id == player.Id;
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
entity.Read(buffer);
entity.Attach(_client, entityId, entityType, hasAuthority, player);
entity.Attach();
}
if (_client.Status == RagonStatus.LOBBY)
+1 -2
View File
@@ -47,8 +47,7 @@ namespace Ragon.Client
public RagonEntityCache Entity => _entityCache;
public NetworkStatistics Statistics => _stats;
public RagonRoom Room => _room;
internal RagonBuffer Buffer => _writeBuffer;
internal INetworkChannel Reliable => _connection.Reliable;
internal INetworkChannel Unreliable => _connection.Unreliable;
+1 -1
View File
@@ -116,7 +116,7 @@ public sealed class RagonEntityCache
foreach (var ent in _entityList)
{
if (!ent.IsAttached ||
!ent.Replication ||
!ent.IsReplicated ||
!ent.PropertiesChanged) continue;
ent.Write(buffer);
-1
View File
@@ -6,7 +6,6 @@
<LangVersion>8</LangVersion>
<RootNamespace>Ragon.Common</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.2.4-rc</Version>
<Title>Ragon.Protocol</Title>
<Copyright>Eduard Kargin</Copyright>
<PackageProjectUrl>https://ragon-server.com</PackageProjectUrl>
@@ -47,22 +47,32 @@ public class WebSocketServer : INetworkServer
{
while (!cancellationToken.IsCancellationRequested)
{
var context = await _httpListener.GetContextAsync();
if (!context.Request.IsWebSocketRequest)
WebSocketConnection connection = null!;
try
{
context.Response.StatusCode = 200;
context.Response.ContentLength64 = 0;
context.Response.Close();
continue;
var context = await _httpListener.GetContextAsync();
if (!context.Request.IsWebSocketRequest)
{
context.Response.StatusCode = 200;
context.Response.ContentLength64 = 0;
context.Response.Close();
continue;
}
var webSocketContext = await context.AcceptWebSocketAsync(null);
var webSocket = webSocketContext.WebSocket;
var peerId = _sequencer.Pop();
connection = new WebSocketConnection(webSocket, peerId);
}
var webSocketContext = await context.AcceptWebSocketAsync(null);
var webSocket = webSocketContext.WebSocket;
var peerId = _sequencer.Pop();
var connection = new WebSocketConnection(webSocket, peerId);
_connections[peerId] = connection;
catch (Exception ex)
{
_logger.Warn(ex);
continue;
}
_connections[connection.Id] = connection;
StartListen(connection, cancellationToken);
}
}
@@ -75,6 +85,7 @@ public class WebSocketServer : INetworkServer
var webSocket = connection.Socket;
var bytes = new byte[2048];
var buffer = new Memory<byte>(bytes);
while (
webSocket.State == WebSocketState.Open ||
!cancellationToken.IsCancellationRequested)
@@ -84,7 +95,7 @@ public class WebSocketServer : INetworkServer
var result = await webSocket.ReceiveAsync(buffer, cancellationToken);
if (result.Count > 0)
{
var payload = buffer.Slice(0, buffer.Length);
var payload = buffer.Slice(0, result.Count);
_networkListener.OnData(connection, NetworkChannel.RELIABLE, payload.ToArray());
}
}
@@ -96,6 +107,7 @@ public class WebSocketServer : INetworkServer
_sequencer.Push(connection.Id);
_activeConnections.Remove(connection);
_networkListener.OnDisconnected(connection);
}
+1 -1
View File
@@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<RootNamespace>Ragon.Core</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.2.4-rc</Version>
<Version>1.3.1</Version>
<Title>Ragon.Server</Title>
<Copyright>Eduard Kargin</Copyright>
<PackageProjectUrl>https://ragon-server.com</PackageProjectUrl>
+1 -1
View File
@@ -45,7 +45,7 @@ public class RagonEvent
public void Write(RagonBuffer buffer)
{
if (_size == 0) return;
if (_size <= 0) return;
buffer.WriteArray(_data, _size);
}
}
@@ -30,21 +30,20 @@ public sealed class RoomDataOperation : BaseOperation
{
var player = context.RoomPlayer;
var room = context.Room;
var data = Reader.RawData;
Writer.Clear();
Writer.WriteOperation(RagonOperation.REPLICATE_RAW_DATA);
Writer.WriteUShort(player.Connection.Id);
var playerData = Writer.ToArray();
var payloadData = data;
var size = playerData.Length + payloadData.Length;
var dataSize = data.Length - 1;
var headerSize = 3;
var size = headerSize + dataSize;
var sendData = new byte[size];
Array.Copy(playerData, 0, sendData, 0, playerData.Length);
Array.Copy(payloadData, 1, sendData, playerData.Length, payloadData.Length - 1);
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);
}
}
+3
View File
@@ -14,6 +14,8 @@
* limitations under the License.
*/
using Ragon.Protocol;
using Ragon.Server.Handler;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
@@ -21,6 +23,7 @@ namespace Ragon.Server;
public interface IRagonServer
{
BaseOperation ResolveHandler(RagonOperation operation);
RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection);
RagonLobbyPlayer? GetPlayerById(string id);
}
@@ -28,10 +28,10 @@ public class RagonWebHookPlugin
{
private Dictionary<string, string> _webHooks;
private RagonServer _server;
private IRagonServer _server;
private HttpClient _httpClient;
public RagonWebHookPlugin(RagonServer server, RagonServerConfiguration configuration)
public RagonWebHookPlugin(IRagonServer server, RagonServerConfiguration configuration)
{
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
_httpClient = new HttpClient();
@@ -46,7 +46,7 @@ public class RagonWebHookPlugin
var executor = context.Executor;
executor.Run(async () =>
{
var authorizationOperation = (AuthorizationOperation) _server.ResolveOperation(RagonOperation.AUTHORIZE);
var authorizationOperation = (AuthorizationOperation) _server.ResolveHandler(RagonOperation.AUTHORIZE);
var response = await _httpClient.PostAsync(new Uri(value), httpContent);
if (response.StatusCode != HttpStatusCode.OK)
{
+1 -1
View File
@@ -223,7 +223,7 @@ public class RagonServer : IRagonServer, INetworkListener
_server.Broadcast(sendData, NetworkChannel.UNRELIABLE);
}
public BaseOperation ResolveOperation(RagonOperation operation)
public BaseOperation ResolveHandler(RagonOperation operation)
{
return _handlers[(byte)operation];
}
@@ -25,11 +25,6 @@ public enum ServerType
WEBSOCKET,
}
public class WebHook
{
}
[Serializable]
public struct RagonServerConfiguration
{
@@ -47,7 +42,7 @@ public struct RagonServerConfiguration
public Dictionary<string, string> WebHooks;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly string ServerVersion = "1.2.9-rc";
private static readonly string ServerVersion = "1.3.1";
private static Dictionary<string, ServerType> _serverTypes = new Dictionary<string, ServerType>()
{
{"enet", Server.ServerType.ENET},
@@ -127,5 +127,10 @@ namespace Ragon.Client
if (_libraryLoaded)
Library.Deinitialize();
}
public void Close()
{
}
}
}
@@ -128,5 +128,10 @@ namespace Ragon.Client
if (_libraryLoaded)
Library.Deinitialize();
}
public void Close()
{
}
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
using Ragon.Client.Property;
using Ragon.Protocol;
namespace Ragon.Client.Simulation;
+7
View File
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.0",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}