Skip to content

Commit

Permalink
fix missing parameters in the event system
Browse files Browse the repository at this point in the history
  • Loading branch information
manups4e committed Apr 8, 2024
1 parent e1fe208 commit 3fc64b3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
14 changes: 11 additions & 3 deletions src/FxEvents.Client/EventSystem/ClientGateway.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FxEvents.Shared.Diagnostics;
using FxEvents.Shared;
using FxEvents.Shared.Diagnostics;
using FxEvents.Shared.EventSubsystem;
using FxEvents.Shared.Message;
using FxEvents.Shared.Serialization;
Expand All @@ -23,6 +24,7 @@ public ClientGateway()
DelayDelegate = async delay => await BaseScript.Delay(delay);
PrepareDelegate = PrepareAsync;
PushDelegate = Push;
PushDelegateLatent = PushLatent;
}

internal void AddEvents()
Expand All @@ -35,7 +37,8 @@ internal void AddEvents()
}
catch (Exception ex)
{
Logger.Error("InboundPipeline:" + ex.ToString());
EventMessage message = encrypted.DecryptObject<EventMessage>(EventDispatcher.EncryptionKey);
Logger.Error($"InboundPipeline [{message.Endpoint}]:" + ex.ToString());
}
}));

Expand Down Expand Up @@ -76,11 +79,16 @@ public void Push(string pipeline, int source, byte[] buffer)
BaseScript.TriggerServerEvent(pipeline, buffer);
}

public void PushLatent(string pipeline, int source, int bytePerSecond, byte[] buffer)
{
if (source != -1) throw new Exception($"The client can only target server events. (arg {nameof(source)} is not matching -1)");
BaseScript.TriggerLatentServerEvent(pipeline, bytePerSecond, buffer);
}

public async void Send(string endpoint, params object[] args)
{
await SendInternal(EventFlowType.Straight, new ServerId().Handle, endpoint, args);
}

public async void SendLatent(string endpoint, int bytePerSecond, params object[] args)
{
await SendInternalLatent(EventFlowType.Straight, new ServerId().Handle, endpoint, bytePerSecond, args);
Expand Down
2 changes: 2 additions & 0 deletions src/FxEvents.Server/EventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static Task<T> Get<T>(Player player, string endpoint, params object[] arg
}
return Events.Get<T>(Convert.ToInt32(player.Handle), endpoint, args);
}

public static Task<T> Get<T>(ISource client, string endpoint, params object[] args)
{
if (!Initialized)
Expand All @@ -225,6 +226,7 @@ public static Task<T> Get<T>(ISource client, string endpoint, params object[] ar
}
return Events.Get<T>(client.Handle, endpoint, args);
}

public static void Mount(string endpoint, Delegate @delegate)
{
if (!Initialized)
Expand Down
8 changes: 8 additions & 0 deletions src/FxEvents.Server/EventSystem/ServerGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ServerGateway()
Serialization = new MsgPackSerialization();
DelayDelegate = async delay => await BaseScript.Delay(delay);
PushDelegate = Push;
PushDelegateLatent = PushLatent;
_signatures = new();
}

Expand All @@ -44,6 +45,13 @@ public void Push(string pipeline, int source, byte[] buffer)
BaseScript.TriggerClientEvent(pipeline, buffer);
}

public void PushLatent(string pipeline, int source, int bytePerSecond, byte[] buffer)
{
if (source != new ServerId().Handle)
BaseScript.TriggerLatentClientEvent(_eventDispatcher.GetPlayers[source], pipeline, bytePerSecond, buffer);
else
BaseScript.TriggerLatentClientEvent(pipeline, bytePerSecond, buffer);
}

