MessageDispatherComponent.cs 3.4 KB

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