IceManager.cs 11 KB

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