/// <summary> /// 调用模块 /// </summary> /// <param name="data"></param> /// <returns>true 属于次模块,false 不属于此模块数据</returns> public bool CallModule(byte[] data, SocketAsyncEventArgs e, out ZYClient_Result_Return returnRes) { returnRes = null; ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { switch (cmd) { case 1001000: { ZYClientCall tmp; if (read.ReadObject <ZYClientCall>(out tmp)) { object returnValue; CallContext.SetData("Current", e); if (RunModule(tmp, out returnValue)) { if (tmp.IsNeedReturn) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = MsgPack.Serialization.SerializationContext.Default.GetSerializer(returnValue.GetType()).PackSingleObject(returnValue); var.ReturnType = returnValue.GetType().FullName; } returnRes = var; } return(true); } } } break; } } return(false); }
private void Call(RPCCallPack rpcPack, RPCUserInfo e) { try { object returnValue; CallContext.SetData("Current", e); string msg; if (e.RPC_Call.RunModule(rpcPack, out msg, out returnValue)) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = rpcPack.Id, CallTime = rpcPack.CallTime, Arguments = rpcPack.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); } e.BeginSendData(BufferFormat.FormatFCA(var)); } } catch (Exception er) { if (MsgOut != null) { if (e.Asyn.RemoteEndPoint != null) { MsgOut(e.Asyn.RemoteEndPoint.ToString() + "::" + rpcPack.CallModule + "->" + rpcPack.Method + "\r\n" + er.ToString(), MsgOutType.Err); } else { MsgOut(rpcPack.CallModule + "->" + rpcPack.Method + "\r\n" + er.ToString(), MsgOutType.Err); } } } }
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: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_Return>(out val)) { RPC_Call.SetReturnValue(val); } } break; case 1001000: { RPCCallPack tmp; if (read.ReadObject <RPCCallPack>(out tmp)) { System.Threading.Tasks.Task.Factory.StartNew((pack) => { CallContext.SetData("Current", this); string msg; object returnValue; if (RPC_Call.RunModule(pack as RPCCallPack, out msg, out returnValue)) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); } Client.BeginSendData(BufferFormat.FormatFCA(var)); } else { if (MsgOut != null) { MsgOut(tmp.CallModule + "->" + tmp.Method + " Call Failure"); } } }, tmp, CancellationToken.None, TaskCreationOptions.None, OrderSchedulerRead).ContinueWith(task => { try { task.Wait(); } catch (Exception er) { if (MsgOut != null) { MsgOut(er.ToString()); } } }); } } break; default: { if (DataOn != null) { DataOn(cmd, read); } } break; } } }
public void Call <Mode>(Expression <Action <Mode> > action) { MethodCallExpression body = action.Body as MethodCallExpression; if (body != null && IsConnect) { string callModule = body.Object.Type.FullName; string method = body.Method.Name; List <string> ArgumentsType = new List <string>(); List <byte[]> Arguments = new List <byte[]>(); List <Type> ArgType = new List <Type>(); foreach (var item in body.Arguments) { var p = Expression.Call(null, this.GetType().GetMethod("GetObjectArg", new[] { item.Type }), item); object x = Expression.Lambda <Func <object> >(p).Compile()(); if (x != null) { ArgType.Add(x.GetType()); ArgumentsType.Add(x.GetType().FullName); Arguments.Add(MsgPack.Serialization.SerializationContext.Default.GetSerializer(x.GetType()).PackSingleObject(x)); } else { Type type = item.Type; ArgType.Add(type); ArgumentsType.Add(type.FullName); Arguments.Add(MsgPack.Serialization.SerializationContext.Default.GetSerializer(type).PackSingleObject(null)); } } ZYClientCall call = new ZYClientCall() { Id = Guid.NewGuid().ToString(), CallTime = DateTime.Now, CallModule = callModule, Method = method, Arguments = Arguments, IsNeedReturn = true, ArgumentsType = ArgumentsType }; Client.Send(BufferFormatV2.FormatFCA(call)); using (EventWaitHandle wait = new EventWaitHandle(false, EventResetMode.AutoReset)) { ResReturn var = new ResReturn() { waitHander = wait, ReturnValue = null }; ResRetrunDiy.AddOrUpdate(call.Id, var, (a, b) => var); if (wait.WaitOne(OutTime)) { ZYClient_Result_Return returnx = ResRetrunDiy[call.Id].ReturnValue; if (returnx.Arguments != null && returnx.Arguments.Count > 0 && ArgType.Count == returnx.Arguments.Count) { object[] args = new object[returnx.Arguments.Count]; for (int i = 0; i < returnx.Arguments.Count; i++) { args[i] = MsgPack.Serialization.SerializationContext.Default.GetSerializer(ArgType[i]).UnpackSingleObject(returnx.Arguments[i]); } if (args.Length == body.Arguments.Count) { for (int i = 0; i < args.Length; i++) { if (body.Arguments[i].NodeType == ExpressionType.MemberAccess) { var set = Expression.Assign(body.Arguments[i], Expression.Constant(args[i])); Expression.Lambda <Action>(set).Compile()(); } } } } } ResRetrunDiy.TryRemove(call.Id, out var); } } }
/// <summary> /// 调用模块 /// </summary> /// <param name="data"></param> /// <returns>true 属于次模块,false 不属于此模块数据</returns> public bool CallModule(byte[] data, RPCUserInfo e, out ReadBytes read, out int cmd) { cmd = -1; read = new ReadBytes(data); int lengt; if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { switch (cmd) { case 1001000: { RPCCallPack tmp; if (read.ReadObject <RPCCallPack>(out tmp)) { System.Threading.Tasks.Task.Factory.StartNew(() => { object returnValue; CallContext.SetData("Current", e); if (e.RPC_Call.RunModule(tmp, out returnValue)) { if (tmp.IsNeedReturn) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); var.ReturnType = returnValue.GetType(); } e.EnsureSend(BufferFormat.FormatFCA(var)); } } }, CancellationToken.None, TaskCreationOptions.None, e.QueueScheduler).ContinueWith(p => { try { p.Wait(); } catch (Exception er) { if (MsgOut != null) { MsgOut(er.ToString()); } } }); return(true); } } break; case 1001001: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_Return>(out val)) { e.RPC_Call.SetReturnValue(val); return(true); } } break; } } return(false); }
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: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_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; CallContext.SetData("Current", this); if (RPC_Call.RunModule(tmp, out returnValue)) { if (tmp.IsNeedReturn) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); var.ReturnType = returnValue.GetType(); } Client.Send(BufferFormat.FormatFCA(var)); } } } }, read); } break; default: { if (DataOn != null) { DataOn(cmd, read); } } break; } } }
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: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_Return>(out val)) { RPC_Call.SetReturnValue(val); } } break; case 1001000: { System.Threading.Tasks.Task.Factory.StartNew(() => { RPCCallPack tmp; if (read.ReadObject <RPCCallPack>(out tmp)) { object returnValue; CallContext.SetData("Current", this); if (RPC_Call.RunModule(tmp, out returnValue)) { if (tmp.IsNeedReturn) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); var.ReturnType = returnValue.GetType(); } Client.Send(BufferFormat.FormatFCA(var)); } } } }, CancellationToken.None, TaskCreationOptions.None, OrderSchedulerRead); } break; default: { if (DataOn != null) { DataOn(cmd, read); } } break; } } }