コード例 #1
0
ファイル: RPC.cs プロジェクト: zhaopan-vip/ZYSOCKET
        public Result CallMethod <Result>(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args, Type returnType = null)
        {
            args = null;
            RPCCallPack call = new RPCCallPack()
            {
                Id         = MakeID.GetID(),
                CallTime   = MakeID.GetTick(),
                CallModule = module,
                Method     = MethodName,
                Arguments  = arglist
            };



            WaitReturnValue var = new WaitReturnValue();

            using (var.waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset))
            {
                ReturnValueDiy.Add(call.Id, var);

                byte[] data = BufferFormat.FormatFCA(call);

                if (CallBufferOutSend != null)
                {
                    CallBufferOutSend(data);
                }

                if (var.waitHandle.WaitOne(OutTime))
                {
                    ZYClient_Result_Return returnx = var.returnvalue;

                    if (returnx.Arguments != null && returnx.Arguments.Count > 0 && arglist.Count == returnx.Arguments.Count)
                    {
                        args = new object[returnx.Arguments.Count];

                        for (int i = 0; i < argTypeList.Count; i++)
                        {
                            args[i] = Serialization.UnpackSingleObject(argTypeList[i], returnx.Arguments[i]);
                        }
                    }


                    if (returnx.Return != null)
                    {
                        if (returnType != null)
                        {
                            object returnobj = Serialization.UnpackSingleObject(returnType, returnx.Return);

                            return((Result)returnobj);
                        }
                        else
                        {
                            object returnobj = Serialization.UnpackSingleObject(typeof(Result), returnx.Return);

                            return((Result)returnobj);
                        }
                    }
                    else
                    {
                        return(default(Result));
                    }
                }
                else
                {
                    ReturnValueDiy.Remove(call.Id);

                    if (ErrMsgOut != null)
                    {
                        ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time.");
                    }

                    return(default(Result));
                }
            }
        }
コード例 #2
0
ファイル: RPC.cs プロジェクト: zhaopan-vip/ZYSOCKET
        public bool RunModule(RPCCallPack tmp, out object returnValue)
        {
            returnValue = null;

            if (ModuleDiy.ContainsKey(tmp.CallModule))
            {
                var module = ModuleDiy[tmp.CallModule];

                if (module.MethodInfoDiy.ContainsKey(tmp.Method))
                {
                    var method = module.MethodInfoDiy[tmp.Method];

                    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(module.Token, arguments);

                        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(module.Token, null);
                        return(true);
                    }
                }
                else
                {
                    string msg = "Not find " + tmp.CallModule + "-> public " + tmp.Method;

                    if (ErrMsgOut != null)
                    {
                        ErrMsgOut(msg);
                    }

                    return(false);
                }
            }
            else
            {
                string msg = "Not find " + tmp.CallModule;

                if (ErrMsgOut != null)
                {
                    ErrMsgOut(msg);
                }
            }


            return(false);
        }
コード例 #3
0
        public Result CallMethod <Result>(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args, Type returnType = null)
        {
            args = null;
            RPCCallPack call = new RPCCallPack()
            {
                Id         = MakeID.GetID(),
                CallTime   = MakeID.GetTick(),
                CallModule = module,
                Method     = MethodName,
                Arguments  = arglist
            };


            TaskCompletionSource <ZYClient_Result_Return> var = new TaskCompletionSource <ZYClient_Result_Return>(TaskCreationOptions.AttachedToParent);

            if (!ReturnValueDiy.TryAdd(call.Id, var))
            {
                SpinWait.SpinUntil(() => ReturnValueDiy.TryAdd(call.Id, var));
            }

            byte[] data = BufferFormat.FormatFCA(call);
            if (CallBufferOutSend != null)
            {
                CallBufferOutSend(data);
            }

            if (var.Task.Wait(OutTime))
            {
                ZYClient_Result_Return returnx = var.Task.Result;


                if (returnx.Arguments != null && returnx.Arguments.Count > 0 && arglist.Count == returnx.Arguments.Count)
                {
                    args = new object[returnx.Arguments.Count];

                    for (int i = 0; i < argTypeList.Count; i++)
                    {
                        args[i] = Serialization.UnpackSingleObject(argTypeList[i], returnx.Arguments[i]);
                    }
                }


                if (returnx.Return != null)
                {
                    if (returnType != null)
                    {
                        object returnobj = Serialization.UnpackSingleObject(returnType, returnx.Return);

                        return((Result)returnobj);
                    }
                    else
                    {
                        object returnobj = Serialization.UnpackSingleObject(typeof(Result), returnx.Return);

                        return((Result)returnobj);
                    }
                }
                else
                {
                    return(default(Result));
                }
            }
            else
            {
                ReturnValueDiy.TryRemove(call.Id, out var);


                if (ErrMsgOut != null)
                {
                    ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time.");
                }

                return(default(Result));
            }
        }