IceManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using Ice;
  4. using System.Web.Helpers;
  5. using CommonLang;
  6. using System.Collections.Concurrent;
  7. using Pomelo.JSGModule;
  8. using BattleIce;
  9. namespace Pomelo
  10. {
  11. public class IceLogger : Ice.Logger
  12. {
  13. private log4net.ILog logger;
  14. public IceLogger()
  15. {
  16. this.logger = log4net.LogManager.GetLogger("Ice");
  17. }
  18. public Logger cloneWithPrefix(string prefix)
  19. {
  20. throw new NotImplementedException();
  21. }
  22. public void error(string message)
  23. {
  24. this.logger.Error(message);
  25. }
  26. public string getPrefix()
  27. {
  28. throw new NotImplementedException();
  29. }
  30. public void print(string message)
  31. {
  32. this.logger.Info(message);
  33. }
  34. public void trace(string category, string message)
  35. {
  36. //this.logger.Debug(message);
  37. }
  38. public void warning(string message)
  39. {
  40. this.logger.Warn(message);
  41. }
  42. }
  43. public class IceConfig
  44. {
  45. /// <summary>
  46. /// 启动host
  47. /// </summary>
  48. public string host = "127.0.0.1";
  49. /// <summary>
  50. /// 启动端口
  51. /// </summary>
  52. public int port;
  53. /// <summary>
  54. /// 是否输出连接警告
  55. /// </summary>
  56. public bool isWarnConnections = true;
  57. /// <summary>
  58. /// 是否输出网络日志
  59. /// </summary>
  60. public bool isTraceNetwork = false;
  61. /// <summary>
  62. /// 是否输出协议日志
  63. /// </summary>
  64. public bool isTraceProtocol = false;
  65. }
  66. /** ice通知消息信息 */
  67. public class ICENotifyData
  68. {
  69. public string name;
  70. public string jsonData; // 通知数据
  71. public long failTime; // 失败时间
  72. public ICENotifyData(string name, string jsonData)
  73. {
  74. this.name = name;
  75. this.jsonData = jsonData;
  76. this.failTime = CommonLang.TimeUtil.GetTimestampMS();
  77. }
  78. }
  79. public class ICEZoneSession
  80. {
  81. private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("ICEZoneSession");
  82. public string srvUUID;
  83. public ZoneManagerCallbackPrx client;
  84. public ZoneManagerCallbackPrx callback;
  85. public IFastSession fastSession;
  86. public int failTimes = 0;
  87. public ConcurrentQueue<ICENotifyData> notifyFailData = new ConcurrentQueue<ICENotifyData>();
  88. public ICEZoneSession(string srvUUID, ZoneManagerCallbackPrx client, ZoneManagerCallbackPrx callback)
  89. {
  90. try
  91. {
  92. this.srvUUID = srvUUID;
  93. this.client = client;
  94. this.callback = callback;
  95. this.failTimes = 0;
  96. }
  97. catch(System.Exception e)
  98. {
  99. log.Error("ICEZoneSession create catch :" + e);
  100. }
  101. }
  102. public bool setFastSession(IFastSession session)
  103. {
  104. this.fastSession = session;
  105. this.failTimes = 0;
  106. return true;
  107. }
  108. /** 重新通知所有时间 */
  109. public void RetryFailNotifys()
  110. {
  111. try
  112. {
  113. this.callback.ice_ping();
  114. }
  115. catch (System.Exception e)
  116. {
  117. log.Warn("RetryFailNotifys ping 异常:", e);
  118. }
  119. try
  120. {
  121. log.Info("RetryFailNotifys start begin 1: " + notifyFailData.Count);
  122. this.callback.ice_ping();
  123. long ltimeEnd = CommonLang.TimeUtil.GetTimestampMS() - 120000;
  124. while (notifyFailData.Count > 0)
  125. {
  126. ICENotifyData notifyData = new ICENotifyData("", "");
  127. if (notifyFailData.TryDequeue(out notifyData) && ltimeEnd < notifyData.failTime)
  128. {
  129. log.Info("RetryFailNotifys start begin 2: " + notifyFailData.Count + ", " + notifyData.name + ", " + notifyData.jsonData + ", time: " + notifyData.failTime);
  130. this.callback.eventNotify(notifyData.name, notifyData.jsonData);
  131. }
  132. else
  133. {
  134. log.Info("RetryFailNotifys start begin 3 skip: " + notifyFailData.Count + ", " + notifyData.name + ", " + notifyData.jsonData + ", time: " + notifyData.failTime);
  135. }
  136. }
  137. }
  138. catch(System.Exception e)
  139. {
  140. log.Error("RetryFailNotifys catch: " + e.Message, e);
  141. }
  142. }
  143. }
  144. public class IceManager : Ice.Application
  145. {
  146. private static IceManager _instance;
  147. private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("IceManager");
  148. private Dictionary<string, Ice.ObjectImpl> proxys;
  149. /// <summary>
  150. /// 战斗服到游戏服ICE通知
  151. /// </summary>
  152. private HashMap<string, ICEZoneSession> callbacks = new HashMap<string, ICEZoneSession>();
  153. public ICEZoneSession getCallback(string gameServerId)
  154. {
  155. lock (callbacks)
  156. {
  157. return callbacks.Get(gameServerId);
  158. }
  159. }
  160. public int setCallback(string gameServerId, string srvUUID, ZoneManagerCallbackPrx client, ZoneManagerCallbackPrx callback)
  161. {
  162. lock (callbacks)
  163. {
  164. ICEZoneSession cacheSession = callbacks.Get(gameServerId);
  165. if (cacheSession != null && cacheSession.callback != null)
  166. {
  167. try
  168. {
  169. cacheSession.callback.ice_ping();
  170. log.Error("已有服务器在线ice, 拒绝连接!");
  171. return GSBindResult.ALREADY_EXISTS;
  172. }
  173. catch(System.Exception)
  174. {
  175. try
  176. {
  177. log.Error("ice连入,远端已经失效,重新设置!");
  178. cacheSession.callback = callback;
  179. bool reconnect = cacheSession.srvUUID.Equals(srvUUID);
  180. //重连处理失败的逻辑
  181. if (reconnect)
  182. {
  183. cacheSession.RetryFailNotifys();
  184. }
  185. cacheSession.srvUUID = srvUUID;
  186. return reconnect ? GSBindResult.JOIN_RECONNECT : GSBindResult.JOIN_OK;
  187. }
  188. catch (System.Exception e)
  189. {
  190. log.Error("setCallback未处理异常:", e);
  191. return GSBindResult.UNKOWN;
  192. }
  193. }
  194. }
  195. else
  196. {
  197. callbacks.Put(gameServerId, new ICEZoneSession(srvUUID, client, callback));
  198. }
  199. }
  200. return GSBindResult.JOIN_OK;
  201. }
  202. public bool setFastSession(string gameServerId, IFastSession session)
  203. {
  204. lock (callbacks)
  205. {
  206. ICEZoneSession cacheSession = callbacks.Get(gameServerId);
  207. if (cacheSession != null)
  208. {
  209. if(cacheSession.fastSession != null && cacheSession.fastSession.IsConnected())
  210. {
  211. ///sesion.fastSession.doClose();
  212. log.Error("已有服务器在线socket:" + gameServerId + ", " + cacheSession.fastSession.GetDescribe() + ", " + session.GetDescribe());
  213. return false;
  214. }
  215. return cacheSession.setFastSession(session);
  216. }
  217. else
  218. {
  219. log.Error("setFastSession 找不到ice信息:" + gameServerId + ", " + session.ConnectorId + ", " + session.GetDescribe());
  220. return false;
  221. }
  222. }
  223. }
  224. //private ZoneManagerCallbackPrx callback;
  225. //public ZoneManagerCallbackPrx Callback
  226. //{
  227. // get { return callback; }
  228. // set { callback = value; }
  229. //}
  230. /// <summary>
  231. /// 单件实例
  232. /// </summary>
  233. /// <returns></returns>
  234. public static IceManager instance()
  235. {
  236. if (_instance == null)
  237. {
  238. _instance = new IceManager();
  239. }
  240. return _instance;
  241. }
  242. public override int run(string[] args)
  243. {
  244. if (args.Length > 0)
  245. {
  246. log.Error(appName() + ": too many arguments");
  247. return 1;
  248. }
  249. Ice.ObjectAdapter adapter = communicator().createObjectAdapter("CSharp");
  250. foreach (var e in proxys)
  251. {
  252. log.Warn($"add ice adapter: {e.Key}");
  253. adapter.add(e.Value, communicator().stringToIdentity(e.Key));
  254. }
  255. adapter.activate();
  256. log.Info("ICE started !");
  257. communicator().waitForShutdown();
  258. return 0;
  259. }
  260. public int Start(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
  261. {
  262. this.proxys = proxys;
  263. Ice.InitializationData initData = new Ice.InitializationData();
  264. //设置日志
  265. initData.logger = new IceLogger();
  266. initData.logger.print(Json.Encode(config));
  267. //设置参数
  268. initData.properties = Ice.Util.createProperties();
  269. initData.properties.setProperty("CSharp.Endpoints", "tcp -p " + config.port);
  270. initData.properties.setProperty("Ice.Default.Host", config.host);
  271. initData.properties.setProperty("Ice.Warn.Connections", config.isWarnConnections ? "1" : "0");
  272. initData.properties.setProperty("Ice.ACM.Close", "0");
  273. //Ice.ACM.Timeout没有,相当于无用设置
  274. initData.properties.setProperty("Ice.ACM.Heartbeat", "3");
  275. initData.properties.setProperty("Ice.ThreadPool.Server.Size", "4");
  276. initData.properties.setProperty("Ice.ThreadPool.Server.SizeMax", "8");
  277. initData.properties.setProperty("Ice.MessageSizeMax", "10240");
  278. initData.properties.setProperty("Ice.RetryIntervals", "0 500 2000 5000");
  279. initData.properties.setProperty("Ice.Trace.Network", config.isTraceNetwork ? "1" : "0");
  280. initData.properties.setProperty("Ice.Trace.Protocol", config.isTraceProtocol ? "1" : "0");
  281. initData.properties.setProperty("Ice.Trace.Retry", "2");
  282. var thread = new System.Threading.Thread(() =>
  283. {
  284. log.Warn("ICE starting at " + config.host + ":" + config.port);
  285. this.main(new string[0], initData);
  286. });
  287. thread.Name = "ICE run";
  288. thread.Start();
  289. return 0;
  290. }
  291. public void Stop()
  292. {
  293. communicator().shutdown();
  294. communicator().destroy();
  295. }
  296. //public void eventNotify(string eventType, string msg)
  297. //{
  298. // if (callback != null)
  299. // {
  300. // callback.eventNotify(eventType, msg);
  301. // }
  302. //}
  303. //private IceManager(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
  304. //{
  305. // this.config = config;
  306. // this.proxys = proxys;
  307. //}
  308. private IceManager()
  309. {
  310. }
  311. }
  312. }