Actor_TransferHandler.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, unit.InstanceId);
  18. // 删除unit,让其它进程发送过来的消息找不到actor,重发
  19. Game.EventSystem.Remove(unitId);
  20. long instanceId = unit.InstanceId;
  21. int mapIndex = message.MapIndex;
  22. StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
  23. // 考虑AllServer情况
  24. if (startConfigComponent.Count == 1)
  25. {
  26. mapIndex = 0;
  27. }
  28. // 传送到map
  29. StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
  30. IPEndPoint address = mapConfig.GetComponent<InnerConfig>().IPEndPoint;
  31. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
  32. // 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
  33. Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
  34. M2M_TrasferUnitResponse m2m_TrasferUnitResponse = (M2M_TrasferUnitResponse)await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
  35. unit.Dispose();
  36. // 解锁unit的地址,并且更新unit的instanceId
  37. await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, instanceId, m2m_TrasferUnitResponse.InstanceId);
  38. reply(response);
  39. }
  40. catch (Exception e)
  41. {
  42. ReplyError(response, e, reply);
  43. }
  44. }
  45. }
  46. }