public static void Load(this MessageDispatcherComponent self)
        {
            self.Handlers.Clear();

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

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

                Type   messageType = iMHandler.GetMessageType();
                ushort opcode      = OpcodeTypeComponent.Instance.GetOpcode(messageType);
                if (opcode == 0)
                {
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }
                self.RegisterHandler(opcode, iMHandler);
            }
        }
Esempio n. 2
0
        public static void Load(this MessageDispatcherComponent self)
        {
            self.Handlers.Clear();
            //获取到配置的appType 在服务器启动的时候有缓存
            AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
            //获取到所有加了MessageHandler特性的类型
            List <Type> types = Game.EventSystem.GetTypes(typeof(MessageHandlerAttribute));

            //对获取到的类型进行遍历
            foreach (Type type in types)
            {            //获取该类型自定义的属性
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {                //如果属性的个数是0 遍历下一个元素
                    continue;
                }
                //如果第一个属性是MessageHandler
                MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
                if (!messageHandlerAttribute.Type.Is(appType))
                {                //判断它的type参数,如果不等于当前的appType 继续遍历下一个元素 如果类型一致 下面要对其进行缓存的
                    continue;
                }
                //根据类型,创建实例实例 判断实例如果继承了IMHandler的接口
                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }
                //通过接口内部的GetMessageType方法 获取到要处理的proto类型 因为我们是继承自AMHanlde 所以实际是获取AMHanlde里的GetMessageType
                Type messageType = iMHandler.GetMessageType();
                //通过类型获取协议号
                ushort opcode = Game.Scene.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode == 0)
                {                //异常 协议号都是自动生成的 不可能为0
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }
                //注册 也就是缓存起来 同时将协议ID与处理的实例关联起来 .... 说简单点就是创建个字典 添加到字典内部去
                self.RegisterHandler(opcode, iMHandler);
            }
        }
Esempio n. 3
0
        public static void Load(this MessageDispatcherComponent self)
        {
            self.Handlers.Clear();

            AppType appType = StartConfigComponent.Instance.StartConfig.AppType;

            List <Type> types = Game.EventSystem.GetTypes(typeof(MessageHandlerAttribute));

            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);
            }
        }