/// <summary> /// CALL RETURN /// </summary> /// <param name="cmdTag"></param> /// <param name="args"></param> /// <returns></returns> public ResultAwatier Func(int cmdTag, params object[] args) { CallPack buffer = new CallPack() { Id = Common.MakeID, CmdTag = cmdTag, Arguments = new List <byte[]>(args.Length) }; foreach (var item in args) { Type type = item.GetType(); buffer.Arguments.Add(Serialization.PackSingleObject(type, item)); } using (MemoryStream stream = new MemoryStream()) { BinaryWriter bufflist = new BinaryWriter(stream); if (CCloudClient.EncodingHandler != null) { bufflist.Write(CmdDef.CallCmd); bufflist.Write(buffer.Id); bufflist.Write(buffer.CmdTag); bufflist.Write(buffer.Arguments.Count); foreach (var arg in buffer.Arguments) { bufflist.Write(arg.Length); bufflist.Write(arg); } byte[] fdata = CCloudClient.EncodingHandler(stream.ToArray()); stream.Position = 0; stream.SetLength(0); bufflist.Write(0); bufflist.Write(fdata); } else { bufflist.Write(0); bufflist.Write(CmdDef.CallCmd); bufflist.Write(buffer.Id); bufflist.Write(buffer.CmdTag); bufflist.Write(buffer.Arguments.Count); foreach (var arg in buffer.Arguments) { bufflist.Write(arg.Length); bufflist.Write(arg); } } int l = (int)(stream.Length); byte[] data = BufferFormat.GetSocketBytes(l); stream.Position = 0; bufflist.Write(data); byte[] pdata = stream.ToArray(); CCloudClient.AddAsyncCallBack(this, buffer.Id); CallSend?.Invoke(pdata); } return(_fiber.Read()); }
private void DataOn(byte[] data) { ReadBytes read = null; if (DecodingHandler != null) { read = new ReadBytes(data, 4, -1, DecodingHandler); } else { read = new ReadBytes(data); } if (read.ReadInt32(out int length) && read.ReadInt32(out int cmd) && read.Length == read.Length) { switch (cmd) { case CmdDef.CallCmd: { CallPack call = new CallPack(); try { call.Id = read.ReadInt64(); call.CmdTag = read.ReadInt32(); call.Arguments = new List <byte[]>(); var lengt = read.ReadInt32(); for (int i = 0; i < lengt; i++) { call.Arguments.Add(read.ReadByteArray()); } CallPackRun(call); } catch (Exception er) { if (PushException(new CallException(er.Message, (int)ErrorTag.CallErr, er))) { Log.Error($"CMD:{call.CmdTag} Error:\r\n{er}"); } } } break; case CmdDef.ReturnResult: { Result result = new Result { Id = read.ReadInt64(), ErrorId = read.ReadInt32() }; byte[] strdata = read.ReadByteArray(); if (strdata.Length > 0) { result.ErrorMsg = Encoding.UTF8.GetString(strdata); } var arglengt = read.ReadInt32(); for (int i = 0; i < arglengt; i++) { result.Arguments.Add(read.ReadByteArray()); } SetReturnValue(result); } break; case CmdDef.SetSession: { if (read.ReadInt64(out long sessionid)) { ClientManager.SessionRW.SetSession(sessionid); } } break; } } }
private void CallPackRun(CallPack pack) { if (Module.ModuleDiy.ContainsKey(pack.CmdTag)) { IAsyncMethodDef method = Module.ModuleDiy[pack.CmdTag]; object[] args = null; int argcount = 0; if (pack.Arguments != null) { argcount = pack.Arguments.Count; } if (!method.IsNotRefAsyncArg) { if (method.ArgsType.Length > 0 && method.ArgsType.Length == (argcount + 1)) { args = new object[method.ArgsType.Length]; args[0] = this; int x = 1; for (int i = 0; i < (method.ArgsType.Length - 1); i++) { x = i + 1; args[x] = Serialization.UnpackSingleObject(method.ArgsType[x], pack.Arguments[i]); } } if (args == null) { Log.ErrorFormat("Server Call To Me-> Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount); } if (method.IsAsync) { if (!method.IsRet) { AsyncCalls _calls_ = new AsyncCalls(this.LoggerFactory, pack.Id, pack.CmdTag, this, method.Obj, method.MethodInfo, args, false); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.ExceptionOut = this.ExceptionOut; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } else { AsyncCalls _calls_ = new AsyncCalls(this.LoggerFactory, pack.Id, pack.CmdTag, this, method.Obj, method.MethodInfo, args, true); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.Complete += RetrunResultData; _calls_.ExceptionOut = this.ExceptionOut; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } } else //SYNC { if (!method.IsRet) { method.MethodInfo.Invoke(method.Obj, args); } else { try { object res = method.MethodInfo.Invoke(method.Obj, args); if (res != null) { var tmp = new Result(res) { Id = pack.Id }; RetrunResultData(tmp); } } catch (Exception er) { RetrunResultData(GetExceptionResult(er, pack.Id)); if (PushException(er)) { Log.Error($"Cmd:{pack.CmdTag} ERROR:{er.ToString()}"); } } } } } else { if (method.ArgsType.Length > 0 && method.ArgsType.Length == argcount) { args = new object[method.ArgsType.Length]; for (int i = 0; i < method.ArgsType.Length; i++) { args[i] = Serialization.UnpackSingleObject(method.ArgsType[i], pack.Arguments[i]); } } if (method.IsAsync) { if (!method.IsRet) { AsyncCalls _calls_ = new AsyncCalls(this.LoggerFactory, pack.Id, pack.CmdTag, this, method.Obj, method.MethodInfo, args, false); _calls_.CallSend += SendData; _calls_.ExceptionOut = this.ExceptionOut; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } else { AsyncCalls _calls_ = new AsyncCalls(this.LoggerFactory, pack.Id, pack.CmdTag, this, method.Obj, method.MethodInfo, args, true); _calls_.CallSend += SendData; _calls_.Complete += RetrunResultData; _calls_.ExceptionOut = this.ExceptionOut; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } } else //SYNC { if (!method.IsRet) { method.MethodInfo.Invoke(method.Obj, args); } else { try { object res = method.MethodInfo.Invoke(method.Obj, args); if (res != null) { var tmp = new Result(res) { Id = pack.Id }; RetrunResultData(tmp); } } catch (Exception er) { RetrunResultData(GetExceptionResult(er, pack.Id)); if (PushException(er)) { Log.Error($"Cmd:{pack.CmdTag} ERROR:{er.ToString()}"); } } } } } } else { Log.Error($"Server Call To Me-> Cmd:{pack.CmdTag} Not Find Cmd"); } }
private void CallPackRun(CallPack pack) { if (CurrentServer.CallsMethods.ContainsKey(pack.CmdTag)) { if (AsyncCallDict == null) { AsyncCallDict = new ConcurrentDictionary <long, AsyncCalls>(); } if (ControllerDict == null) { ControllerDict = new ConcurrentDictionary <Type, ControllerBase>(); } var method = CurrentServer.CallsMethods[pack.CmdTag]; if (!ControllerDict.TryGetValue(method.ImplementationType, out ControllerBase implement)) { implement = (ControllerBase)Activator.CreateInstance(method.ImplementationType, this.Container, this.LoggerFactory); ControllerDict[method.ImplementationType] = implement; } object[] args = null; int argcount = 0; if (pack.Arguments != null) { argcount = pack.Arguments.Count; } if (!method.IsNotAsyncArg) { if (method.ArgsType.Length > 0 && method.ArgsType.Length == (argcount + 1)) { args = new object[method.ArgsType.Length]; args[0] = this; int x = 1; for (int i = 0; i < (method.ArgsType.Length - 1); i++) { x = i + 1; args[x] = Serialization.UnpackSingleObject(method.ArgsType[x], pack.Arguments[i]); } } if (method.IsAsync) { if (!method.IsRet) { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, false); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.ExceptionOut = this.ExceptionOut; AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); _calls_.Run(); } else { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, true); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.Complete += ResrunResultData; _calls_.ExceptionOut = this.ExceptionOut; AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); _calls_.Run(); } } else //SYNC { if (!method.IsRet) { method.MethodInfo.Invoke(implement, args); } else { try { object res = method.MethodInfo.Invoke(implement, args); if (res != null) { if (res is Result result) { result.Id = pack.Id; ResrunResultData(result); } else { Result tmp = new Result(res) { Id = pack.Id }; ResrunResultData(tmp); } } } catch (Exception er) { var tmp = base.GetExceptionResult(er, pack.Id); ResrunResultData(tmp); if (PushException(er)) { Log.Error($"Cmd:{pack.CmdTag} ERROR:{er}"); } } } } } else { if (method.ArgsType.Length > 0 && method.ArgsType.Length == argcount) { args = new object[method.ArgsType.Length]; for (int i = 0; i < method.ArgsType.Length; i++) { args[i] = Serialization.UnpackSingleObject(method.ArgsType[i], pack.Arguments[i]); } } if (method.IsAsync) { if (!method.IsRet) { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, false); _calls_.CallSend += SendData; _calls_.ExceptionOut = this.ExceptionOut; AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); _calls_.Run(); } else { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, implement, method.MethodInfo, args, true); _calls_.CallSend += SendData; _calls_.Complete += ResrunResultData; _calls_.ExceptionOut = this.ExceptionOut; AsyncCallDict.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); _calls_.Run(); } } else //SYNC { if (!method.IsRet) { method.MethodInfo.Invoke(implement, args); } else { try { object res = method.MethodInfo.Invoke(implement, args); if (res != null) { if (res is Result result) { result.Id = pack.Id; ResrunResultData(result); } else { Result tmp = new Result(res) { Id = pack.Id }; ResrunResultData(tmp); } } } catch (Exception er) { var tmp = base.GetExceptionResult(er, pack.Id); ResrunResultData(tmp); if (PushException(er)) { Log.Error($"Cmd:{pack.CmdTag} ERROR:{er}"); } } } } } } else { Log.Error($"Clent Call Cmd:{pack.CmdTag} Not Find"); } }
private void DataOn(byte[] data) { ReadBytes read = null; if (CurrentServer.DecodeingHandler != null) { read = new ReadBytes(data, 4, -1, CurrentServer.DecodeingHandler); } else { read = new ReadBytes(data); } if (read.ReadInt32(out int length) && read.ReadInt32(out int cmd) && read.Length == length) { switch (cmd) { case CmdDef.CallCmd: { CallPack call = new CallPack(); try { call.Id = read.ReadInt64(); call.CmdTag = read.ReadInt32(); call.Arguments = new List <byte[]>(); var lengt = read.ReadInt32(); for (int i = 0; i < lengt; i++) { call.Arguments.Add(read.ReadByteArray()); } CallPackRun(call); } catch (Exception er) { if (PushException(new CallException(er.Message, (int)ErrorTag.CallErr, er))) { Log.Error($"CMD:{call.CmdTag} Error:\r\n{er}"); } } } break; case CmdDef.ReturnResult: { Result result = new Result { Id = read.ReadInt64(), ErrorId = read.ReadInt32() }; byte[] strdata = read.ReadByteArray(); if (strdata.Length > 0) { result.ErrorMsg = Encoding.UTF8.GetString(strdata); } var arglengt = read.ReadInt32(); for (int i = 0; i < arglengt; i++) { result.Arguments.Add(read.ReadByteArray()); } if (CallBackDict.ContainsKey(result.Id)) { if (CallBackDict.TryRemove(result.Id, out AsyncCalls call)) { try { call.SetRes(result); } catch (Exception er) { var ermsg = $"Cmd:{call.Cmd} Error:\r\n {er}"; if (PushException(new CallException(ermsg, (int)ErrorTag.CallErr, er))) { Log.Error(ermsg); } } } } } break; } } }
public override ResultAwatier CR(int cmdTag, params object[] args) { CallPack buffer = new CallPack() { Id = Common.MakeID, CmdTag = cmdTag, Arguments = new List <byte[]>(args.Length) }; this.Id = buffer.Id; foreach (var item in args) { Type type = item.GetType(); buffer.Arguments.Add(Serialization.PackSingleObject(type, item)); } using (MemoryStream stream = new MemoryStream()) { BinaryWriter bufflist = new BinaryWriter(stream); if (CCloudClient.EncodingHandler != null) { bufflist.Write(CmdDef.CallCmd); byte[] classdata = BufferFormat.SerializeObject(buffer); bufflist.Write(classdata.Length); bufflist.Write(classdata); byte[] fdata = CCloudClient.EncodingHandler(stream.ToArray()); stream.Position = 0; stream.SetLength(0); bufflist.Write(0); bufflist.Write(fdata); } else { bufflist.Write(0); bufflist.Write(CmdDef.CallCmd); byte[] classdata = BufferFormat.SerializeObject(buffer); bufflist.Write(classdata.Length); bufflist.Write(classdata); } int l = (int)(stream.Length); byte[] data = BufferFormat.GetSocketBytes(l); stream.Position = 0; bufflist.Write(data); byte[] pdata = stream.ToArray(); #if !COREFX stream.Close(); #endif stream.Dispose(); CCloudClient.AddAsyncRunBack(this, buffer.Id); if (CallSend != null) { CallSend(pdata); } } if (awaiter == null) { awaiter = new ResultAwatier(); } return(base.awaiter); }
private void CallPackRun(CallPack pack) { if (Module.ModuleDiy.ContainsKey(pack.CmdTag)) { AsyncMethodDef method = Module.ModuleDiy[pack.CmdTag]; object[] args = null; int argcount = 0; if (pack.Arguments != null) { argcount = pack.Arguments.Count; } if (method.ArgsType.Length > 0 && method.ArgsType.Length == (argcount + 1)) { args = new object[method.ArgsType.Length]; args[0] = this; int x = 1; for (int i = 0; i < (method.ArgsType.Length - 1); i++) { x = i + 1; args[x] = Serialization.UnpackSingleObject(method.ArgsType[x], pack.Arguments[i]); } } if (args == null) { LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount); } if (method.IsAsync) { if (!method.IsOut) { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, false); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } else { AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, true); args[0] = _calls_; _calls_.CallSend += SendData; _calls_.Complete += RetrunResultData; _calls_.Run(); AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_); } } else //SYNC { if (!method.IsOut) { method.methodInfo.Invoke(method.Obj, args); } else { try { object res = method.methodInfo.Invoke(method.Obj, args); if (res != null) { ReturnResult tmp = new ReturnResult(res); tmp.Id = pack.Id; RetrunResultData(tmp); } } catch (Exception er) { ReturnResult tmp = new ReturnResult(); tmp.Id = pack.Id; RetrunResultData(tmp); LogAction.Log(LogType.Err, "Cmd:{0} ERROR:" + er.ToString(), pack.CmdTag); } } } } else { LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} Not Find Cmd", pack.CmdTag); } }
public ResultAwatier CR(int cmdTag, params object[] args) { CallPack buffer = new CallPack() { Id = Common.MakeID, CmdTag = cmdTag, Arguments = new List <byte[]>(args.Length) }; foreach (var item in args) { Type type = item.GetType(); buffer.Arguments.Add(Serialization.PackSingleObject(type, item)); } using (MemoryStream stream = new MemoryStream()) { BinaryWriter bufflist = new BinaryWriter(stream); if (AsyncUser.dataExtra != null) { bufflist.Write(CmdDef.CallCmd); byte[] classdata = BufferFormat.SerializeObject(buffer); bufflist.Write(classdata.Length); bufflist.Write(classdata); byte[] fdata = AsyncUser.dataExtra(stream.ToArray()); stream.Position = 0; stream.SetLength(0); bufflist.Write(0); bufflist.Write(fdata); } else { bufflist.Write(0); bufflist.Write(CmdDef.CallCmd); byte[] classdata = BufferFormat.SerializeObject(buffer); bufflist.Write(classdata.Length); bufflist.Write(classdata); } int l = (int)(stream.Length); byte[] data = BufferFormat.GetSocketBytes(l); stream.Position = 0; bufflist.Write(data); byte[] pdata = stream.ToArray(); #if !COREFX stream.Close(); #endif stream.Dispose(); AsyncUser.AddAsyncCallBack(this, buffer.Id); if (CallSend != null) { CallSend(pdata); } } return(fiber.Read()); }