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> /// 设置返回值 /// </summary> /// <param name="val"></param> /// <returns></returns> public bool SetReturnValue(ZYClient_Result_Return val) { if (AsynRetrunDiy.ContainsKey(val.Id)) { AsynReturn asynRet = new AsynReturn() { ReturnValue = val }; var callback = AsynRetrunDiy[val.Id]; asynRet.Format(callback.ReturnType, callback.ArgsType); AsynRetrunDiy.TryRemove(val.Id, out callback); System.Threading.Tasks.Task.Factory.StartNew(() => { callback.Call(asynRet); }).ContinueWith(p => { try { p.Wait(); } catch (Exception er) { if (ErrMsgOut != null) { ErrMsgOut(er.ToString()); } } }); return(true); } else if (ReturnValueDiy.ContainsKey(val.Id)) { var x = ReturnValueDiy[val.Id]; //x.returnvalue = val; //x.IsReturn = true; x.SetResult(val); ReturnValueDiy.TryRemove(val.Id, out x); return(true); } return(false); }
/// <summary> /// 设置返回值 /// </summary> /// <param name="val"></param> /// <returns></returns> public bool SetReturnValue(ZYClient_Result_Return val) { if (AsynRetrunDiy.ContainsKey(val.Id)) { AsynReturn asynRet = new AsynReturn() { ReturnValue = val }; asynRet.Format(); Action <AsynReturn> callback = AsynRetrunDiy[val.Id]; AsynRetrunDiy.TryRemove(val.Id, out callback); System.Threading.Tasks.Task.Factory.StartNew(() => { callback(asynRet); }).ContinueWith(p => { try { p.Wait(); } catch (Exception er) { if (ErrMsgOut != null) { ErrMsgOut(er.ToString()); } } }); } else if (ReturnValueDiy.ContainsKey(val.Id)) { WaitReturnValue x; ReturnValueDiy.TryRemove(val.Id, out x); x.returnvalue = val; x.waitHandle.Set(); } return(false); }