SessionStreamDispatcherServerInner.cs 4.6 KB

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