MessageHandlerComponent.cs 3.0 KB

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