Exemple #1
0
        /// <summary>
        /// 调用CALL
        /// </summary>
        /// <param name="tmp">调用包</param>
        /// <param name="needReturn">是否需要返回</param>
        /// <param name="returnValue">返回值</param>
        /// <returns></returns>
        public bool RunModule(RPCCallPack tmp, bool needReturn, out object returnValue)
        {
            returnValue = null;

            try
            {
                var method = Module.GetMethod(tmp.Tag, tmp.Method);

                if (method != null)
                {
                    if (tmp.Arguments != null)
                    {
                        object[] arguments = new object[method.ArgsType.Length];


                        for (int i = 0; i < tmp.Arguments.Count; i++)
                        {
                            arguments[i] = Serialization.UnpackSingleObject(method.ArgsType[i], tmp.Arguments[i]);
                        }

                        returnValue = method.methodInfo.Invoke(method.Token, arguments);

                        if (needReturn)
                        {
                            if (method.IsOut)
                            {
                                for (int i = 0; i < arguments.Length; i++)
                                {
                                    tmp.Arguments[i] = Serialization.PackSingleObject(method.ArgsType[i], arguments[i]);
                                }
                            }
                            else
                            {
                                tmp.Arguments = null;
                            }
                        }

                        return(true);
                    }
                    else
                    {
                        returnValue = method.methodInfo.Invoke(method.Token, null);
                        return(true);
                    }
                }
                else
                {
                    LogAction.Warn("Not find " + tmp.Tag + "-> public " + tmp.Method);
                }

                return(false);
            }
            catch (Exception er)
            {
                LogAction.Err(er.ToString());
                return(false);
            }
        }
Exemple #2
0
        public override IMessage Invoke(IMessage reqMsg)
        {
            IMethodCallMessage ctorMsg = reqMsg as IMethodCallMessage;


            List <byte[]> arglist = new List <byte[]>(ctorMsg.ArgCount);

            Type[] types = ctorMsg.MethodSignature as Type[];

            Type[] argsType = new Type[ctorMsg.ArgCount];

            object[] args = ctorMsg.Args;

            for (int i = 0; i < ctorMsg.ArgCount; i++)
            {
                argsType[i] = args[i].GetType();
                arglist.Add(Serialization.PackSingleObject(argsType[i], args[i]));
            }

            var returnType = (ctorMsg.MethodBase as MethodInfo).ReturnType;

            if (returnType != nullType)
            {
                if (CallHaveReturn == null)
                {
                    throw new Exception("event not register");
                }
                ProxyReturnValue returnval = CallHaveReturn(Tag, Make.MakeMethodName(ctorMsg.MethodName, types), argsType, arglist, returnType);
                if (returnval.Args == null)
                {
                    returnval.Args = args;
                }

                return(new ReturnMessage(returnval.returnVal, returnval.Args, returnval.Args == null ? 0 : returnval.Args.Length, null, ctorMsg));
            }
            else
            {
                if (CallNullReturn == null)
                {
                    throw new Exception("event not register");
                }

                CallNullReturn(Tag, Make.MakeMethodName(ctorMsg.MethodName, types), argsType, arglist); //如果你没有返回值那么out ref将失效


                return(new ReturnMessage(null, args, args == null ? 0 : args.Length, null, ctorMsg));
            }
        }
Exemple #3
0
        public void BinaryInput(byte[] data)
        {
            ReadBytes read = new ReadBytes(data);

            int lengt;
            int cmd;

            if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
            {
                switch (cmd)
                {
                case 1001001:
                {
                    Result_Have_Return val;

                    if (read.ReadObject <Result_Have_Return>(out val))
                    {
                        RPC_Call.SetReturnValue(val);
                    }
                }
                break;

                case 1001000:
                {
                    ThreadPool.QueueUserWorkItem((o) =>
                        {
                            RPCCallPack tmp;
                            ReadBytes pread = (ReadBytes)o;

                            if (pread.ReadObject <RPCCallPack>(out tmp))
                            {
                                object returnValue;

                                try
                                {
                                    if (RPC_Call.RunModule(tmp, tmp.NeedReturn, out returnValue))
                                    {
                                        if (tmp.NeedReturn)
                                        {
                                            Result_Have_Return var = new Result_Have_Return()
                                            {
                                                Id        = tmp.Id,
                                                Arguments = tmp.Arguments
                                            };

                                            if (returnValue != null)
                                            {
                                                var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue);
                                            }

                                            Client.BeginSendData(BufferFormat.FormatFCA(var));
                                        }
                                    }
                                }
                                catch (Exception er)
                                {
                                    LogAction.Err(er.ToString());
                                }
                            }
                        }, read);
                }
                break;

                default:
                {
                    if (DataOn != null)
                    {
                        DataOn(cmd, read);
                    }
                }
                break;
                }
            }
        }