MessageDispatherComponent.cs 3.1 KB

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