MessageHandlerComponent.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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<string>
  10. {
  11. public void Load()
  12. {
  13. this.GetValue().Load();
  14. }
  15. public void Awake(string appType)
  16. {
  17. this.GetValue().Awake(appType);
  18. }
  19. }
  20. /// <summary>
  21. /// 消息分发组件
  22. /// </summary>
  23. public class MessageHandlerComponent: Component, IMessageHandler
  24. {
  25. private string AppType;
  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(string appType)
  29. {
  30. this.AppType = appType;
  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.AppType != this.AppType)
  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 RegisterOpcode(Type type, ushort opcode)
  64. {
  65. this.MessageOpcode[type] = opcode;
  66. }
  67. public void Register<T>(ushort opcode, Action<Entity, T> action)
  68. {
  69. if (!this.events.ContainsKey(opcode))
  70. {
  71. this.events.Add(opcode, new List<Action<Entity, byte[], int, int>>());
  72. }
  73. List<Action<Entity, byte[], int, int>> actions = this.events[opcode];
  74. actions.Add((entity, messageBytes, offset, count) =>
  75. {
  76. T t;
  77. try
  78. {
  79. t = MongoHelper.FromBson<T>(messageBytes, offset, count);
  80. }
  81. catch (Exception ex)
  82. {
  83. throw new Exception("解释消息失败:" + opcode, ex);
  84. }
  85. action(entity, t);
  86. });
  87. }
  88. public void Handle(Entity entity, ushort opcode, byte[] messageBytes, int offset)
  89. {
  90. List<Action<Entity, byte[], int, int>> actions;
  91. if (!this.events.TryGetValue(opcode, out actions))
  92. {
  93. Log.Error($"消息{opcode}没有处理");
  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. }