private void GetSignature([FromSource] string source)
{
Expand Down
64 changes: 39 additions & 25 deletions src/FxEvents.Shared/EventSubsystem/BaseGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ object CallInternalDelegate()
using SerializationContext context = new SerializationContext(message.Endpoint, $"(Process) Parameter Index {idx}",
Serialization, parameter.Data);

holder.Add(context.Deserialize(type));
object a = context.Deserialize(type);
holder.Add(a);

//Debug.WriteLine($"inbound {message.Endpoint} - index: {idx}, type:{type.FullName}, value:{a.ToJson()}");
}

parameters.AddRange(holder.ToArray());

//Debug.WriteLine(parameters.ToArray().ToJson());
return @delegate.DynamicInvoke(parameters.ToArray());
}

Expand Down Expand Up @@ -224,42 +228,52 @@ public void ProcessOutbound(EventResponseMessage response)

protected async Task<EventMessage> SendInternal(EventFlowType flow, int source, string endpoint, params object[] args)
{
StopwatchUtil stopwatch = StopwatchUtil.StartNew();
List<EventParameter> parameters = [];

for (int idx = 0; idx < args.Length; idx++)
try
{
object argument = args[idx];
Type type = argument.GetType();
StopwatchUtil stopwatch = StopwatchUtil.StartNew();
List<EventParameter> parameters = [];

using SerializationContext context = new(endpoint, $"(Send) Parameter Index '{idx}'", Serialization);
for (int idx = 0; idx < args.Length; idx++)
{
object argument = args[idx];
Type type = argument.GetType();
//Debug.WriteLine($"outbound {endpoint} - index: {idx}, type:{type.FullName}, value:{argument.ToJson()}");

context.Serialize(type, argument);
parameters.Add(new EventParameter(context.GetData()));
}
using SerializationContext context = new(endpoint, $"(Send) Parameter Index '{idx}'", Serialization);

EventMessage message = new(endpoint, flow, parameters);
context.Serialize(type, argument);
parameters.Add(new EventParameter(context.GetData()));
}

if (PrepareDelegate != null)
{
stopwatch.Stop();
EventMessage message = new(endpoint, flow, parameters);

await PrepareDelegate(InboundPipeline, source, message);
stopwatch.Start();
}
if (PrepareDelegate != null)
{
stopwatch.Stop();

byte[] data = message.EncryptObject(EventDispatcher.EncryptionKey);
await PrepareDelegate(InboundPipeline, source, message);
stopwatch.Start();
}

PushDelegate(InboundPipeline, source, data);
if (EventDispatcher.Debug)
{
byte[] data = message.EncryptObject(EventDispatcher.EncryptionKey);

PushDelegate(InboundPipeline, source, data);
if (EventDispatcher.Debug)
{
#if CLIENT
Logger.Debug($"[{endpoint} {flow}] Sent {data.Length} byte(s) to {(source == -1 ? "Server" : API.GetPlayerName(source))} in {stopwatch.Elapsed.TotalMilliseconds}ms");
Logger.Debug($"[{endpoint} {flow}] Sent {data.Length} byte(s) to {(source == -1 ? "Server" : API.GetPlayerName(source))} in {stopwatch.Elapsed.TotalMilliseconds}ms");
#elif SERVER
Logger.Debug($"[{endpoint} {flow}] Sent {data.Length} byte(s) to {(source == -1 ? "Server" : API.GetPlayerName("" + source))} in {stopwatch.Elapsed.TotalMilliseconds}ms");
Logger.Debug($"[{endpoint} {flow}] Sent {data.Length} byte(s) to {(source == -1 ? "Server" : API.GetPlayerName("" + source))} in {stopwatch.Elapsed.TotalMilliseconds}ms");
#endif
}
return message;
}
catch (Exception ex)
{
Logger.Error($"{endpoint} - {ex.ToString()}");
EventMessage message = new(endpoint, flow, new List<EventParameter>());
return message;
}
return message;
}

protected async Task<EventMessage> SendInternalLatent(EventFlowType flow, int source, string endpoint, int bytePerSecond, params object[] args)
Expand Down

0 comments on commit 3fc64b3

Please sign in to comment.