/// <summary> /// 注册命令 /// </summary> /// <param name="cmd">命令</param> /// <param name="instanceType">实例类型</param> /// <param name="methodInfo">方法</param> private void IsRegisterCmd(object instance, int cmd, Type instanceType, MethodInfo methodInfo) { if (IsTypeOfBaseTypeIs(methodInfo.ReturnType, typeof(Task)) || methodInfo.ReturnType == typeof(void) || methodInfo.ReturnType == null) { var sr = new InstanceRegister(instance, instanceType, methodInfo); if (!MethodInstanceDict.ContainsKey(cmd)) { Log.InfoFormat("Add cmd:{cmd} to {sr}", cmd, sr); MethodInstanceDict.Add(cmd, sr); } else { Log.InfoFormat("Replace cmd:{cmd} to {sr}", cmd, sr); MethodInstanceDict[cmd] = sr; } } else { Log.ErrorFormat("RegisterService Return Type {Name} Err,Use void, Task or Task<T>", methodInfo.Name); } }
private async void RunCall(InstanceRegister service, int cmd, long id, byte runType, object[] args) { try { switch (service.ReturnMode) { case ReturnTypeMode.Null: if (runType == 0) { if (service.Instance is IMethodController controller) { controller.Current = this; } service.Method.Execute(service.Instance, args); return; } break; case ReturnTypeMode.Task: if (runType == 1) { if (service.Instance is IMethodController controller) { controller.Current = this; } await service.Method.ExecuteAsync(service.Instance, args); await SendResult(id); return; } break; case ReturnTypeMode.TaskValue: if (runType == 2) { if (service.Instance is IMethodController controller) { controller.Current = this; } var ret_value = (object)await service.Method.ExecuteAsync(service.Instance, args); switch (ret_value) { case Result result: { result.Id = id; await SendResult(result); return; } default: { await SendResult(id, ret_value); return; } } } break; } await SendNotRunType(service, id, runType); } catch (System.Net.Sockets.SocketException) { } catch (NetxException er) { Log.Error(er); await SendError(id, $"Client Method Tag:{cmd} Call Err:{er.Message}", ErrorType.CallErr); } catch (Exception er) { Log.Error(er); await SendError(id, $"Client Method Tag:{cmd} Call Err:{er.Message}", ErrorType.CallErr); } }