MessageDispatherComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Base;
  5. namespace Model
  6. {
  7. /// <summary>
  8. /// 消息分发组件
  9. /// </summary>
  10. [DisposerEvent(typeof(MessageDispatherComponent))]
  11. public class MessageDispatherComponent: Component
  12. {
  13. private AppType AppType;
  14. private Dictionary<ushort, List<IMHandler>> handlers;
  15. private Dictionary<Type, MessageAttribute> messageOpcode { get; set; }
  16. public void Awake(AppType appType)
  17. {
  18. this.AppType = appType;
  19. this.Load();
  20. }
  21. public void Load()
  22. {
  23. this.handlers = new Dictionary<ushort, List<IMHandler>>();
  24. this.messageOpcode = new Dictionary<Type, MessageAttribute>();
  25. Assembly[] assemblies = Game.DisposerEventManager.GetAssemblies();
  26. foreach (Assembly assembly in assemblies)
  27. {
  28. Type[] types = assembly.GetTypes();
  29. foreach (Type type in types)
  30. {
  31. object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false);
  32. if (attrs.Length == 0)
  33. {
  34. continue;
  35. }
  36. MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
  37. this.messageOpcode[type] = messageAttribute;
  38. }
  39. }
  40. foreach (Assembly assembly in assemblies)
  41. {
  42. Type[] types = assembly.GetTypes();
  43. foreach (Type type in types)
  44. {
  45. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  46. if (attrs.Length == 0)
  47. {
  48. continue;
  49. }
  50. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  51. if (!messageHandlerAttribute.Type.Is(this.AppType))
  52. {
  53. continue;
  54. }
  55. object obj = Activator.CreateInstance(type);
  56. IMHandler imHandler = obj as IMHandler;
  57. if (imHandler == null)
  58. {
  59. throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
  60. }
  61. Type messageType = imHandler.GetMessageType();
  62. ushort opcode = this.GetOpcode(messageType);
  63. List<IMHandler> list;
  64. if (!this.handlers.TryGetValue(opcode, out list))
  65. {
  66. list = new List<IMHandler>();
  67. this.handlers.Add(opcode, list);
  68. }
  69. list.Add(imHandler);
  70. }
  71. }
  72. }
  73. public ushort GetOpcode(Type type)
  74. {
  75. MessageAttribute messageAttribute;
  76. if (!this.messageOpcode.TryGetValue(type, out messageAttribute))
  77. {
  78. throw new Exception($"查找Opcode失败: {type.Name}");
  79. }
  80. return messageAttribute.Opcode;
  81. }
  82. public void Handle(Session session, MessageInfo messageInfo)
  83. {
  84. List<IMHandler> actions;
  85. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  86. {
  87. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  88. return;
  89. }
  90. foreach (IMHandler ev in actions)
  91. {
  92. try
  93. {
  94. ev.Handle(session, messageInfo);
  95. }
  96. catch (Exception e)
  97. {
  98. Log.Error(e.ToString());
  99. }
  100. }
  101. }
  102. public override void Dispose()
  103. {
  104. if (this.Id == 0)
  105. {
  106. return;
  107. }
  108. base.Dispose();
  109. }
  110. }
  111. }