Exemple #1
0
 public static void RegisterHandler(this MessageDispatherComponent self, ushort opcode, IMHandler handler)
 {
     if (!self.Handlers.ContainsKey(opcode))
     {
         self.Handlers.Add(opcode, new List <IMHandler>());
     }
     self.Handlers[opcode].Add(handler);
 }
        public void Load()
        {
            this.handlers.Clear();

            ETModel.MessageDispatherComponent messageDispatherComponent = ETModel.Game.Scene.GetComponent <ETModel.MessageDispatherComponent>();
            ETModel.OpcodeTypeComponent       opcodeTypeComponent       = ETModel.Game.Scene.GetComponent <ETModel.OpcodeTypeComponent>();

            Type[] types = ETModel.Game.Hotfix.GetHotfixTypes();

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }

                Type   messageType = iMHandler.GetMessageType();
                ushort opcode      = this.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode != 0)
                {
                    this.RegisterHandler(opcode, iMHandler);
                }

                // 尝试注册到mono层
                if (messageDispatherComponent != null && opcodeTypeComponent != null)
                {
                    ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType);
                    if (monoOpcode == 0)
                    {
                        continue;
                    }

                    MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
                    messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy);
                }
            }
        }
Exemple #3
0
        public static void Handle(this MessageDispatherComponent self, Session session, MessageInfo messageInfo)
        {
            List <IMHandler> actions;

            if (!self.Handlers.TryGetValue(messageInfo.Opcode, out actions))
            {
                Log.Error($"消息没有处理: {messageInfo.Opcode} {JsonHelper.ToJson(messageInfo.Message)}");
                return;
            }

            foreach (IMHandler ev in actions)
            {
                try
                {
                    ev.Handle(session, messageInfo.Message);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }
        }
Exemple #4
0
        public static void Load(this MessageDispatherComponent self)
        {
            self.Handlers.Clear();

            AppType appType = Game.Scene.GetComponent <StartConfigComponent>().StartConfig.AppType;

            Type[] types = DllHelper.GetMonoTypes();

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
                if (!messageHandlerAttribute.Type.Is(appType))
                {
                    continue;
                }

                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }

                Type   messageType = iMHandler.GetMessageType();
                ushort opcode      = Game.Scene.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode == 0)
                {
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }
                self.RegisterHandler(opcode, iMHandler);
            }
        }