MessageDispatherComponent.cs 3.2 KB

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