NetInnerComponentOnReadEvent.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. namespace ET.Server
  3. {
  4. [Event(SceneType.Process)]
  5. public class NetInnerComponentOnReadEvent: AEvent<NetInnerComponentOnRead>
  6. {
  7. [EnableAccessEntiyChild]
  8. protected override async ETTask Run(Scene scene, NetInnerComponentOnRead args)
  9. {
  10. try
  11. {
  12. long actorId = args.ActorId;
  13. object message = args.Message;
  14. InstanceIdStruct instanceIdStruct = new(actorId);
  15. int fromProcess = instanceIdStruct.Process;
  16. instanceIdStruct.Process = Options.Instance.Process;
  17. long realActorId = instanceIdStruct.ToLong();
  18. // 收到actor消息,放入actor队列
  19. switch (message)
  20. {
  21. case IActorResponse iActorResponse:
  22. {
  23. ActorMessageSenderComponent.Instance.RunMessage(realActorId, iActorResponse);
  24. break;
  25. }
  26. case IActorRequest iActorRequest:
  27. {
  28. void Reply(IActorResponse response)
  29. {
  30. if (fromProcess == Options.Instance.Process) // 返回消息是同一个进程
  31. {
  32. // NetInnerComponent.Instance.HandleMessage(realActorId, response); 等同于直接调用下面这句
  33. ActorMessageSenderComponent.Instance.RunMessage(realActorId, response);
  34. return;
  35. }
  36. Session replySession = NetInnerComponent.Instance.Get(fromProcess);
  37. // 发回真实的actorId 做查问题使用
  38. replySession.Send(realActorId, response);
  39. }
  40. Entity entity = Root.Instance.Get(realActorId);
  41. if (entity == null)
  42. {
  43. IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
  44. Reply(response);
  45. break;
  46. }
  47. MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
  48. if (mailBoxComponent == null)
  49. {
  50. Log.Warning($"actor not found mailbox: {entity.GetType().Name} {realActorId} {iActorRequest}");
  51. IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
  52. Reply(response);
  53. break;
  54. }
  55. switch (mailBoxComponent.MailboxType)
  56. {
  57. case MailboxType.MessageDispatcher:
  58. {
  59. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, realActorId))
  60. {
  61. if (entity.InstanceId != realActorId)
  62. {
  63. IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
  64. Reply(response);
  65. break;
  66. }
  67. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorRequest, Reply);
  68. }
  69. break;
  70. }
  71. case MailboxType.UnOrderMessageDispatcher:
  72. {
  73. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorRequest, Reply);
  74. break;
  75. }
  76. }
  77. break;
  78. }
  79. case IActorMessage iActorMessage:
  80. {
  81. Entity entity = Root.Instance.Get(realActorId);
  82. if (entity == null)
  83. {
  84. Log.Error($"not found actor: {scene.Name} {realActorId} {message}");
  85. break;
  86. }
  87. MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
  88. if (mailBoxComponent == null)
  89. {
  90. Log.Error($"actor not found mailbox: {entity.GetType().Name} {realActorId} {iActorMessage}");
  91. break;
  92. }
  93. switch (mailBoxComponent.MailboxType)
  94. {
  95. case MailboxType.MessageDispatcher:
  96. {
  97. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, realActorId))
  98. {
  99. if (entity.InstanceId != realActorId)
  100. {
  101. break;
  102. }
  103. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null);
  104. }
  105. break;
  106. }
  107. case MailboxType.UnOrderMessageDispatcher:
  108. {
  109. await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null);
  110. break;
  111. }
  112. case MailboxType.GateSession:
  113. {
  114. if (entity is Session gateSession)
  115. {
  116. // 发送给客户端
  117. gateSession.Send(0, iActorMessage);
  118. }
  119. break;
  120. }
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. Log.Error($"InnerMessageDispatcher error: {args.Message.GetType().Name}\n{e}");
  129. }
  130. }
  131. }
  132. }