ActorMessageDispatcherComponentSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET.Server
  4. {
  5. /// <summary>
  6. /// Actor消息分发组件
  7. /// </summary>
  8. [FriendOf(typeof(ActorMessageDispatcherComponent))]
  9. public static class ActorMessageDispatcherComponentHelper
  10. {
  11. [ObjectSystem]
  12. public class ActorMessageDispatcherComponentAwakeSystem: AwakeSystem<ActorMessageDispatcherComponent>
  13. {
  14. protected override void Awake(ActorMessageDispatcherComponent self)
  15. {
  16. ActorMessageDispatcherComponent.Instance = self;
  17. self.Awake();
  18. }
  19. }
  20. [ObjectSystem]
  21. public class ActorMessageDispatcherComponentLoadSystem: LoadSystem<ActorMessageDispatcherComponent>
  22. {
  23. protected override void Load(ActorMessageDispatcherComponent self)
  24. {
  25. self.Load();
  26. }
  27. }
  28. [ObjectSystem]
  29. public class ActorMessageDispatcherComponentDestroySystem: DestroySystem<ActorMessageDispatcherComponent>
  30. {
  31. protected override void Destroy(ActorMessageDispatcherComponent self)
  32. {
  33. self.ActorMessageHandlers.Clear();
  34. ActorMessageDispatcherComponent.Instance = null;
  35. }
  36. }
  37. public static void Awake(this ActorMessageDispatcherComponent self)
  38. {
  39. self.Load();
  40. }
  41. public static void Load(this ActorMessageDispatcherComponent self)
  42. {
  43. self.ActorMessageHandlers.Clear();
  44. var types = EventSystem.Instance.GetTypes(typeof (ActorMessageHandlerAttribute));
  45. foreach (Type type in types)
  46. {
  47. object obj = Activator.CreateInstance(type);
  48. IMActorHandler imHandler = obj as IMActorHandler;
  49. if (imHandler == null)
  50. {
  51. throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}");
  52. }
  53. object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
  54. foreach (object attr in attrs)
  55. {
  56. ActorMessageHandlerAttribute actorMessageHandlerAttribute = attr as ActorMessageHandlerAttribute;
  57. Type messageType = imHandler.GetRequestType();
  58. Type handleResponseType = imHandler.GetResponseType();
  59. if (handleResponseType != null)
  60. {
  61. Type responseType = OpcodeTypeComponent.Instance.GetResponseType(messageType);
  62. if (handleResponseType != responseType)
  63. {
  64. throw new Exception($"message handler response type error: {messageType.FullName}");
  65. }
  66. }
  67. ActorMessageDispatcherInfo actorMessageDispatcherInfo = new ActorMessageDispatcherInfo(actorMessageHandlerAttribute.SceneType, imHandler);
  68. self.RegisterHandler(messageType, actorMessageDispatcherInfo);
  69. }
  70. }
  71. }
  72. private static void RegisterHandler(this ActorMessageDispatcherComponent self, Type type, ActorMessageDispatcherInfo handler)
  73. {
  74. if (!self.ActorMessageHandlers.ContainsKey(type))
  75. {
  76. self.ActorMessageHandlers.Add(type, new List<ActorMessageDispatcherInfo>());
  77. }
  78. self.ActorMessageHandlers[type].Add(handler);
  79. }
  80. /// <summary>
  81. /// 分发actor消息
  82. /// </summary>
  83. public static async ETTask Handle(
  84. this ActorMessageDispatcherComponent self, Entity entity, object message, Action<IActorResponse> reply)
  85. {
  86. List<ActorMessageDispatcherInfo> list;
  87. if (!self.ActorMessageHandlers.TryGetValue(message.GetType(), out list))
  88. {
  89. throw new Exception($"not found message handler: {message}");
  90. }
  91. SceneType sceneType = entity.DomainScene().SceneType;
  92. foreach (ActorMessageDispatcherInfo actorMessageDispatcherInfo in list)
  93. {
  94. if (actorMessageDispatcherInfo.SceneType != sceneType)
  95. {
  96. continue;
  97. }
  98. await actorMessageDispatcherInfo.IMActorHandler.Handle(entity, message, reply);
  99. }
  100. }
  101. }
  102. }