示例#1
0
        private void CallPackRun(CallPack pack)
        {
            if (CurrentServer.CallsMethods.ContainsKey(pack.CmdTag))
            {
                if (AsyncCallDict == null)
                {
                    AsyncCallDict = new ConcurrentDictionary <long, AsyncCalls>();
                }
                if (ControllerDict == null)
                {
                    ControllerDict = new ConcurrentDictionary <Type, ControllerBase>();
                }

                var method = CurrentServer.CallsMethods[pack.CmdTag];


                if (!ControllerDict.TryGetValue(method.ImplementationType, out ControllerBase implement))
                {
                    implement = (ControllerBase)Activator.CreateInstance(method.ImplementationType, this.Container, this.LoggerFactory);
                    ControllerDict[method.ImplementationType] = implement;
                }



                object[] args = null;

                int argcount = 0;

                if (pack.Arguments != null)
                {
                    argcount = pack.Arguments.Count;
                }
                if (!method.IsNotAsyncArg)
                {
                    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 (method.IsAsync)
                    {
                        if (!method.IsRet)
                        {
                            AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, false);
                            args[0]              = _calls_;
                            _calls_.CallSend    += SendData;
                            _calls_.ExceptionOut = this.ExceptionOut;
                            AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                            _calls_.Run();
                        }
                        else
                        {
                            AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, true);
                            args[0]              = _calls_;
                            _calls_.CallSend    += SendData;
                            _calls_.Complete    += ResrunResultData;
                            _calls_.ExceptionOut = this.ExceptionOut;
                            AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                            _calls_.Run();
                        }
                    }
                    else //SYNC
                    {
                        if (!method.IsRet)
                        {
                            method.MethodInfo.Invoke(implement, args);
                        }
                        else
                        {
                            try
                            {
                                object res = method.MethodInfo.Invoke(implement, args);

                                if (res != null)
                                {
                                    if (res is Result result)
                                    {
                                        result.Id = pack.Id;

                                        ResrunResultData(result);
                                    }
                                    else
                                    {
                                        Result tmp = new Result(res)
                                        {
                                            Id = pack.Id
                                        };

                                        ResrunResultData(tmp);
                                    }
                                }
                            }
                            catch (Exception er)
                            {
                                var tmp = base.GetExceptionResult(er, pack.Id);
                                ResrunResultData(tmp);
                                if (PushException(er))
                                {
                                    Log.Error($"Cmd:{pack.CmdTag} ERROR:{er}");
                                }
                            }
                        }
                    }
                }
                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(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, false);
                            _calls_.CallSend    += SendData;
                            _calls_.ExceptionOut = this.ExceptionOut;
                            AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                            _calls_.Run();
                        }
                        else
                        {
                            AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, true);
                            _calls_.CallSend    += SendData;
                            _calls_.Complete    += ResrunResultData;
                            _calls_.ExceptionOut = this.ExceptionOut;
                            AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                            _calls_.Run();
                        }
                    }
                    else //SYNC
                    {
                        if (!method.IsRet)
                        {
                            method.MethodInfo.Invoke(implement, args);
                        }
                        else
                        {
                            try
                            {
                                object res = method.MethodInfo.Invoke(implement, args);

                                if (res != null)
                                {
                                    if (res is Result result)
                                    {
                                        result.Id = pack.Id;

                                        ResrunResultData(result);
                                    }
                                    else
                                    {
                                        Result tmp = new Result(res)
                                        {
                                            Id = pack.Id
                                        };

                                        ResrunResultData(tmp);
                                    }
                                }
                            }
                            catch (Exception er)
                            {
                                var tmp = base.GetExceptionResult(er, pack.Id);
                                ResrunResultData(tmp);
                                if (PushException(er))
                                {
                                    Log.Error($"Cmd:{pack.CmdTag} ERROR:{er}");
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Log.Error($"Clent Call Cmd:{pack.CmdTag} Not Find");
            }
        }
示例#2
0
        private void CallPackRun(CallPack pack)
        {
            if (CurrentServer.CallsMethods.ContainsKey(pack.CmdTag))
            {
                if (AsyncCallDiy == null)
                {
                    AsyncCallDiy = new ConcurrentDictionary <long, AsyncCalls>();
                }

                var method = CurrentServer.CallsMethods[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, "Clent Call Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount);
                    return;
                }
                if (method.IsAsync)
                {
                    if (!method.IsOut)
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.methodInfo, args, false);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                        _calls_.Run();
                    }
                    else
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.methodInfo, args, true);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        _calls_.Complete += RetrunResultData;
                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                        _calls_.Run();
                    }
                }
                else //SYNC
                {
                    if (!method.IsOut)
                    {
                        method.methodInfo.Invoke(null, args);
                    }
                    else
                    {
                        try
                        {
                            object res = method.methodInfo.Invoke(null, 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, "Clent Call Cmd:{0} Not Find", pack.CmdTag);
            }
        }