MessageHandlerComponent.cs 2.9 KB

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