OuterMessageDispatcher.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using ETModel;
  3. namespace ETHotfix
  4. {
  5. public class OuterMessageDispatcher: IMessageDispatcher
  6. {
  7. public async void Dispatch(Session session, Packet packet)
  8. {
  9. ushort opcode = packet.Opcode();
  10. Type messageType = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(opcode);
  11. object message = session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);
  12. // gate session收到actor消息直接转发给actor自己去处理
  13. if (message is IActorMessage)
  14. {
  15. long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
  16. ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
  17. actorProxy.Send((IMessage)message);
  18. return;
  19. }
  20. // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
  21. if (message is IActorRequest aActorRequest)
  22. {
  23. long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
  24. ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
  25. IResponse response = await actorProxy.Call(aActorRequest);
  26. session.Reply(response);
  27. return;
  28. }
  29. if (message != null)
  30. {
  31. Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
  32. return;
  33. }
  34. throw new Exception($"message type error: {message.GetType().FullName}");
  35. }
  36. public void Dispatch(Session session, ushort opcode, object message)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. }
  41. }