InnerMessageDispatcherHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.IO;
  3. namespace ET.Server
  4. {
  5. public static class InnerMessageDispatcherHelper
  6. {
  7. public static void HandleIActorResponse(ushort opcode, long actorId, IActorResponse iActorResponse)
  8. {
  9. ActorMessageSenderComponent.Instance.RunMessage(actorId, iActorResponse);
  10. }
  11. public static void HandleIActorRequest(ushort opcode, long actorId, IActorRequest iActorRequest, Action<IActorResponse> reply)
  12. {
  13. Entity entity = Game.EventSystem.Get(actorId);
  14. if (entity == null)
  15. {
  16. FailResponse(iActorRequest, ErrorCore.ERR_NotFoundActor, reply);
  17. return;
  18. }
  19. MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
  20. if (mailBoxComponent == null)
  21. {
  22. Log.Warning($"actor not found mailbox: {entity.GetType().Name} {actorId} {iActorRequest}");
  23. FailResponse(iActorRequest, ErrorCore.ERR_NotFoundActor, reply);
  24. return;
  25. }
  26. switch (mailBoxComponent.MailboxType)
  27. {
  28. case MailboxType.MessageDispatcher:
  29. {
  30. async ETTask MessageDispatcherHandler()
  31. {
  32. long instanceId = entity.InstanceId;
  33. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId))
  34. {
  35. if (entity.InstanceId != instanceId)
  36. {
  37. FailResponse(iActorRequest, ErrorCore.ERR_NotFoundActor, reply);
  38. return;
  39. }
  40. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorRequest, reply);
  41. }
  42. }
  43. MessageDispatcherHandler().Coroutine();
  44. break;
  45. }
  46. case MailboxType.UnOrderMessageDispatcher:
  47. {
  48. ActorMessageDispatcherComponent.Instance.Handle(entity, iActorRequest, reply).Coroutine();
  49. break;
  50. }
  51. }
  52. }
  53. private static void FailResponse(IActorRequest iActorRequest, int error, Action<IActorResponse> reply)
  54. {
  55. IActorResponse response = ActorHelper.CreateResponse(iActorRequest, error);
  56. reply.Invoke(response);
  57. }
  58. public static void HandleIActorMessage(ushort opcode, long actorId, IActorMessage iActorMessage)
  59. {
  60. OpcodeHelper.LogMsg(opcode, actorId, iActorMessage);
  61. Entity entity = Game.EventSystem.Get(actorId);
  62. if (entity == null)
  63. {
  64. Log.Error($"not found actor: {actorId} {iActorMessage}");
  65. return;
  66. }
  67. MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
  68. if (mailBoxComponent == null)
  69. {
  70. Log.Error($"actor not found mailbox: {entity.GetType().Name} {actorId} {iActorMessage}");
  71. return;
  72. }
  73. switch (mailBoxComponent.MailboxType)
  74. {
  75. case MailboxType.MessageDispatcher:
  76. {
  77. async ETTask MessageDispatcherHandler()
  78. {
  79. long instanceId = entity.InstanceId;
  80. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId))
  81. {
  82. if (entity.InstanceId != instanceId)
  83. {
  84. return;
  85. }
  86. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null);
  87. }
  88. }
  89. MessageDispatcherHandler().Coroutine();
  90. break;
  91. }
  92. case MailboxType.UnOrderMessageDispatcher:
  93. {
  94. ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null).Coroutine();
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }