public static void Load(this MailboxDispatcherComponent self) { AppType appType = StartConfigComponent.Instance.StartConfig.AppType; self.MailboxHandlers.Clear(); //获取所有加了MailboxHandler的类型 List <Type> types = Game.EventSystem.GetTypes(typeof(MailboxHandlerAttribute)); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(MailboxHandlerAttribute), false); if (attrs.Length == 0) { continue; } //如果类型跟我这个应用的不一致 就跳过 MailboxHandlerAttribute mailboxHandlerAttribute = (MailboxHandlerAttribute)attrs[0]; if (!mailboxHandlerAttribute.Type.Is(appType)) { continue; } //创建跟这个appType对应的mailbox实例 object obj = Activator.CreateInstance(type); IMailboxHandler iMailboxHandler = obj as IMailboxHandler; if (iMailboxHandler == null) { throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}"); } //缓存起来 self.MailboxHandlers.Add(mailboxHandlerAttribute.MailboxType, iMailboxHandler); } }
public static async ETVoid HandleAsync(this MailBoxComponent self) { MailboxDispatcherComponent mailboxDispatcherComponent = Game.Scene.GetComponent <MailboxDispatcherComponent>(); long instanceId = self.InstanceId; while (true) { if (self.InstanceId != instanceId) { return; } try { ActorMessageInfo info = await self.GetAsync(); // 返回null表示actor已经删除,协程要终止 if (info.Message == null) { return; } // 根据这个mailbox类型分发给相应的处理 await mailboxDispatcherComponent.Handle(self, info); } catch (Exception e) { Log.Error(e); } } }
/// <summary> /// 根据mailbox类型做不同的处理 /// </summary> public static async ETTask Handle( this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo) { IMailboxHandler iMailboxHandler; if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler)) { await iMailboxHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message); } }
/// <summary> /// 根据mailbox类型做不同的处理,有两种类型gate session类型的Mailbox,消息分发类型的Mailbox,对mailbox中的消息进行分发处理 /// </summary> public static async ETTask Handle( this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, Session session, object message) { IMailboxHandler iMailboxHandler; if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler)) { await iMailboxHandler.Handle(session, mailBoxComponent.Entity, message); } }
/// <summary> /// 根据mailbox类型做不同的处理 /// </summary> public static async ETTask Handle(this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, Session session, object message) { IMailboxHandler iMailboxHandler; //根据邮箱类型 获取邮箱... if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler)) { //交由邮箱处理请求 邮箱是挂载在会话实体上的...外网的会话实体 await iMailboxHandler.Handle(session, mailBoxComponent.Entity, message); } }
public static void Awake(this MailboxDispatcherComponent self) { self.Load(); }
public static async ETTask Add(this MailBoxComponent self, Session session, object message) { //获取到邮箱分发组件 进行处理 MailboxDispatcherComponent mailboxDispatcherComponent = Game.Scene.GetComponent <MailboxDispatcherComponent>(); await mailboxDispatcherComponent.Handle(self, session, message); }