Example #1
0
        private async void InvokeDelegate(RPCPacket packet)
        {
            var path = packet.Url.SubRightWith('/', out string action);

            if (mDelegateHandlers.TryGetValue(action, out DelegateHandler handler))
            {
                try
                {
                    packet.LoadParameters(handler.Parameters);
                    object result = handler.Delegate.DynamicInvoke(packet.Data);
                    if (!handler.IsVoid)
                    {
                        await(Task) result;
                        if (handler.TargetReturnType != null)
                        {
                            var data = handler.GetValue(result);
                            packet.Reply(data);
                        }
                        else
                        {
                            packet.ReplySuccess();
                        }
                    }
                }
                catch (Exception e_)
                {
                    if (!handler.IsVoid)
                    {
                        packet.ReplyError((short)StatusCode.INNER_ERROR, $"{action} delegate invoke error {e_.Message}!");
                    }
                    if (EnableLog(LogType.Error))
                    {
                        Log(LogType.Error, $"{action} delegate invoke error {e_.Message}@{e_.StackTrace}");
                    }
                }
            }
            else
            {
                if (packet.NeedReply)
                {
                    packet.ReplyError((short)StatusCode.ACTION_NOT_FOUND, $"{action} delegate not found!");
                }
            }
        }