OuterMessageDispatcher.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using ETModel;
  2. namespace ETHotfix
  3. {
  4. public class OuterMessageDispatcher: IMessageDispatcher
  5. {
  6. public void Dispatch(Session session, ushort opcode, object message)
  7. {
  8. DispatchAsync(session, opcode, message).Coroutine();
  9. }
  10. public async ETVoid DispatchAsync(Session session, ushort opcode, object message)
  11. {
  12. // 根据消息接口判断是不是Actor消息,不同的接口做不同的处理
  13. switch (message)
  14. {
  15. case IActorLocationRequest actorLocationRequest: // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
  16. {
  17. long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
  18. ActorLocationSender actorLocationSender = Game.Scene.GetComponent<ActorLocationSenderComponent>().Get(unitId);
  19. int rpcId = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
  20. long instanceId = session.InstanceId;
  21. IResponse response = await actorLocationSender.Call(actorLocationRequest);
  22. response.RpcId = rpcId;
  23. // session可能已经断开了,所以这里需要判断
  24. if (session.InstanceId == instanceId)
  25. {
  26. session.Reply(response);
  27. }
  28. break;
  29. }
  30. case IActorLocationMessage actorLocationMessage:
  31. {
  32. long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
  33. ActorLocationSender actorLocationSender = Game.Scene.GetComponent<ActorLocationSenderComponent>().Get(unitId);
  34. actorLocationSender.Send(actorLocationMessage);
  35. break;
  36. }
  37. case IActorRequest actorRequest: // 分发IActorRequest消息,目前没有用到,需要的自己添加
  38. {
  39. break;
  40. }
  41. case IActorMessage actorMessage: // 分发IActorMessage消息,目前没有用到,需要的自己添加
  42. {
  43. break;
  44. }
  45. default:
  46. {
  47. // 非Actor消息
  48. Game.Scene.GetComponent<MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. }