OuterMessageDispatcher.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. namespace Model
  3. {
  4. public class OuterMessageDispatcher: IMessageDispatcher
  5. {
  6. public async void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message)
  7. {
  8. // gate session收到actor消息直接转发给actor自己去处理
  9. if (message is AActorMessage aActorMessage)
  10. {
  11. ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(aActorMessage.Id);
  12. long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
  13. aActorMessage.Id = unitId;
  14. actorProxy.Send(aActorMessage);
  15. return;
  16. }
  17. // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
  18. if (message is AActorRequest aActorRequest)
  19. {
  20. ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(aActorRequest.Id);
  21. aActorRequest.Id = session.GetComponent<SessionPlayerComponent>().Player.Id;
  22. uint rpcId = aActorRequest.RpcId;
  23. AActorResponse aActorResponse = await actorProxy.Call<AActorResponse>(aActorRequest);
  24. aActorResponse.RpcId = rpcId;
  25. session.Reply(aActorResponse);
  26. return;
  27. }
  28. if (message is AMessage)
  29. {
  30. Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, message);
  31. return;
  32. }
  33. throw new Exception($"message type error: {message.GetType().FullName}");
  34. }
  35. }
  36. }