public async Task Handle(Session session, Entity entity, IActorLanuch actorLaunch) { Message msg = actorLaunch as Message; if (msg == null) { Log.Error($"消息类型转换错误: {actorLaunch.GetType().FullName} to {typeof (Message).Name}"); return; } E e = entity as E; if (e == null) { Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}"); return; } int rpcId = actorLaunch.RpcId; ActorResponse response = new ActorResponse { RpcId = rpcId }; session.Reply(response); await this.Run(e, msg); }
/// <summary> /// 根据actor消息分发给ActorMessageHandler处理 /// </summary> public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorLanuch actorLaunch) { if (!self.ActorMessageHandlers.TryGetValue(actorLaunch.GetType(), out IMActorHandler handler)) { throw new Exception($"not found message handler: {MongoHelper.ToJson(actorLaunch)}"); } await handler.Handle(session, entity, actorLaunch); }
public async Task Handle(Session session, Entity entity, IActorLanuch actorLaunch) { try { Request request = actorLaunch as Request; if (request == null) { Log.Error($"消息类型转换错误: {actorLaunch.GetType().FullName} to {typeof (Request).Name}"); return; } E e = entity as E; if (e == null) { Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}"); return; } int rpcId = request.RpcId; long instanceId = session.InstanceId; await this.Run(e, request, response => { // 等回调回来,session可以已经断开了,所以需要判断session InstanceId是否一样 if (session.InstanceId != instanceId) { return; } response.RpcId = rpcId; session.Reply(response); }); } catch (Exception e) { throw new Exception($"解释消息失败: {actorLaunch.GetType().FullName}", e); } }