Actor_TransferHandler.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Net;
  3. using ETModel;
  4. namespace ETHotfix
  5. {
  6. [ActorMessageHandler(AppType.Map)]
  7. public class Actor_TransferHandler : AMActorRpcHandler<Unit, Actor_TransferRequest, Actor_TransferResponse>
  8. {
  9. protected override async ETTask Run(Unit unit, Actor_TransferRequest message, Action<Actor_TransferResponse> reply)
  10. {
  11. Actor_TransferResponse response = new Actor_TransferResponse();
  12. try
  13. {
  14. long unitId = unit.Id;
  15. // 先在location锁住unit的地址
  16. await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId, unit.InstanceId);
  17. // 删除unit,让其它进程发送过来的消息找不到actor,重发
  18. Game.EventSystem.Remove(unitId);
  19. long instanceId = unit.InstanceId;
  20. int mapIndex = message.MapIndex;
  21. StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
  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. M2M_TrasferUnitResponse m2m_TrasferUnitResponse = (M2M_TrasferUnitResponse)await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
  34. unit.Dispose();
  35. // 解锁unit的地址,并且更新unit的instanceId
  36. await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, instanceId, m2m_TrasferUnitResponse.InstanceId);
  37. reply(response);
  38. }
  39. catch (Exception e)
  40. {
  41. ReplyError(response, e, reply);
  42. }
  43. }
  44. }
  45. }