| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using Base;
- using Object = Base.Object;
- namespace Model
- {
- [ObjectEvent]
- public class MessageHandlerComponentEvent : ObjectEvent<MessageDispatherComponent>, ILoader, IAwake<AppType>
- {
- public void Load()
- {
- this.GetValue().Load();
- }
- public void Awake(AppType appType)
- {
- this.GetValue().Awake(appType);
- }
- }
-
- /// <summary>
- /// 消息分发组件
- /// </summary>
- public class MessageDispatherComponent: Component
- {
- private AppType AppType;
- private Dictionary<ushort, List<IMHandler>> handlers;
- private Dictionary<Type, MessageAttribute> messageOpcode { get; set; }
-
- public void Awake(AppType appType)
- {
- this.AppType = appType;
- this.Load();
- }
- public void Load()
- {
- this.handlers = new Dictionary<ushort, List<IMHandler>>();
- this.messageOpcode = new Dictionary<Type, MessageAttribute>();
- Assembly[] assemblies = Object.ObjectManager.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- Type[] types = assembly.GetTypes();
- 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;
- }
- }
- foreach (Assembly assembly in assemblies)
- {
- Type[] types = assembly.GetTypes();
- 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);
- }
- }
- }
- public ushort GetOpcode(Type type)
- {
- MessageAttribute messageAttribute;
- if (!this.messageOpcode.TryGetValue(type, out messageAttribute))
- {
- throw new Exception($"查找Opcode失败: {type.Name}");
- }
- return messageAttribute.Opcode;
- }
- public void Handle(Session session, MessageInfo messageInfo)
- {
- List<IMHandler> actions;
- if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
- {
- Log.Error($"消息 {messageInfo.Opcode} 没有处理");
- return;
- }
- foreach (IMHandler ev in actions)
- {
- try
- {
- ev.Handle(session, messageInfo);
- }
- catch (Exception e)
- {
- Log.Error(e.ToString());
- }
- }
- }
-
- public override void Dispose()
- {
- if (this.Id == 0)
- {
- return;
- }
- base.Dispose();
- }
- }
- }
|