MessageDispatherComponent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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>>> handlers;
  34. private Dictionary<ushort, Action<Entity, MessageInfo>> rpcHandlers;
  35. private Dictionary<Type, MessageAttribute> messageOpcode { get; 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.handlers = new Dictionary<ushort, List<Action<Entity, MessageInfo>>>();
  44. this.rpcHandlers = new Dictionary<ushort, 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.Contains(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 AMEvent or AMRpcEvent abstract class: {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<Message>(ushort opcode, Action<Entity, Message> action) where Message: AMessage
  91. {
  92. if (!this.handlers.ContainsKey(opcode))
  93. {
  94. this.handlers.Add(opcode, new List<Action<Entity, MessageInfo>>());
  95. }
  96. List<Action<Entity, MessageInfo>> actions = this.handlers[opcode];
  97. actions.Add((entity, messageInfo) =>
  98. {
  99. Message message;
  100. try
  101. {
  102. message = MongoHelper.FromBson<Message>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  103. Log.Info(MongoHelper.ToJson(message));
  104. }
  105. catch (Exception ex)
  106. {
  107. throw new Exception("解释消息失败:" + opcode, ex);
  108. }
  109. action(entity, message);
  110. });
  111. }
  112. public void RegisterRpcHandler<Request, Response>(ushort opcode, Action<Entity, Request, Action<Response>> action)
  113. where Request: ARequest
  114. where Response: AResponse
  115. {
  116. if (this.rpcHandlers.ContainsKey(opcode))
  117. {
  118. Log.Error($"rpc消息不能注册两次! opcode: {opcode}");
  119. return;
  120. }
  121. this.rpcHandlers.Add(opcode, (entity, messageInfo) =>
  122. {
  123. Request request;
  124. try
  125. {
  126. request = MongoHelper.FromBson<Request>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  127. Log.Info(MongoHelper.ToJson(request));
  128. }
  129. catch (Exception ex)
  130. {
  131. throw new Exception("解释消息失败:" + opcode, ex);
  132. }
  133. action(entity, request, response =>
  134. {
  135. entity.GetComponent<MessageComponent>().Reply(messageInfo.RpcId, response);
  136. }
  137. );
  138. });
  139. }
  140. public void Handle(Entity entity, ushort opcode, byte[] messageBytes, int offset)
  141. {
  142. List<Action<Entity, MessageInfo>> actions;
  143. if (!this.handlers.TryGetValue(opcode, out actions))
  144. {
  145. Log.Error($"消息 {opcode} 没有处理");
  146. return;
  147. }
  148. foreach (var ev in actions)
  149. {
  150. try
  151. {
  152. ev(entity, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset });
  153. }
  154. catch (Exception e)
  155. {
  156. Log.Error(e.ToString());
  157. }
  158. }
  159. }
  160. public void HandleRpc(Entity entity, ushort opcode, byte[] messageBytes, int offset, uint rpcId)
  161. {
  162. Action<Entity, MessageInfo> action;
  163. if (!this.rpcHandlers.TryGetValue(opcode, out action))
  164. {
  165. Log.Error($"Rpc消息 {opcode} 没有处理");
  166. return;
  167. }
  168. try
  169. {
  170. action(entity, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset, RpcId = rpcId });
  171. }
  172. catch (Exception e)
  173. {
  174. Log.Error(e.ToString());
  175. }
  176. }
  177. public override void Dispose()
  178. {
  179. if (this.Id == 0)
  180. {
  181. return;
  182. }
  183. base.Dispose();
  184. }
  185. }
  186. }