MessageDispatherComponent.cs 5.3 KB

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