public void Init(object o) { Token = o; Type type = o.GetType(); var methos = type.GetMethods(); foreach (var item in methos) { if (item.IsPublic) { string methodName = item.Name; var args = item.GetParameters(); Type[] typeArg = new Type[args.Length]; for (int i = 0; i < args.Length; i++) { typeArg[i] = args[i].ParameterType; } methodName = MakeID.MakeMethodName(methodName, typeArg); if (!MethodInfoDiy.ContainsKey(methodName)) { MethodInfoDiy.Add(methodName, new MethodModuleDef(item)); } } } }
public override IMessage Invoke(IMessage reqMsg) { IMethodCallMessage ctorMsg = reqMsg as IMethodCallMessage; if (Call != null) { List <byte[]> arglist = new List <byte[]>(); Type[] types = ctorMsg.MethodSignature as Type[]; List <Type> argsType = new List <Type>(ctorMsg.ArgCount); object[] args = ctorMsg.Args; for (int i = 0; i < ctorMsg.ArgCount; i++) { argsType.Add(args[i].GetType()); arglist.Add(Serialization.PackSingleObject(argsType[i], args[i])); } ReturnValue returnval = Call(ModuleName, MakeID.MakeMethodName(ctorMsg.MethodName, types), argsType, arglist, (ctorMsg.MethodBase as MethodInfo).ReturnType); if (returnval.Args == null) { returnval.Args = args; } return(new ReturnMessage(returnval.returnVal, returnval.Args, returnval.Args == null ? 0 : returnval.Args.Length, null, ctorMsg)); } throw new Exception("event not register"); }
public void CallMethod(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args) { 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]); } } return; } else { ReturnValueDiy.Remove(call.Id); if (ErrMsgOut != null) { ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time."); } return; } } }
public void CallMethod(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args) { 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]); } } } else { ReturnValueDiy.TryRemove(call.Id, out var); if (ErrMsgOut != null) { ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time."); } } }
public void CallMethod(string module, string MethodName, List <RPCArgument> arglist, out object[] args) { args = null; RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = DateTime.Now, CallModule = module, Method = MethodName, Arguments = arglist, IsNeedReturn = true, }; WaitReturnValue var = new WaitReturnValue(); using (var.waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset)) { ReturnValueDiy.AddOrUpdate(call.Id, var, (a, b) => 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 < returnx.Arguments.Count; i++) { args[i] = Serialization.UnpackSingleObject(returnx.Arguments[i].type, returnx.Arguments[i].Value); } } return; } else { ReturnValueDiy.TryRemove(call.Id, out var); throw new TimeoutException("out time,Please set the timeout time."); } } }
/// <summary> /// 异步调用,返回值后调用 Callback /// </summary> /// <typeparam name="Mode"></typeparam> /// <typeparam name="Result"></typeparam> /// <param name="action"></param> /// <param name="Callback"></param> public void CallAsyn <Mode, Result>(Expression <Func <Mode, Result> > action, Action <AsynReturn> Callback) { MethodCallExpression body = action.Body as MethodCallExpression; List <RPCArgument> argumentlist = new List <RPCArgument>(); var parameters = body.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { dynamic p = Expression.Call(FormatValue.GetMethodInfo(body.Arguments[i].Type), body.Arguments[i]); dynamic x = Expression.Lambda <Func <dynamic> >(p).Compile()(); RPCArgument tmp = new RPCArgument(); tmp.type = body.Arguments[i].Type; tmp.RefType = parameters[i].ParameterType; tmp.Value = Serialization.PackSingleObject(body.Arguments[i].Type, x); argumentlist.Add(tmp); } RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = DateTime.Now, CallModule = body.Object.Type.Name, Method = body.Method.Name, Arguments = argumentlist, IsNeedReturn = true, }; byte[] data = BufferFormat.FormatFCA(call); if (CallBufferOutSend != null) { CallBufferOutSend(data); } AsynRetrunDiy.AddOrUpdate(call.Id, Callback, (a, b) => Callback); }