MessageDispatherComponent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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<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 MessageDispatherComponent: Component, IMessageDispather
  24. {
  25. private class MessageInfo
  26. {
  27. public byte[] MessageBytes;
  28. public int Offset;
  29. public int Count;
  30. public uint RpcId;
  31. }
  32. private string AppType;
  33. private Dictionary<ushort, List<Action<Entity, MessageInfo>>> events;
  34. private Dictionary<ushort, List<Action<Entity, MessageInfo>>> rpcHandlers;
  35. public Dictionary<Type, MessageAttribute> messageOpcode { get; private set; } = new Dictionary<Type, MessageAttribute>();
  36. public void Awake(string appType)
  37. {
  38. this.AppType = appType;
  39. this.Load();
  40. }
  41. public void Load()
  42. {
  43. this.events = new Dictionary<ushort, List<Action<Entity, MessageInfo>>>();
  44. this.rpcHandlers = new Dictionary<ushort, List<Action<Entity, MessageInfo>>>();
  45. this.messageOpcode = new Dictionary<Type, MessageAttribute>();
  46. Assembly[] assemblies = Object.ObjectManager.GetAssemblies();
  47. foreach (Assembly assembly in assemblies)
  48. {
  49. Type[] types = assembly.GetTypes();
  50. foreach (Type type in types)
  51. {
  52. object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false);
  53. if (attrs.Length == 0)
  54. {
  55. continue;
  56. }
  57. MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
  58. this.messageOpcode[type] = messageAttribute;
  59. }
  60. }
  61. foreach (Assembly assembly in assemblies)
  62. {
  63. Type[] types = assembly.GetTypes();
  64. foreach (Type type in types)
  65. {
  66. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  67. if (attrs.Length == 0)
  68. {
  69. continue;
  70. }
  71. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  72. if (messageHandlerAttribute.AppType != this.AppType)
  73. {
  74. continue;
  75. }
  76. object obj = Activator.CreateInstance(type);
  77. IMRegister iMRegister = obj as IMRegister;
  78. if (iMRegister == null)
  79. {
  80. throw new Exception($"message handler not inherit IEventSync or IEventAsync interface: {obj.GetType().FullName}");
  81. }
  82. iMRegister.Register(this);
  83. }
  84. }
  85. }
  86. public ushort GetOpcode(Type type)
  87. {
  88. return this.messageOpcode[type].Opcode;
  89. }
  90. public void RegisterHandler<T>(ushort opcode, Action<Entity, T> action)
  91. {
  92. if (!this.events.ContainsKey(opcode))
  93. {
  94. this.events.Add(opcode, new List<Action<Entity, MessageInfo>>());
  95. }
  96. List<Action<Entity, MessageInfo>> actions = this.events[opcode];
  97. actions.Add((entity, messageInfo) =>
  98. {
  99. T t;
  100. try
  101. {
  102. t = MongoHelper.FromBson<T>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  103. }
  104. catch (Exception ex)
  105. {
  106. throw new Exception("解释消息失败:" + opcode, ex);
  107. }
  108. action(entity, t);
  109. });
  110. }
  111. public void RegisterRpcHandler<T>(ushort opcode, Action<Entity, T, uint> action)
  112. {
  113. if (!this.rpcHandlers.ContainsKey(opcode))
  114. {
  115. this.rpcHandlers.Add(opcode, new List<Action<Entity, MessageInfo>>());
  116. }
  117. List<Action<Entity, MessageInfo>> actions = this.rpcHandlers[opcode];
  118. actions.Add((entity, messageInfo) =>
  119. {
  120. T t;
  121. try
  122. {
  123. t = MongoHelper.FromBson<T>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  124. }
  125. catch (Exception ex)
  126. {
  127. throw new Exception("解释消息失败:" + opcode, ex);
  128. }
  129. action(entity, t, messageInfo.RpcId);
  130. });
  131. }
  132. public void Handle(Entity entity, ushort opcode, byte[] messageBytes, int offset)
  133. {
  134. List<Action<Entity, MessageInfo>> actions;
  135. if (!this.events.TryGetValue(opcode, out actions))
  136. {
  137. Log.Error($"消息 {opcode} 没有处理");
  138. return;
  139. }
  140. foreach (var ev in actions)
  141. {
  142. try
  143. {
  144. ev(entity, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset });
  145. }
  146. catch (Exception e)
  147. {
  148. Log.Error(e.ToString());
  149. }
  150. }
  151. }
  152. public void HandleRpc(Entity entity, ushort opcode, byte[] messageBytes, int offset, uint rpcId)
  153. {
  154. List<Action<Entity, MessageInfo>> actions;
  155. if (!this.rpcHandlers.TryGetValue(opcode, out actions))
  156. {
  157. Log.Error($"Rpc消息 {opcode} 没有处理");
  158. return;
  159. }
  160. foreach (var ev in actions)
  161. {
  162. try
  163. {
  164. ev(entity, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset, RpcId = rpcId });
  165. }
  166. catch (Exception e)
  167. {
  168. Log.Error(e.ToString());
  169. }
  170. }
  171. }
  172. public override void Dispose()
  173. {
  174. if (this.Id == 0)
  175. {
  176. return;
  177. }
  178. base.Dispose();
  179. }
  180. }
  181. }