public static void Load(this ActorMessageDispatcherComponent self)
        {
            AppType appType = StartConfigComponent.Instance.StartConfig.AppType;

            self.ActorMessageHandlers.Clear();

            HashSet <Type> types = Game.EventSystem.GetTypes(typeof(ActorMessageHandlerAttribute));

            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();
                self.ActorMessageHandlers.Add(messageType, imHandler);
            }
        }
        /// <summary>
        /// 分发actor消息
        /// </summary>
        public static async ETTask Handle(
            this ActorMessageDispatcherComponent self, Entity entity, ActorMessageInfo actorMessageInfo)
        {
            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, entity, actorMessageInfo.Message);
        }
コード例 #3
0
        /// <summary>
        /// 分发actor消息
        /// </summary>
        public static async ETTask Handle(
            this ActorMessageDispatcherComponent self, Entity entity, Session session, object message)
        {
            if (!self.ActorMessageHandlers.TryGetValue(message.GetType(), out IMActorHandler handler))
            {
                throw new Exception($"not found message handler: {message}");
            }

            await handler.Handle(session, entity, message);
        }
 /// <summary>
 /// 分发actor消息
 /// </summary>
 public static async ETTask Handle(this ActorMessageDispatcherComponent self, Entity entity, ActorMessageInfo actorMessageInfo)
 {
     //根据消息类型 获取已缓存的 用于处理Actor消息的实例 如果没有则抛出异常
     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, entity, actorMessageInfo.Message);
 }
コード例 #5
0
        public static void Load(this ActorMessageDispatcherComponent self)
        {
            self.ActorMessageHandlers.Clear();

            HashSet <Type> types = Game.EventSystem.GetTypes(typeof(ActorMessageHandlerAttribute));

            foreach (Type type in types)
            {
                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);
            }
        }
 public static void Awake(this ActorMessageDispatcherComponent self)
 {
     self.Load();
 }