Actor_TransferHandler.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. using ETModel;
  5. namespace ETHotfix
  6. {
  7. [ActorMessageHandler(AppType.Map)]
  8. public class Actor_TransferHandler : AMActorRpcHandler<Unit, Actor_TransferRequest, Actor_TransferResponse>
  9. {
  10. protected override async Task Run(Unit unit, Actor_TransferRequest message, Action<Actor_TransferResponse> reply)
  11. {
  12. Actor_TransferResponse response = new Actor_TransferResponse();
  13. try
  14. {
  15. long unitId = unit.Id;
  16. // 先在location锁住unit的地址
  17. await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId);
  18. // 删除unit actorcomponent,让其它进程发送过来的消息找不到actor,重发
  19. unit.RemoveComponent<ActorComponent>();
  20. int mapIndex = message.MapIndex;
  21. StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
  22. // 考虑AllServer情况
  23. if (startConfigComponent.Count == 1)
  24. {
  25. mapIndex = 0;
  26. }
  27. // 传送到map
  28. StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
  29. IPEndPoint address = mapConfig.GetComponent<InnerConfig>().IPEndPoint;
  30. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
  31. // 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
  32. Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
  33. await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
  34. unit.Dispose();
  35. // 解锁unit的地址,并且更新unit的地址
  36. await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, mapConfig.AppId);
  37. reply(response);
  38. }
  39. catch (Exception e)
  40. {
  41. ReplyError(response, e, reply);
  42. }
  43. }
  44. }
  45. }