From 3fc64b3ff16de05a5bf32424e36985787ff469f2 Mon Sep 17 00:00:00 2001 From: Leonardo Emanuele Date: Mon, 8 Apr 2024 11:05:23 +0200 Subject: [PATCH] fix missing parameters in the event system --- .../EventSystem/ClientGateway.cs | 14 +++- src/FxEvents.Server/EventDispatcher.cs | 2 + .../EventSystem/ServerGateway.cs | 8 +++ .../EventSubsystem/BaseGateway.cs | 64 +++++++++++-------- 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/FxEvents.Client/EventSystem/ClientGateway.cs b/src/FxEvents.Client/EventSystem/ClientGateway.cs index 9dd854b..b5ccedb 100644 --- a/src/FxEvents.Client/EventSystem/ClientGateway.cs +++ b/src/FxEvents.Client/EventSystem/ClientGateway.cs @@ -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; @@ -23,6 +24,7 @@ public ClientGateway() DelayDelegate = async delay => await BaseScript.Delay(delay); PrepareDelegate = PrepareAsync; PushDelegate = Push; + PushDelegateLatent = PushLatent; } internal void AddEvents() @@ -35,7 +37,8 @@ internal void AddEvents() } catch (Exception ex) { - Logger.Error("InboundPipeline:" + ex.ToString()); + EventMessage message = encrypted.DecryptObject(EventDispatcher.EncryptionKey); + Logger.Error($"InboundPipeline [{message.Endpoint}]:" + ex.ToString()); } })); @@ -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); diff --git a/src/FxEvents.Server/EventDispatcher.cs b/src/FxEvents.Server/EventDispatcher.cs index 884ec97..1e9a4df 100644 --- a/src/FxEvents.Server/EventDispatcher.cs +++ b/src/FxEvents.Server/EventDispatcher.cs @@ -216,6 +216,7 @@ public static Task Get(Player player, string endpoint, params object[] arg } return Events.Get(Convert.ToInt32(player.Handle), endpoint, args); } + public static Task Get(ISource client, string endpoint, params object[] args) { if (!Initialized) @@ -225,6 +226,7 @@ public static Task Get(ISource client, string endpoint, params object[] ar } return Events.Get(client.Handle, endpoint, args); } + public static void Mount(string endpoint, Delegate @delegate) { if (!Initialized) diff --git a/src/FxEvents.Server/EventSystem/ServerGateway.cs b/src/FxEvents.Server/EventSystem/ServerGateway.cs index 0024d3d..5ca5774 100644 --- a/src/FxEvents.Server/EventSystem/ServerGateway.cs +++ b/src/FxEvents.Server/EventSystem/ServerGateway.cs @@ -26,6 +26,7 @@ public ServerGateway() Serialization = new MsgPackSerialization(); DelayDelegate = async delay => await BaseScript.Delay(delay); PushDelegate = Push; + PushDelegateLatent = PushLatent; _signatures = new(); } @@ -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) { diff --git a/src/FxEvents.Shared/EventSubsystem/BaseGateway.cs b/src/FxEvents.Shared/EventSubsystem/BaseGateway.cs index 851af7e..5024415 100644 --- a/src/FxEvents.Shared/EventSubsystem/BaseGateway.cs +++ b/src/FxEvents.Shared/EventSubsystem/BaseGateway.cs @@ -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()); } @@ -224,42 +228,52 @@ public void ProcessOutbound(EventResponseMessage response) protected async Task SendInternal(EventFlowType flow, int source, string endpoint, params object[] args) { - StopwatchUtil stopwatch = StopwatchUtil.StartNew(); - List parameters = []; - - for (int idx = 0; idx < args.Length; idx++) + try { - object argument = args[idx]; - Type type = argument.GetType(); + StopwatchUtil stopwatch = StopwatchUtil.StartNew(); + List 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()); + return message; } - return message; } protected async Task SendInternalLatent(EventFlowType flow, int source, string endpoint, int bytePerSecond, params object[] args)