/// <summary> /// 执行方法 /// </summary> /// <param name="invocation"></param> /// <param name="session"></param> /// <returns></returns> public object ImplementFunc(IInvocation invocation, RRPCSession session) { RequestExecutiveInformation information = new RequestExecutiveInformation() { AssemblyFullName = invocation.Method.DeclaringType.Assembly.FullName, FullName = invocation.Method.DeclaringType.FullName, ID = Guid.NewGuid(), MethodName = invocation.Method.Name, Arguments = invocation.Arguments.Select(d => JsonConvert.SerializeObject(d)).ToList() }; var result = session.RemoteCallQueue.AddTaskQueue(information.ID, information, session); session.RemoteCallQueue.RemoteExecutionFunc(result); AOPFilterEntity filterType = null; if (TransformationSkipType.TryGetValue(invocation.Method.ReturnType.FullName, out var value)) { filterType = value.Invoke(); if (filterType.IsReturn) { return(filterType.Result); } } result.WaitHandle.WaitOne(); switch (result.State) { case ReceiveMessageState.Wait: throw new Exception("任务出现错误,目前正在等待状态,却通过了健康检查"); case ReceiveMessageState.Success: if (filterType != null && filterType.IsReplaceResult) { return(filterType.Result); } else { var obj = JsonConvert.DeserializeObject(result.ReturnValue, invocation.Method.ReturnType); return(obj); } case ReceiveMessageState.Overtime: throw new Exception("任务超时:" + result.ReturnValue); case ReceiveMessageState.Error: throw new Exception("任务出现了异常:" + result.ReturnValue); case ReceiveMessageState.Other: throw new Exception("不存在的任务"); default: break; } throw new Exception("任务出现了异常:它没有按规矩走"); }
/// <summary> /// 查询转发的请求 /// </summary> /// <param name="info">请求信息</param> /// <param name="session">查询到的session</param> /// <param name="RRPCServers">服务列表</param> /// <param name="ExcludeSessID">排除的ID</param> /// <returns></returns> public bool GetService(RequestExecutiveInformation info, List <RRPCServer> RRPCServers, string ExcludeSessID, out RRPCSession session) { ForwardingRequestEnity value; if (ForwardingRequestunity.TryGetValue(info.FullName, out value) || ForwardingRequestunity.TryGetValue(info.AssemblyFullName, out value)) { var rrpcServer = value.SelectRRPCServer(RRPCServers); if (rrpcServer == null) { RRPCServer.RRPCServerList.FirstOrDefault().Value.Log($"转发请求失败{info.FullName} 无法找到指定的RRPCServer 没有找到转发该请求的服务配置", LoggerType.Error); session = null; return(false);; } var count = rrpcServer.GetAllSessions().Count(); if (count == 0) { rrpcServer.Log($"转发请求失败{info.FullName} 由于{rrpcServer.Config.Name} 连接客户端为0", LoggerType.Error); session = null; return(false); } return(SelectSession(info, rrpcServer, rrpcServer.GetAllSessions(), value, 0, ExcludeSessID, out session)); } else { session = null; return(false); } }
/// <summary> /// 选择Session /// </summary> /// <param name="sessions">Session列表</param> /// <param name="info">请求信息</param> /// <param name="server">选中的Server服务</param> /// <param name="session">匹配到的Session</param> /// <param name="value">保存的配置</param> /// <param name="ExcludeSessID">排除的ID</param> /// <param name="state">0:优先SelectRRPCSession 1:执行指定sessionID选择 2:随机选择 其他:记录日志没有找到合适的session</param> private bool SelectSession(RequestExecutiveInformation info, RRPCServer server, IEnumerable <RRPCSession> sessions, ForwardingRequestEnity value, int state, string ExcludeSessID, out RRPCSession session) { switch (state) { case 0: session = value.SelectRRPCSession?.Invoke(sessions); if (session == null) { return(SelectSession(info, server, sessions, value, state + 1, ExcludeSessID, out session)); } return(true); case 1: session = sessions.FirstOrDefault(d => d.SessionID.Equals(info.RRPCSessionID?.ToString())); if (session == null) { return(SelectSession(info, server, sessions, value, state + 1, ExcludeSessID, out session)); } return(true); case 2: session = sessions.Where(d => !d.SessionID.Equals(ExcludeSessID)).OrderBy(d => Guid.NewGuid()).FirstOrDefault(); if (session == null) { return(SelectSession(info, server, sessions, value, state + 1, ExcludeSessID, out session)); } return(true); default: server.Log("没有找到匹配的Session 本次请求作废" + JsonConvert.SerializeObject(info)); session = null; return(false); } }
/// <summary> /// 获取远程类 /// </summary> /// <typeparam name="T">对象</typeparam> /// <param name="session">需要通讯的对象</param> /// <returns></returns> public T GetServices <T>(RRPCSession session) where T : class { return(generator.CreateInterfaceProxyWithoutTarget <T>(new AOPRPCInterceptor((invocation) => { return ImplementFunc(invocation, session); }))); }
/// <summary> /// 获取远程类 /// </summary> /// <param name="session">需要通讯的对象</param> /// <param name="serverType">需要实例化的类型</param> /// <returns></returns> public object GetServices(RRPCSession session, Type serverType) { return(generator.CreateInterfaceProxyWithoutTarget(serverType, new AOPRPCInterceptor((invocation) => { return ImplementFunc(invocation, session); }))); }
/// <summary> /// 任务存储 /// </summary> /// <param name="id">任务ID</param> /// <param name="taskInfo">任务信息</param> /// <param name="state">状态</param> /// <param name="expirationTime">过期时间</param> /// <param name="requestClient">请求对象</param> /// <param name="giveClient">转发对象</param> public ForwardingRequestEntity(Guid id, RequestExecutiveInformation taskInfo, ReceiveMessageState state, DateTime expirationTime, RRPCSession requestClient, RRPCSession giveClient) { this.ID = id; this.TaskInfo = taskInfo; this.State = state; this.ExpirationTime = expirationTime; this.RequestClient = requestClient; this.GiveClient = giveClient; }