Actor_TransferHandler.cs 1.7 KB

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