Skip to content

Commit

Permalink
⚡ remove allocation on raw data replication
Browse files Browse the repository at this point in the history
  • Loading branch information
edmand46 committed Oct 13, 2023
1 parent e25f42f commit d2577e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
46 changes: 28 additions & 18 deletions Ragon.Client/Sources/Entity/RagonEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,31 @@

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);
}
}

private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);

private RagonClient _client;
Expand Down Expand Up @@ -189,19 +212,18 @@ public void ReplicateEvent<TEvent>(
_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>>();
Expand All @@ -222,19 +244,7 @@ public void ReplicateEvent<TEvent>(
});
}

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)
Expand Down
25 changes: 12 additions & 13 deletions Ragon.Server/Sources/Handler/RoomDataOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@ public override void Handle(RagonContext context, NetworkChannel channel)
{
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);
}
}

0 comments on commit d2577e5

Please sign in to comment.