MessageDispatherComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  48. #if ILRuntime
  49. IInstanceMethod method = new ILInstanceMethod(type, "Handle");
  50. #else
  51. IInstanceMethod method = new MonoInstanceMethod(type, "Handle");
  52. #endif
  53. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  54. {
  55. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());
  56. }
  57. this.handlers[messageHandlerAttribute.Opcode].Add(method);
  58. }
  59. }
  60. public ushort GetOpcode(Type type)
  61. {
  62. return this.opcodeTypes.GetKeyByValue(type);
  63. }
  64. public void Handle(Session session, MessageInfo messageInfo)
  65. {
  66. List<IInstanceMethod> actions;
  67. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  68. {
  69. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  70. return;
  71. }
  72. Type messageType = this.opcodeTypes.GetValueByKey(messageInfo.Opcode);
  73. object message = MongoHelper.FromBson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  74. messageInfo.Message = message;
  75. foreach (IInstanceMethod ev in actions)
  76. {
  77. try
  78. {
  79. ev.Run(session, messageInfo);
  80. }
  81. catch (Exception e)
  82. {
  83. Log.Error(e.ToString());
  84. }
  85. }
  86. }
  87. public override void Dispose()
  88. {
  89. if (this.Id == 0)
  90. {
  91. return;
  92. }
  93. base.Dispose();
  94. }
  95. }
  96. }