MessageHandlerComponent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace Base
  5. {
  6. [ObjectEvent]
  7. public class MessageHandlerComponentEvent : ObjectEvent<MessageHandlerComponent>, ILoader, IAwake<SceneType>
  8. {
  9. public void Load()
  10. {
  11. this.GetValue().Load();
  12. }
  13. public void Awake(SceneType sceneType)
  14. {
  15. this.GetValue().Awake(sceneType);
  16. }
  17. }
  18. /// <summary>
  19. /// 消息分发组件
  20. /// </summary>
  21. public class MessageHandlerComponent: Component
  22. {
  23. private SceneType SceneType;
  24. private Dictionary<Opcode, List<Action<Entity, byte[], int, int>>> events;
  25. public void Awake(SceneType sceneType)
  26. {
  27. this.SceneType = sceneType;
  28. this.Load();
  29. }
  30. public void Load()
  31. {
  32. this.events = new Dictionary<Opcode, List<Action<Entity, byte[], int, int>>>();
  33. Assembly[] assemblies = Object.ObjectManager.GetAssemblies();
  34. foreach (Assembly assembly in assemblies)
  35. {
  36. Type[] types = assembly.GetTypes();
  37. foreach (Type type in types)
  38. {
  39. object[] attrs = type.GetCustomAttributes(typeof(MessageAttribute), false);
  40. if (attrs.Length == 0)
  41. {
  42. continue;
  43. }
  44. MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
  45. if (messageAttribute.SceneType != this.SceneType)
  46. {
  47. continue;
  48. }
  49. object obj = Activator.CreateInstance(type);
  50. IMRegister<MessageHandlerComponent> iMRegister = obj as IMRegister<MessageHandlerComponent>;
  51. if (iMRegister == null)
  52. {
  53. throw new GameException($"message handler not inherit IEventSync or IEventAsync interface: {obj.GetType().FullName}");
  54. }
  55. iMRegister.Register(this);
  56. }
  57. }
  58. }
  59. public void Register<T>(Action<Entity, T> action)
  60. {
  61. Opcode opcode = EnumHelper.FromString<Opcode>(typeof (T).Name);
  62. if (!this.events.ContainsKey(opcode))
  63. {
  64. this.events.Add(opcode, new List<Action<Entity, byte[], int, int>>());
  65. }
  66. List<Action<Entity, byte[], int, int>> actions = this.events[opcode];
  67. actions.Add((entity, messageBytes, offset, count) =>
  68. {
  69. T t;
  70. try
  71. {
  72. t = MongoHelper.FromBson<T>(messageBytes, offset, count);
  73. }
  74. catch (Exception ex)
  75. {
  76. throw new GameException("解释消息失败:" + opcode, ex);
  77. }
  78. if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
  79. {
  80. Log.Debug(MongoHelper.ToJson(t));
  81. }
  82. action(entity, t);
  83. });
  84. }
  85. public void Handle(Entity entity, Opcode opcode, byte[] messageBytes, int offset)
  86. {
  87. List<Action<Entity, byte[], int, int>> actions;
  88. if (!this.events.TryGetValue(opcode, out actions))
  89. {
  90. if (this.SceneType == SceneType.Game)
  91. {
  92. Log.Error($"消息{opcode}没有处理");
  93. }
  94. return;
  95. }
  96. foreach (var ev in actions)
  97. {
  98. try
  99. {
  100. ev(entity, messageBytes, offset, messageBytes.Length - offset);
  101. }
  102. catch (Exception e)
  103. {
  104. Log.Error(e.ToString());
  105. }
  106. }
  107. }
  108. public override void Dispose()
  109. {
  110. if (this.Id == 0)
  111. {
  112. return;
  113. }
  114. base.Dispose();
  115. }
  116. }
  117. }