Ejemplo n.º 1
0
        public void Install(Type packHandlerType)
        {
            if (packHandlerType.BaseType != typeof(ControllerBase))
            {
                throw new TypeLoadException($"{packHandlerType.Name} not inherit ControllerBase");
            }

            var methods = packHandlerType.GetMethods();


            Type tasktype = typeof(Task);

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


                foreach (var att in attr)
                {
                    if (att is TAG attrcmdtype)
                    {
                        if (!CallsMethods.ContainsKey(attrcmdtype.CmdTag))
                        {
                            IAsyncMethodDef tmp = base.Container.Resolve <IAsyncMethodDef>(new NamedParameter("implementationType", packHandlerType), new NamedParameter("methodInfo", method));

                            if (method.GetParameters().Length == 0 || (method.GetParameters()[0].ParameterType.BaseType != typeof(IASync) && method.GetParameters()[0].ParameterType != typeof(IASync)))
                            {
                                tmp.IsNotAsyncArg = true;
                            }

                            CallsMethods.Add(attrcmdtype.CmdTag, tmp);
                        }

                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void CallPackRun(CallPack pack)
        {
            if (Module.ModuleDiy.ContainsKey(pack.CmdTag))
            {
                IAsyncMethodDef method = Module.ModuleDiy[pack.CmdTag];


                object[] args = null;

                int argcount = 0;

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

                if (!method.IsNotRefAsyncArg)
                {
                    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)
                    {
                        Log.ErrorFormat("Server Call To Me-> Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount);
                    }

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

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


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

                                if (res != null)
                                {
                                    var tmp = new Result(res)
                                    {
                                        Id = pack.Id
                                    };

                                    RetrunResultData(tmp);
                                }
                            }
                            catch (Exception er)
                            {
                                RetrunResultData(GetExceptionResult(er, pack.Id));

                                if (PushException(er))
                                {
                                    Log.Error($"Cmd:{pack.CmdTag} ERROR:{er.ToString()}");
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (method.ArgsType.Length > 0 && method.ArgsType.Length == argcount)
                    {
                        args = new object[method.ArgsType.Length];

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

                    if (method.IsAsync)
                    {
                        if (!method.IsRet)
                        {
                            AsyncCalls _calls_ = new AsyncCalls(this.LoggerFactory, pack.Id, pack.CmdTag, this, method.Obj, method.MethodInfo, args, false);
                            _calls_.CallSend    += SendData;
                            _calls_.ExceptionOut = this.ExceptionOut;
                            _calls_.Run();

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


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

                                if (res != null)
                                {
                                    var tmp = new Result(res)
                                    {
                                        Id = pack.Id
                                    };

                                    RetrunResultData(tmp);
                                }
                            }
                            catch (Exception er)
                            {
                                RetrunResultData(GetExceptionResult(er, pack.Id));

                                if (PushException(er))
                                {
                                    Log.Error($"Cmd:{pack.CmdTag} ERROR:{er.ToString()}");
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Log.Error($"Server Call To Me-> Cmd:{pack.CmdTag} Not Find Cmd");
            }
        }