MessageDispatherComponent.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using Base;
  4. namespace Model
  5. {
  6. /// <summary>
  7. /// 消息分发组件
  8. /// </summary>
  9. [EntityEvent(EntityEventId.MessageDispatherComponent)]
  10. public class MessageDispatherComponent: Component
  11. {
  12. private AppType AppType;
  13. private Dictionary<ushort, List<IInstanceMethod>> handlers;
  14. private DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
  15. private void Awake(AppType appType)
  16. {
  17. this.AppType = appType;
  18. this.Load();
  19. }
  20. private void Load()
  21. {
  22. this.handlers = new Dictionary<ushort, List<IInstanceMethod>>();
  23. this.opcodeTypes = new DoubleMap<ushort, Type>();
  24. Type[] monoTypes = DllHelper.GetMonoTypes();
  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. #if ILRuntime
  40. Type[] types = DllHelper.GetHotfixTypes();
  41. #else
  42. Type[] types = DllHelper.GetMonoTypes();
  43. #endif
  44. foreach (Type type in types)
  45. {
  46. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  47. if (attrs.Length == 0)
  48. {
  49. continue;
  50. }
  51. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  52. #if ILRuntime
  53. IInstanceMethod method = new ILInstanceMethod(type, "Handle");
  54. #else
  55. IInstanceMethod method = new MonoInstanceMethod(type, "Handle");
  56. #endif
  57. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  58. {
  59. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());
  60. }
  61. this.handlers[messageHandlerAttribute.Opcode].Add(method);
  62. }
  63. }
  64. public ushort GetOpcode(Type type)
  65. {
  66. return this.opcodeTypes.GetKeyByValue(type);
  67. }
  68. public void Handle(Session session, MessageInfo messageInfo)
  69. {
  70. List<IInstanceMethod> actions;
  71. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  72. {
  73. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  74. return;
  75. }
  76. Type messageType = this.opcodeTypes.GetValueByKey(messageInfo.Opcode);
  77. object message = MongoHelper.FromBson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  78. messageInfo.Message = message;
  79. foreach (IInstanceMethod ev in actions)
  80. {
  81. try
  82. {
  83. ev.Run(session, messageInfo.Message);
  84. }
  85. catch (Exception e)
  86. {
  87. Log.Error(e.ToString());
  88. }
  89. }
  90. }
  91. public override void Dispose()
  92. {
  93. if (this.Id == 0)
  94. {
  95. return;
  96. }
  97. base.Dispose();
  98. }
  99. }
  100. }