internal void Disconnect(string message) { this.Asyn = null; Stream.Flush(); IsDisconnect = true; DisconnectDateTime = DateTime.Now; if (AsyncCallDict != null) { AsyncCallDict.Clear(); } if (CallBackDict != null) { foreach (var item in CallBackDict.Values) { var disconn = GetExceptionResult("Disconnect", (int)ErrorTag.Disconnect, item.Id); disconn.Arguments = new List <byte[]>(); item.SetRes(disconn); } CallBackDict.Clear(); } UserDisconnect?.Invoke(this, message); UserDisconnect = null; }
internal void RemoveAsyncCall(long id) { if (AsyncCallDict.ContainsKey(id)) { if (!AsyncCallDict.TryRemove(id, out AsyncCalls call)) { Log.Error($"Id:{id} ERROR:\r\nNot TryRemove AsyncCall"); } } }
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"); } }