MessageDispatherComponent.cs 3.1 KB

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