NetComponentOnReadInvoker_Gate.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. namespace ET.Server
  3. {
  4. [Invoke(SceneType.Gate)]
  5. public class NetComponentOnReadInvoker_Gate: AInvokeHandler<NetComponentOnRead>
  6. {
  7. public override void Handle(NetComponentOnRead args)
  8. {
  9. HandleAsync(args).NoContext();
  10. }
  11. private async ETTask HandleAsync(NetComponentOnRead args)
  12. {
  13. Session session = args.Session;
  14. object message = args.Message;
  15. Scene root = args.Session.Root();
  16. // 根据消息接口判断是不是Actor消息,不同的接口做不同的处理,比如需要转发给Chat Scene,可以做一个IChatMessage接口
  17. switch (message)
  18. {
  19. case ISessionMessage:
  20. {
  21. MessageSessionDispatcher.Instance.Handle(session, message);
  22. break;
  23. }
  24. case ILocationMessage actorLocationMessage:
  25. {
  26. long unitId = session.GetComponent<SessionPlayerComponent>().Player.Id;
  27. root.GetComponent<MessageLocationSenderComponent>().Get(LocationType.Unit).Send(unitId, actorLocationMessage);
  28. break;
  29. }
  30. case ILocationRequest actorLocationRequest: // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
  31. {
  32. long unitId = session.GetComponent<SessionPlayerComponent>().Player.Id;
  33. int rpcId = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
  34. long instanceId = session.InstanceId;
  35. IResponse iResponse = await root.GetComponent<MessageLocationSenderComponent>().Get(LocationType.Unit).Call(unitId, actorLocationRequest);
  36. iResponse.RpcId = rpcId;
  37. // session可能已经断开了,所以这里需要判断
  38. if (session.InstanceId == instanceId)
  39. {
  40. session.Send(iResponse);
  41. }
  42. break;
  43. }
  44. case IRequest actorRequest: // 分发IActorRequest消息,目前没有用到,需要的自己添加
  45. {
  46. break;
  47. }
  48. case IMessage actorMessage: // 分发IActorMessage消息,目前没有用到,需要的自己添加
  49. {
  50. break;
  51. }
  52. default:
  53. {
  54. throw new Exception($"not found handler: {message}");
  55. }
  56. }
  57. }
  58. }
  59. }