SessionStreamDispatcherServerInner.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.IO;
  3. namespace ET.Server
  4. {
  5. [SessionStreamDispatcher(SessionStreamDispatcherType.SessionStreamDispatcherServerInner)]
  6. public class SessionStreamDispatcherServerInner: ISessionStreamDispatcher
  7. {
  8. public void Dispatch(Session session, MemoryStream memoryStream)
  9. {
  10. ushort opcode = 0;
  11. try
  12. {
  13. long actorId = BitConverter.ToInt64(memoryStream.GetBuffer(), Packet.ActorIdIndex);
  14. opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);
  15. Type type = null;
  16. object message = null;
  17. #if SERVER
  18. // 内网收到外网消息,有可能是gateUnit消息,还有可能是gate广播消息
  19. if (OpcodeTypeComponent.Instance.IsOutrActorMessage(opcode))
  20. {
  21. InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
  22. instanceIdStruct.Process = Game.Options.Process;
  23. long realActorId = instanceIdStruct.ToLong();
  24. Entity entity = Game.EventSystem.Get(realActorId);
  25. if (entity == null)
  26. {
  27. type = OpcodeTypeComponent.Instance.GetType(opcode);
  28. message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
  29. Log.Error($"not found actor: {session.DomainScene().Name} {opcode} {realActorId} {message}");
  30. return;
  31. }
  32. if (entity is Session gateSession)
  33. {
  34. // 发送给客户端
  35. memoryStream.Seek(Packet.OpcodeIndex, SeekOrigin.Begin);
  36. gateSession.Send(0, memoryStream);
  37. return;
  38. }
  39. }
  40. #endif
  41. type = OpcodeTypeComponent.Instance.GetType(opcode);
  42. message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
  43. if (message is IResponse iResponse && !(message is IActorResponse))
  44. {
  45. session.OnRead(opcode, iResponse);
  46. return;
  47. }
  48. OpcodeHelper.LogMsg(session.DomainZone(), opcode, message);
  49. // 收到actor消息,放入actor队列
  50. switch (message)
  51. {
  52. case IActorRequest iActorRequest:
  53. {
  54. InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
  55. int fromProcess = instanceIdStruct.Process;
  56. instanceIdStruct.Process = Game.Options.Process;
  57. long realActorId = instanceIdStruct.ToLong();
  58. void Reply(IActorResponse response)
  59. {
  60. Session replySession = NetInnerComponent.Instance.Get(fromProcess);
  61. // 发回真实的actorId 做查问题使用
  62. replySession.Send(realActorId, response);
  63. }
  64. InnerMessageDispatcherHelper.HandleIActorRequest(opcode, realActorId, iActorRequest, Reply);
  65. return;
  66. }
  67. case IActorResponse iActorResponse:
  68. {
  69. InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
  70. instanceIdStruct.Process = Game.Options.Process;
  71. long realActorId = instanceIdStruct.ToLong();
  72. InnerMessageDispatcherHelper.HandleIActorResponse(opcode, realActorId, iActorResponse);
  73. return;
  74. }
  75. case IActorMessage iactorMessage:
  76. {
  77. InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
  78. instanceIdStruct.Process = Game.Options.Process;
  79. long realActorId = instanceIdStruct.ToLong();
  80. InnerMessageDispatcherHelper.HandleIActorMessage(opcode, realActorId, iactorMessage);
  81. return;
  82. }
  83. default:
  84. {
  85. MessageDispatcherComponent.Instance.Handle(session, opcode, message);
  86. break;
  87. }
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. Log.Error($"InnerMessageDispatcher error: {opcode}\n{e}");
  93. }
  94. }
  95. }
  96. }