MessageHandlerComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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<MessageHandlerComponent>, ILoader, IAwake<string>
  10. {
  11. public void Load()
  12. {
  13. this.GetValue().Load();
  14. }
  15. public void Awake(string appType)
  16. {
  17. this.GetValue().Awake(appType);
  18. }
  19. }
  20. /// <summary>
  21. /// 消息分发组件
  22. /// </summary>
  23. public class MessageHandlerComponent: Component, IMessageHandler
  24. {
  25. private string AppType;
  26. private Dictionary<ushort, List<Action<Entity, byte[], int, int>>> events;
  27. public Dictionary<Type, ushort> messageOpcode { get; private set; } = new Dictionary<Type, ushort>();
  28. public void Awake(string appType)
  29. {
  30. this.AppType = appType;
  31. this.Load();
  32. }
  33. public void Load()
  34. {
  35. this.events = new Dictionary<ushort, List<Action<Entity, byte[], int, int>>>();
  36. this.messageOpcode = new Dictionary<Type, ushort>();
  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(OpcodeAttribute), false);
  44. if (attrs.Length == 0)
  45. {
  46. continue;
  47. }
  48. OpcodeAttribute opcodeAttribute = (OpcodeAttribute)attrs[0];
  49. this.messageOpcode[type] = opcodeAttribute.Opcode;
  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(MessageAttribute), false);
  58. if (attrs.Length == 0)
  59. {
  60. continue;
  61. }
  62. MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
  63. if (messageAttribute.AppType != this.AppType)
  64. {
  65. continue;
  66. }
  67. object obj = Activator.CreateInstance(type);
  68. IMRegister iMRegister = obj as IMRegister;
  69. if (iMRegister == null)
  70. {
  71. throw new Exception($"message handler not inherit IEventSync or IEventAsync interface: {obj.GetType().FullName}");
  72. }
  73. iMRegister.Register(this);
  74. }
  75. }
  76. }
  77. public ushort GetOpcode(Type type)
  78. {
  79. return this.messageOpcode[type];
  80. }
  81. public void RegisterHandler<T>(ushort opcode, Action<Entity, T, uint> action)
  82. {
  83. if (!this.events.ContainsKey(opcode))
  84. {
  85. this.events.Add(opcode, new List<Action<Entity, byte[], int, int>>());
  86. }
  87. List<Action<Entity, byte[], int, int>> actions = this.events[opcode];
  88. actions.Add((entity, messageBytes, offset, count) =>
  89. {
  90. T t;
  91. uint rpcId;
  92. try
  93. {
  94. rpcId = BitConverter.ToUInt32(messageBytes, 2) & 0x7fffffff;
  95. t = MongoHelper.FromBson<T>(messageBytes, offset, count);
  96. }
  97. catch (Exception ex)
  98. {
  99. throw new Exception("解释消息失败:" + opcode, ex);
  100. }
  101. action(entity, t, rpcId);
  102. });
  103. }
  104. public void Handle(Entity entity, ushort opcode, byte[] messageBytes, int offset)
  105. {
  106. List<Action<Entity, byte[], int, int>> actions;
  107. if (!this.events.TryGetValue(opcode, out actions))
  108. {
  109. Log.Error($"消息 {opcode} 没有处理");
  110. return;
  111. }
  112. foreach (var ev in actions)
  113. {
  114. try
  115. {
  116. ev(entity, messageBytes, offset, messageBytes.Length - offset);
  117. }
  118. catch (Exception e)
  119. {
  120. Log.Error(e.ToString());
  121. }
  122. }
  123. }
  124. public override void Dispose()
  125. {
  126. if (this.Id == 0)
  127. {
  128. return;
  129. }
  130. base.Dispose();
  131. }
  132. }
  133. }