MessageDispatherComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using Model;
  4. namespace Hotfix
  5. {
  6. /// <summary>
  7. /// 消息分发组件
  8. /// </summary>
  9. [EntityEvent(EntityEventId.MessageDispatherComponent)]
  10. public class MessageDispatherComponent: Component, IAwake<AppType>, ILoad
  11. {
  12. private AppType AppType;
  13. private Dictionary<ushort, List<IInstanceMethod>> handlers;
  14. private DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
  15. public void Awake(AppType appType)
  16. {
  17. this.AppType = appType;
  18. this.Load();
  19. }
  20. public void Load()
  21. {
  22. this.handlers = new Dictionary<ushort, List<IInstanceMethod>>();
  23. this.opcodeTypes = new DoubleMap<ushort, Type>();
  24. Type[] monoTypes = DllHelper.GetAllTypes();
  25. foreach (Type monoType in monoTypes)
  26. {
  27. object[] attrs = monoType.GetCustomAttributes(typeof(MessageAttribute), false);
  28. if (attrs.Length == 0)
  29. {
  30. continue;
  31. }
  32. MessageAttribute messageAttribute = attrs[0] as MessageAttribute;
  33. if (messageAttribute == null)
  34. {
  35. continue;
  36. }
  37. this.opcodeTypes.Add(messageAttribute.Opcode, monoType);
  38. }
  39. Type[] types = DllHelper.GetAllTypes();
  40. foreach (Type type in types)
  41. {
  42. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  43. if (attrs.Length == 0)
  44. {
  45. continue;
  46. }
  47. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  48. IInstanceMethod method = new ILInstanceMethod(type, "Handle");
  49. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  50. {
  51. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());
  52. }
  53. this.handlers[messageHandlerAttribute.Opcode].Add(method);
  54. }
  55. }
  56. public ushort GetOpcode(Type type)
  57. {
  58. return this.opcodeTypes.GetKeyByValue(type);
  59. }
  60. public void Handle(Session session, MessageInfo messageInfo)
  61. {
  62. List<IInstanceMethod> actions;
  63. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  64. {
  65. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  66. return;
  67. }
  68. Type messageType = this.opcodeTypes.GetValueByKey(messageInfo.Opcode);
  69. object message = JsonHelper.FromJson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  70. messageInfo.Message = message;
  71. foreach (IInstanceMethod ev in actions)
  72. {
  73. try
  74. {
  75. ev.Run(session, messageInfo.Message);
  76. }
  77. catch (Exception e)
  78. {
  79. Log.Error(e.ToString());
  80. }
  81. }
  82. }
  83. public override void Dispose()
  84. {
  85. if (this.Id == 0)
  86. {
  87. return;
  88. }
  89. base.Dispose();
  90. }
  91. }
  92. }