/// <summary> /// 在初始化后会 Start里面会自动调用 /// </summary> /// <param name="self"></param> public static async void HandleAsync(this MailBoxComponent self) { ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent <ActorMessageDispatherComponent>(); long instanceId = self.InstanceId; while (true) { if (self.InstanceId != instanceId) { return; } try { ActorMessageInfo info = await self.GetAsync(); // 返回null表示actor已经删除,协程要终止 if (info.Message == null) { return; } // 根据这个actor的类型分发给相应的ActorHandler处理 await actorMessageDispatherComponent.Handle(self, info); } catch (Exception e) { Log.Error(e); } } }
public static async void HandleAsync(this ActorComponent self) { ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent <ActorMessageDispatherComponent>(); while (true) { if (self.IsDisposed) { return; } try { ActorMessageInfo info = await self.GetAsync(); // 返回null表示actor已经删除,协程要终止 if (info.Message == null) { return; } // 根据这个actor的类型分发给相应的ActorHandler处理 await actorMessageDispatherComponent.ActorTypeHandle(self.ActorType, (Entity)self.Parent, info); } catch (Exception e) { Log.Error(e); } } }
public static void Load(this ActorMessageDispatherComponent self) { AppType appType = self.Entity.GetComponent <StartConfigComponent>().StartConfig.AppType; self.ActorMessageHandlers.Clear(); self.ActorTypeHandlers.Clear(); Type[] types = DllHelper.GetMonoTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ActorTypeHandlerAttribute), false); if (attrs.Length == 0) { continue; } ActorTypeHandlerAttribute actorTypeHandlerAttribute = (ActorTypeHandlerAttribute)attrs[0]; if (!actorTypeHandlerAttribute.appType.Is(appType)) { continue; } object obj = Activator.CreateInstance(type); IActorTypeHandler iActorTypeHandler = obj as IActorTypeHandler; if (iActorTypeHandler == null) { throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}"); } self.ActorTypeHandlers.Add(actorTypeHandlerAttribute.actorType, iActorTypeHandler); } foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false); if (attrs.Length == 0) { continue; } ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute)attrs[0]; if (!messageHandlerAttribute.appType.Is(appType)) { continue; } object obj = Activator.CreateInstance(type); IMActorHandler imHandler = obj as IMActorHandler; if (imHandler == null) { throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}"); } Type messageType = imHandler.GetMessageType(); self.ActorMessageHandlers.Add(messageType, imHandler); } }
/// <summary> /// 根据actor消息分发给ActorMessageHandler处理 /// </summary> public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorMessage actorRequest) { if (!self.ActorMessageHandlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler)) { throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}"); } await handler.Handle(session, entity, actorRequest); }
/// <summary> /// 根据actor消息分发给ActorMessageHandler处理 /// </summary> public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorMessage actorRequest) { if (!self.ActorMessageHandlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler)) { throw new Exception($"not found message handler: {Newtonsoft.Json.JsonConvert.SerializeObject(actorRequest)}"); } await handler.Handle(session, entity, actorRequest); }
/// <summary> /// 一个actor收到的所有消息先由其指定的ActorTypeHandle处理 /// </summary> public static async Task ActorTypeHandle( this ActorMessageDispatherComponent self, string actorType, Entity entity, ActorMessageInfo actorMessageInfo) { IActorTypeHandler iActorTypeHandler; if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler)) { throw new Exception($"not found actortype handler: {actorType}"); } await iActorTypeHandler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message); }
public static async Task Handle( this ActorMessageDispatherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo) { // 有拦截器使用拦截器处理 IActorInterceptTypeHandler iActorInterceptTypeHandler; if (self.ActorTypeHandlers.TryGetValue(mailBoxComponent.ActorInterceptType, out iActorInterceptTypeHandler)) { await iActorInterceptTypeHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message); return; } // 没有拦截器就用IMActorHandler处理 if (!self.ActorMessageHandlers.TryGetValue(actorMessageInfo.Message.GetType(), out IMActorHandler handler)) { throw new Exception($"not found message handler: {MongoHelper.ToJson(actorMessageInfo.Message)}"); } await handler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message); }
public static void Awake(this ActorMessageDispatherComponent self) { self.Load(); }
/// <summary> /// Awake()时候会被执行 /// </summary> /// <param name="self"></param> public static void Load(this ActorMessageDispatherComponent self) { //得到服务器类型 AppType appType = StartConfigComponent.Instance.StartConfig.AppType; self.ActorMessageHandlers.Clear(); //清空ActorMessage self.ActorTypeHandlers.Clear(); //清空Actor处理函数 List <Type> types = Game.EventSystem.GetTypes(typeof(ActorInterceptTypeHandlerAttribute)); foreach (Type type in types) //加载所有的Actor处理函数到字典中 { object[] attrs = type.GetCustomAttributes(typeof(ActorInterceptTypeHandlerAttribute), false); if (attrs.Length == 0) { continue; } ActorInterceptTypeHandlerAttribute actorInterceptTypeHandlerAttribute = (ActorInterceptTypeHandlerAttribute)attrs[0]; if (!actorInterceptTypeHandlerAttribute.Type.Is(appType)) //每个服务器只会加载自身的actor相关类 方法 { continue; } object obj = Activator.CreateInstance(type); IActorInterceptTypeHandler iActorInterceptTypeHandler = obj as IActorInterceptTypeHandler; if (iActorInterceptTypeHandler == null) { throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}"); } //直接把传参ActorType作为key使用 self.ActorTypeHandlers.Add(actorInterceptTypeHandlerAttribute.ActorType, iActorInterceptTypeHandler); } types = Game.EventSystem.GetTypes(typeof(ActorMessageHandlerAttribute)); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false); if (attrs.Length == 0) { continue; } ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute)attrs[0]; if (!messageHandlerAttribute.Type.Is(appType)) { continue; } object obj = Activator.CreateInstance(type); IMActorHandler imHandler = obj as IMActorHandler; if (imHandler == null) { throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}"); } Type messageType = imHandler.GetMessageType(); //获取消息的类型作为key self.ActorMessageHandlers.Add(messageType, imHandler); } }