MessageDispatherComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. [EntityEvent(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. private Dictionary<ushort, Type> opcodeType { get; set; }
  17. private void Awake(AppType appType)
  18. {
  19. this.AppType = appType;
  20. this.Load();
  21. }
  22. private void Load()
  23. {
  24. this.handlers = new Dictionary<ushort, List<IMHandler>>();
  25. this.messageOpcode = new Dictionary<Type, MessageAttribute>();
  26. this.opcodeType = new Dictionary<ushort, Type>();
  27. Assembly[] assemblies = Game.EntityEventManager.GetAssemblies();
  28. foreach (Assembly assembly in assemblies)
  29. {
  30. Type[] types = assembly.GetTypes();
  31. foreach (Type type in types)
  32. {
  33. object[] attrs = type.GetCustomAttributes(typeof (MessageAttribute), false);
  34. if (attrs.Length == 0)
  35. {
  36. continue;
  37. }
  38. MessageAttribute messageAttribute = (MessageAttribute) attrs[0];
  39. this.messageOpcode[type] = messageAttribute;
  40. this.opcodeType[messageAttribute.Opcode] = type;
  41. }
  42. }
  43. foreach (Assembly assembly in assemblies)
  44. {
  45. Type[] types = assembly.GetTypes();
  46. foreach (Type type in types)
  47. {
  48. object[] attrs = type.GetCustomAttributes(typeof (MessageHandlerAttribute), false);
  49. if (attrs.Length == 0)
  50. {
  51. continue;
  52. }
  53. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute) attrs[0];
  54. if (!messageHandlerAttribute.Type.Is(this.AppType))
  55. {
  56. continue;
  57. }
  58. object obj = Activator.CreateInstance(type);
  59. IMHandler imHandler = obj as IMHandler;
  60. if (imHandler == null)
  61. {
  62. throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
  63. }
  64. Type messageType = imHandler.GetMessageType();
  65. ushort opcode = this.GetOpcode(messageType);
  66. List<IMHandler> list;
  67. if (!this.handlers.TryGetValue(opcode, out list))
  68. {
  69. list = new List<IMHandler>();
  70. this.handlers.Add(opcode, list);
  71. }
  72. list.Add(imHandler);
  73. }
  74. }
  75. }
  76. public ushort GetOpcode(Type type)
  77. {
  78. MessageAttribute messageAttribute;
  79. if (!this.messageOpcode.TryGetValue(type, out messageAttribute))
  80. {
  81. throw new Exception($"查找Opcode失败: {type.Name}");
  82. }
  83. return messageAttribute.Opcode;
  84. }
  85. public Type GetType(ushort opcode)
  86. {
  87. Type messageType;
  88. if (!this.opcodeType.TryGetValue(opcode, out messageType))
  89. {
  90. throw new Exception($"查找Opcode Type失败: {opcode}");
  91. }
  92. return messageType;
  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. Type messageType = this.GetType(messageInfo.Opcode);
  103. object message = MongoHelper.FromBson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  104. messageInfo.Message = message;
  105. foreach (IMHandler ev in actions)
  106. {
  107. try
  108. {
  109. ev.Handle(session, messageInfo);
  110. }
  111. catch (Exception e)
  112. {
  113. Log.Error(e.ToString());
  114. }
  115. }
  116. }
  117. public override void Dispose()
  118. {
  119. if (this.Id == 0)
  120. {
  121. return;
  122. }
  123. base.Dispose();
  124. }
  125. }
  126. }