コード例 #1
0
ファイル: AsyncModuleDef.cs プロジェクト: 12345fish/ZYNet
        public void Install(object o)
        {
            Type type = o.GetType();

            var methos = type.GetMethods();

            Type tasktype = typeof(Task);


            foreach (var method in methos)
            {
                var attr = method.GetCustomAttributes(typeof(MethodRun), true);

                foreach (var att in attr)
                {
                    MethodRun attrcmdtype = att as MethodRun;

                    if (attrcmdtype != null)
                    {
                        if ((method.ReturnType == tasktype || (Common.IsTypeOfBaseTypeIs(method.ReturnType, tasktype) && method.ReturnType.IsConstructedGenericType && method.ReturnType.GenericTypeArguments[0] == typeof(ReturnResult))))
                        {
                            if (method.GetParameters().Length > 0 && method.GetParameters()[0].ParameterType == typeof(AsyncCalls))
                            {
                                if (!ModuleDiy.ContainsKey(attrcmdtype.CmdType))
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                    ModuleDiy.Add(attrcmdtype.CmdType, tmp);
                                }
                                else
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                    ModuleDiy[attrcmdtype.CmdType] = tmp;
                                }
                            }
                        }
                        else if (method.GetParameters().Length > 0 && method.GetParameters()[0].ParameterType == typeof(CloudClient))
                        {
                            if (!ModuleDiy.ContainsKey(attrcmdtype.CmdType))
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                ModuleDiy.Add(attrcmdtype.CmdType, tmp);
                            }
                            else
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                ModuleDiy[attrcmdtype.CmdType] = tmp;
                            }
                        }


                        break;
                    }
                }
            }
        }
コード例 #2
0
ファイル: CloudClient.cs プロジェクト: 12345fish/ZYNet
        private void CallPackRun(CallPack pack)
        {
            if (Module.ModuleDiy.ContainsKey(pack.CmdTag))
            {
                AsyncMethodDef method = Module.ModuleDiy[pack.CmdTag];

                object[] args = null;

                int argcount = 0;

                if (pack.Arguments != null)
                {
                    argcount = pack.Arguments.Count;
                }

                if (method.ArgsType.Length > 0 && method.ArgsType.Length == (argcount + 1))
                {
                    args = new object[method.ArgsType.Length];

                    args[0] = this;
                    int x = 1;
                    for (int i = 0; i < (method.ArgsType.Length - 1); i++)
                    {
                        x       = i + 1;
                        args[x] = Serialization.UnpackSingleObject(method.ArgsType[x], pack.Arguments[i]);
                    }
                }

                if (args == null)
                {
                    LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount);
                }

                if (method.IsAsync)
                {
                    if (!method.IsOut)
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, false);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        _calls_.Run();

                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                    }
                    else
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, true);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        _calls_.Complete += RetrunResultData;
                        _calls_.Run();


                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                    }
                }
                else //SYNC
                {
                    if (!method.IsOut)
                    {
                        method.methodInfo.Invoke(method.Obj, args);
                    }
                    else
                    {
                        try
                        {
                            object res = method.methodInfo.Invoke(method.Obj, args);

                            if (res != null)
                            {
                                ReturnResult tmp = new ReturnResult(res);
                                tmp.Id = pack.Id;
                                RetrunResultData(tmp);
                            }
                        }
                        catch (Exception er)
                        {
                            ReturnResult tmp = new ReturnResult();
                            tmp.Id = pack.Id;
                            RetrunResultData(tmp);

                            LogAction.Log(LogType.Err, "Cmd:{0} ERROR:" + er.ToString(), pack.CmdTag);
                        }
                    }
                }
            }
            else
            {
                LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} Not Find Cmd", pack.CmdTag);
            }
        }
コード例 #3
0
ファイル: AsyncModuleDef.cs プロジェクト: lanicon/ZYNet
        public void Install(object o)
        {
            Type type = o.GetType();

            var methos = type.GetMethods();

            Type tasktype = typeof(Task);


            foreach (var method in methos)
            {
                var attr = method.GetCustomAttributes(typeof(TAG), true);

                foreach (var att in attr)
                {
                    if (att is TAG attrcmdtype)
                    {
                        if ((method.ReturnType == tasktype || (Common.IsTypeOfBaseTypeIs(method.ReturnType, tasktype) && method.ReturnType.IsConstructedGenericType)))
                        {
                            if (method.GetParameters().Length > 0 && (method.GetParameters()[0].ParameterType == typeof(AsyncCalls) || method.GetParameters()[0].ParameterType == typeof(IASync)))
                            {
                                if (!ModuleDiy.ContainsKey(attrcmdtype.CmdTag))
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                    ModuleDiy.Add(attrcmdtype.CmdTag, tmp);
                                }
                                else
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                    ModuleDiy[attrcmdtype.CmdTag] = tmp;
                                }
                            }
                            else if (method.GetParameters().Length == 0 || (method.GetParameters()[0].ParameterType != typeof(AsyncCalls) && method.GetParameters()[0].ParameterType != typeof(IASync)))
                            {
                                if (!ModuleDiy.ContainsKey(attrcmdtype.CmdTag))
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o)
                                    {
                                        IsNotRefAsyncArg = true
                                    };

                                    ModuleDiy.Add(attrcmdtype.CmdTag, tmp);
                                }
                                else
                                {
                                    AsyncMethodDef tmp = new AsyncMethodDef(method, o)
                                    {
                                        IsNotRefAsyncArg = true
                                    };
                                    ModuleDiy[attrcmdtype.CmdTag] = tmp;
                                }
                            }
                        }
                        else if (method.GetParameters().Length > 0 && (method.GetParameters()[0].ParameterType == typeof(CloudClient) || method.GetParameters()[0].ParameterType == typeof(IASync)))
                        {
                            if (!ModuleDiy.ContainsKey(attrcmdtype.CmdTag))
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                ModuleDiy.Add(attrcmdtype.CmdTag, tmp);
                            }
                            else
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o);
                                ModuleDiy[attrcmdtype.CmdTag] = tmp;
                            }
                        }
                        else if (method.GetParameters().Length == 0 || (method.GetParameters()[0].ParameterType != typeof(CloudClient) || method.GetParameters()[0].ParameterType != typeof(IASync)))
                        {
                            if (!ModuleDiy.ContainsKey(attrcmdtype.CmdTag))
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o)
                                {
                                    IsNotRefAsyncArg = true
                                };
                                ModuleDiy.Add(attrcmdtype.CmdTag, tmp);
                            }
                            else
                            {
                                AsyncMethodDef tmp = new AsyncMethodDef(method, o)
                                {
                                    IsNotRefAsyncArg = true
                                };
                                ModuleDiy[attrcmdtype.CmdTag] = tmp;
                            }
                        }


                        break;
                    }
                }
            }
        }