BattleIceAgentComponentSystem.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Ice;
  2. using System;
  3. using System.Threading;
  4. using BattleIce;
  5. using Newtonsoft.Json.Linq;
  6. namespace ET.Server
  7. {
  8. [FriendOf(typeof (BattleIceAgentComponent))]
  9. public static class BattleIceAgentComponentSystem
  10. {
  11. public class BattleIceAgentComponentAwakeSystem: AwakeSystem<BattleIceAgentComponent>
  12. {
  13. protected override void Awake(BattleIceAgentComponent self)
  14. {
  15. BattleIceAgentComponent.Instance = self;
  16. Thread thread = new Thread(() =>
  17. {
  18. IceApp IceApp = new IceApp();
  19. IceApp.main(Array.Empty<string>(), "../Config/ice.config");
  20. });
  21. thread.Start();
  22. }
  23. }
  24. public class BattleIceAgentComponentDestroySystem: DestroySystem<BattleIceAgentComponent>
  25. {
  26. protected override void Destroy(BattleIceAgentComponent self)
  27. {
  28. Log.Info($"Ice component destroyed");
  29. Application.communicator().Dispose();
  30. }
  31. }
  32. [ObjectSystem]
  33. [FriendOf(typeof (BattleIceAgentComponent))]
  34. private class IceApp: Application
  35. {
  36. public override int run(string[] args)
  37. {
  38. try
  39. {
  40. ZoneManagerPrx ZoneIce = ZoneManagerPrxHelper.checkedCast(communicator().propertyToProxy("zoneManager.Proxy"));
  41. if (ZoneIce == null)
  42. {
  43. Log.Error("not found ice proxy: zoneManager");
  44. return 1;
  45. }
  46. BattleIceAgentComponent.Instance.IceZoneManager = ZoneIce;
  47. Log.Debug("got zoneManager.Proxy");
  48. ZoneIce.ice_invocationTimeout(15000);
  49. ObjectAdapter adapter = communicator().createObjectAdapter("");
  50. ObjectPrx prx = adapter.add(new ZoneManagerCallback(), communicator().stringToIdentity("bs-" + ConstGame.GameServerId));
  51. ZoneIce.ice_getCachedConnection().setAdapter(adapter);
  52. //-3: 未知异常
  53. //-2: 异常参数;
  54. //-1: 已有在线的,拒绝;
  55. //0 : 加入成功
  56. //1 : 加入成功,重连
  57. int code = ZoneIce.setCallback(prx.ice_getIdentity(), ConstGame.GameServerUUID);
  58. if (code < 0)
  59. {
  60. Log.Error($"战斗服连接异常: {code}");
  61. return 2;
  62. }
  63. //向战斗服注册本GameServer
  64. string res = ZoneIce.registerGameServer(ConstGame.GameServerId, ConstGame.GameServerId);
  65. BattleIceAgentComponent.Instance.StrBattleServerVersion = res;
  66. Log.Info($"Battle Server version: {res}");
  67. }
  68. catch (Ice.Exception e)
  69. {
  70. Log.Error($"BattleIceAgentComponent ZoneManagerPrx Exception.{e.Message}");
  71. return 3;
  72. }
  73. try
  74. {
  75. XmdsManagerPrx XmdsIce = XmdsManagerPrxHelper.checkedCast(communicator().propertyToProxy("XmdsManager.Proxy"));
  76. if (XmdsIce == null)
  77. {
  78. Log.Error("not found ice proxy: XmdsManager");
  79. return 4;
  80. }
  81. Log.Debug("got XmdsManager.Proxy");
  82. BattleIceAgentComponent.Instance.IceXmdsManager = XmdsIce;
  83. XmdsIce.ice_invocationTimeout(15000);
  84. }
  85. catch (Ice.Exception e)
  86. {
  87. Log.Error($"BattleIceAgentComponent XmdsManagerPrx Exception.{e.Message}");
  88. return 5;
  89. }
  90. //连接战斗服FastStream组件
  91. BattleIceAgentComponent.Instance.DomainScene().AddComponent<FastStreamComponent>();
  92. IceApp.communicator().waitForShutdown();
  93. Log.Info("ice thread end");
  94. return 0;
  95. }
  96. }
  97. [ObjectSystem]
  98. private class ZoneManagerCallback: ZoneManagerCallbackDisp_
  99. {
  100. public override void eventNotify(string eventType, string msg, Current current__)
  101. {
  102. switch (eventType)
  103. {
  104. case "areaEvent":
  105. case "zoneEvent":
  106. {
  107. BattleServerEventHelper.AreaBattleServerEvent(JObject.Parse(msg));
  108. return;
  109. }
  110. case "playerEvent":
  111. {
  112. BattleServerEventHelper.PlayerBattleServerEvent(JObject.Parse(msg));
  113. return;
  114. }
  115. case "taskEvent":
  116. {
  117. BattleServerEventHelper.TaskBattleServerEvent(JObject.Parse(msg));
  118. return;
  119. }
  120. case "mapNotify":
  121. {
  122. BattleServerEventHelper.MapNotifyEvent(JObject.Parse(msg));
  123. return;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }