示例#1
0
        public override Task <ISlarkProtocol> FindAsync(SlarkContext context)
        {
            if (context.Message is TMJsonRequest request)
            {
                if (request != null)
                {
                    var methods = context.Server.GetType().GetMethods();

                    foreach (var method in methods)
                    {
                        var requestAttributes = method.GetCustomAttributes(typeof(RequestAttribute), false);
                        if (requestAttributes.Length == 1)
                        {
                            var functionAttribute = (RequestAttribute)requestAttributes[0];
                            if (functionAttribute.Match(request))
                            {
                                RpcFunctionDelegate del = method.IsStatic ? (RpcFunctionDelegate)Delegate.CreateDelegate(typeof(RpcFunctionDelegate), method) : (RpcFunctionDelegate)Delegate.CreateDelegate(typeof(RpcFunctionDelegate), context.Server, method);
                                return(Task.FromResult(new StandardRpcFunctionHandler(del) as ISlarkProtocol));
                            }
                        }
                    }
                }
            }
            else if (context.Message is TMJsonResponse response)
            {
                if (context.Sender.Client is TMClient client)
                {
                    client.OnResponse?.Invoke(client, response);
                }

                return(Task.FromResult(new OkProtocol() as ISlarkProtocol));
            }

            return(base.FindAsync(context));
        }
 public virtual void Register(string functionName, RpcFunctionDelegate functionHandler)
 {
     RpcFuncs[functionName] = functionHandler;
     Console.WriteLine($"rpc.Register:{functionName}");
 }
示例#3
0
文件: Rpc.cs 项目: wujun4code/Slark
 public StandardRpcFunctionHandler(RpcFunctionDelegate funcDelegate)
 {
     FuncDel = funcDelegate;
 }
示例#4
0
文件: Rpc.cs 项目: wujun4code/Slark
 public static void Register(this TMLobby server, string functionName, RpcFunctionDelegate rpcFunction)
 {
     server.Register(functionName, new StandardRpcFunctionHandler(rpcFunction));
 }
示例#5
0
文件: Rpc.cs 项目: wujun4code/Slark
        public static List <(string, RpcFunctionDelegate)> ReflectRpcFunctions(this IRpc rpcHost)
        {
            var hostType = rpcHost.GetType();
            var methods  = hostType.GetMethods();

            var tupple = new List <(string, RpcFunctionDelegate)>();

            foreach (var method in methods)
            {
                var hookAttributes = method.GetCustomAttributes(typeof(RpcAttribute), false);

                if (hookAttributes.Length == 1)
                {
                    var rpcAttribute = (RpcAttribute)hookAttributes[0];

                    RpcFunctionDelegate rpcFunction = async(context) =>
                    {
                        var pas = BindParamters(method, context);

                        object result = null;

                        object host = null;
                        if (!method.IsStatic)
                        {
                            host = rpcHost;
                        }

                        if (method.ReturnType == typeof(Task))
                        {
                            Task  awaitable = (Task)method.Invoke(host, pas);
                            await awaitable;
                        }
                        else if (method.ReturnType == typeof(void))
                        {
                            method.Invoke(host, pas);
                        }
                        else if (method.ReturnType.IsGenericType)
                        {
                            if (method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                            {
                                result = await method.InvokeAsync(host, pas);
                            }
                            else
                            {
                                result = method.Invoke(host, pas);
                            }
                        }
                        else
                        {
                            result = method.Invoke(host, pas);
                        }

                        var encodedObject = TMEncoding.Instance.Encode(result);

                        var resultWrapper = new Dictionary <string, object>()
                        {
                            { "results", encodedObject }
                        };

                        context.Response = Json.Encode(resultWrapper);
                    };

                    var hostAttribute = hostType.GetCustomAttribute <RpcHostAttribute>();
                    var hostName      = string.IsNullOrEmpty(hostAttribute.Name) ? hostType.Name : hostAttribute.Name;
                    var methodName    = rpcAttribute.Name ?? method.Name;
                    var rpcMethodName = $"{hostName}_{methodName}";
                    if (!method.IsStatic)
                    {
                        var hostId = rpcHost.ReflectRpcHostIdPropertyValue();
                        rpcMethodName = $"{hostName}_{hostId}_{methodName}";
                    }
                    var mixedResult = (rpcMethodName, rpcFunction);

                    tupple.Add(mixedResult);
                }
            }
            return(tupple);
        }