コード例 #1
0
 //注册 无非就是压入字典而已
 public void RegisterHandler(ushort opcode, IMHandler handler)
 {
     //因为同一个操作码 可能被多方监听 所以value是以list数据结构进行设计
     //如果字典不包含操作码  就直接new list,然后都要往这个list里压入
     if (!this.handlers.ContainsKey(opcode))
     {
         this.handlers.Add(opcode, new List <IMHandler>());
     }
     this.handlers[opcode].Add(handler);
 }
コード例 #2
0
        private void Load()
        {
            this.handlers      = new Dictionary <ushort, List <IMHandler> >();
            this.messageOpcode = new Dictionary <Type, MessageAttribute>();
            this.opcodeType    = new Dictionary <ushort, Type>();

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

                MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
                this.messageOpcode[type] = messageAttribute;
                this.opcodeType[messageAttribute.Opcode] = type;
            }

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

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

                object obj = Activator.CreateInstance(type);

                IMHandler imHandler = obj as IMHandler;
                if (imHandler == null)
                {
                    throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
                }

                Type             messageType = imHandler.GetMessageType();
                ushort           opcode      = this.GetOpcode(messageType);
                List <IMHandler> list;
                if (!this.handlers.TryGetValue(opcode, out list))
                {
                    list = new List <IMHandler>();
                    this.handlers.Add(opcode, list);
                }
                list.Add(imHandler);
            }
        }
コード例 #3
0
        public void Load()
        {
            this.handlers.Clear();

            //ETModel.MessageDispatcherComponent messageDispatcherComponent = ETModel.Game.Scene.GetComponent<ETModel.MessageDispatcherComponent>();
            //ETModel.OpcodeTypeComponent opcodeTypeComponent = ETModel.Game.Scene.GetComponent<ETModel.OpcodeTypeComponent>();


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

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

                MessageHandlerAttribute attribute = attrs[0] as MessageHandlerAttribute;
                int opcode = attribute.Opcode;
                if (opcode != 0)
                {
                    this.RegisterHandler(opcode, iMHandler);
                    this.typeMessages.Add(opcode, Activator.CreateInstance(messageType));
                }

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

                //	MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
                //	messageDispatcherComponent.RegisterHandler(monoOpcode, messageProxy);
                //}
            }
        }
コード例 #4
0
        public void Load()
        {
            this.handlers.Clear();

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

            Type[] types = DllHelper.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);
                }
            }
        }
コード例 #5
0
        public void Load()
        {
            handlers = new Dictionary <Opcode, List <IMessageMethod> >();

            Type[] types = DllHelper.GetMonoTypes();

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }
                MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
                IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
                if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
                {
                    this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List <IMessageMethod>());
                }
                this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
            }

            // hotfix dll
            Type[] hotfixTypes = DllHelper.GetHotfixTypes();
            foreach (Type type in hotfixTypes)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }
                MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
#if ILRuntime
                IMessageMethod iMessageMethod = new IMessageILMethod(type, "Handle");
#else
                IMHandler      iMHandler      = (IMHandler)Activator.CreateInstance(type);
                IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
#endif
                if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
                {
                    this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List <IMessageMethod>());
                }
                this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(iMessageMethod);
            }
        }
コード例 #6
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);
            }
        }
コード例 #7
0
        public void Load()
        {
            this.handlers = new Dictionary <ushort, List <IMHandler> >();

            Type[] types = DllHelper.GetHotfixTypes();
            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }
                MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
                IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
                if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
                {
                    this.handlers.Add(messageHandlerAttribute.Opcode, new List <IMHandler>());
                }
                this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler);
            }
        }
コード例 #8
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);
            }
        }
コード例 #9
0
        public void Load()
        {
            AppType appType = this.Parent.GetComponent <StartConfigComponent>().StartConfig.AppType;

            this.handlers = new Dictionary <Type, List <IMHandler> >();

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

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

                object obj = Activator.CreateInstance(type);

                IMHandler imHandler = obj as IMHandler;
                if (imHandler == null)
                {
                    throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
                }

                Type messageType = imHandler.GetMessageType();
                if (!this.handlers.TryGetValue(messageType, out List <IMHandler> list))
                {
                    list = new List <IMHandler>();
                    this.handlers.Add(messageType, list);
                }
                list.Add(imHandler);
            }
        }
コード例 #10
0
        //加载
        public void Load()
        {
            this.handlers.Clear();
            //先获取开发时候写的 所有用于处理网络消息的类
            //加了MessageHandlerAttribute特性的
            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;
                }
                //并且是直接或间接继承自IMHandler接口的
                //其中AMHanlde就是继承自IMHandle,所以继承自AMHandler的类也可以转化为IMHandler
                //那么iMHandler就不会等于null了.
                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }
                //获取消息类型
                Type messageType = iMHandler.GetMessageType();
                //根据类型获取到操作码/协议号 opcode
                ushort opcode = this.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode == 0)
                {
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }
                //注册存储处理的方法,key:操作码 value:实例
                //后面调用Handle的时候就可以通过key找到实例,然后进行调用
                this.RegisterHandler(opcode, iMHandler);
            }
        }
コード例 #11
0
        public void Awake(AppType appType)
        {
            this.AppType = appType;
            this.handlers.Clear();

            var types = Game.EventSystem.GetTypes();// DllHelper.GetMonoTypes(DllHelper.GetLogicAssembly());

            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(this.AppType))
                {
                    continue;
                }

                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    continue;
                }

                Type   messageType = iMHandler.GetMessageType();
                ushort opcode      = (this.Parent as Entity).GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode == 0)
                {
                    continue;
                }

                this.RegisterHandler(opcode, iMHandler);
            }
        }
コード例 #12
0
        public void Load()
        {
            this.handlers.Clear();

            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(this.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      = this.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode == 0)
                {
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }
                this.RegisterHandler(opcode, iMHandler);
            }
        }
コード例 #13
0
 public static void RegisterHandler(this MessageDispatcherComponent self, ushort opcode, IMHandler handler)
 {
     if (!self.Handlers.ContainsKey(opcode))
     {
         self.Handlers.Add(opcode, new List <IMHandler>());
     }
     self.Handlers[opcode].Add(handler);
 }
コード例 #14
0
 public IMessageMonoMethod(IMHandler iMHandler)
 {
     this.iMHandler = iMHandler;
 }
コード例 #15
0
 public MessageDispatcherInfo(SceneType sceneType, IMHandler imHandler)
 {
     this.SceneType = sceneType;
     this.IMHandler = imHandler;
 }