IceManager.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using Ice;
  4. using System.Web.Helpers;
  5. using CommonLang;
  6. namespace Pomelo
  7. {
  8. public class IceLogger : Ice.Logger
  9. {
  10. private log4net.ILog logger;
  11. public IceLogger()
  12. {
  13. this.logger = log4net.LogManager.GetLogger("Ice");
  14. }
  15. public Logger cloneWithPrefix(string prefix)
  16. {
  17. throw new NotImplementedException();
  18. }
  19. public void error(string message)
  20. {
  21. this.logger.Error(message);
  22. }
  23. public string getPrefix()
  24. {
  25. throw new NotImplementedException();
  26. }
  27. public void print(string message)
  28. {
  29. this.logger.Info(message);
  30. }
  31. public void trace(string category, string message)
  32. {
  33. //this.logger.Debug(message);
  34. }
  35. public void warning(string message)
  36. {
  37. this.logger.Warn(message);
  38. }
  39. }
  40. public class IceConfig
  41. {
  42. /// <summary>
  43. /// 启动host
  44. /// </summary>
  45. public string host = "127.0.0.1";
  46. /// <summary>
  47. /// 启动端口
  48. /// </summary>
  49. public int port;
  50. /// <summary>
  51. /// 是否输出连接警告
  52. /// </summary>
  53. public bool isWarnConnections = true;
  54. /// <summary>
  55. /// 是否输出网络日志
  56. /// </summary>
  57. public bool isTraceNetwork = false;
  58. /// <summary>
  59. /// 是否输出协议日志
  60. /// </summary>
  61. public bool isTraceProtocol = false;
  62. }
  63. public class ICEZoneSession
  64. {
  65. private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("ICEZoneSession");
  66. public ZoneManagerCallbackPrx callback;
  67. public IFastSession fastSession;
  68. public int failTimes = 0;
  69. public ICEZoneSession(ZoneManagerCallbackPrx callback)
  70. {
  71. try
  72. {
  73. this.callback = callback;
  74. this.failTimes = 0;
  75. }
  76. catch(System.Exception e)
  77. {
  78. log.Error("ICEZoneSession create catch :" + e);
  79. }
  80. }
  81. public bool setFastSession(IFastSession session)
  82. {
  83. this.fastSession = session;
  84. this.failTimes = 0;
  85. return true;
  86. }
  87. }
  88. public class IceManager : Ice.Application
  89. {
  90. private static IceManager _instance;
  91. private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("IceManager");
  92. private Dictionary<string, Ice.ObjectImpl> proxys;
  93. /// <summary>
  94. /// 战斗服到游戏服ICE通知
  95. /// </summary>
  96. private HashMap<string, ICEZoneSession> callbacks = new HashMap<string, ICEZoneSession>();
  97. public ICEZoneSession getCallback(string gameServerId)
  98. {
  99. lock (callbacks)
  100. {
  101. return callbacks.Get(gameServerId);
  102. }
  103. }
  104. public int setCallback(string gameServerId, ZoneManagerCallbackPrx callback)
  105. {
  106. lock (callbacks)
  107. {
  108. ICEZoneSession cacheSession = callbacks.Get(gameServerId);
  109. if (cacheSession != null && cacheSession.callback != null)
  110. {
  111. try
  112. {
  113. cacheSession.callback.ice_ping();
  114. log.Error("已有服务器在线ice, 拒绝连接!");
  115. return 1;
  116. }
  117. catch(System.Exception)
  118. {
  119. log.Error("ice连入,远端已经失效,重新设置!");
  120. cacheSession.callback = callback;
  121. return 0;
  122. }
  123. }
  124. else
  125. {
  126. callbacks.Put(gameServerId, new ICEZoneSession(callback));
  127. }
  128. }
  129. return 0;
  130. }
  131. public bool setFastSession(string gameServerId, IFastSession session)
  132. {
  133. lock (callbacks)
  134. {
  135. ICEZoneSession cacheSession = callbacks.Get(gameServerId);
  136. if (cacheSession != null)
  137. {
  138. if(cacheSession.fastSession != null && cacheSession.fastSession.IsConnected())
  139. {
  140. ///sesion.fastSession.doClose();
  141. log.Error("已有服务器在线socket:" + gameServerId + ", " + cacheSession.fastSession.GetDescribe() + ", " + session.GetDescribe());
  142. return false;
  143. }
  144. return cacheSession.setFastSession(session);
  145. }
  146. else
  147. {
  148. log.Error("setFastSession 找不到ice信息:" + gameServerId + ", " + session.ConnectorId + ", " + session.GetDescribe());
  149. return false;
  150. }
  151. }
  152. }
  153. //private ZoneManagerCallbackPrx callback;
  154. //public ZoneManagerCallbackPrx Callback
  155. //{
  156. // get { return callback; }
  157. // set { callback = value; }
  158. //}
  159. /// <summary>
  160. /// 单件实例
  161. /// </summary>
  162. /// <returns></returns>
  163. public static IceManager instance()
  164. {
  165. if (_instance == null)
  166. {
  167. _instance = new IceManager();
  168. }
  169. return _instance;
  170. }
  171. public override int run(string[] args)
  172. {
  173. if (args.Length > 0)
  174. {
  175. log.Error(appName() + ": too many arguments");
  176. return 1;
  177. }
  178. Ice.ObjectAdapter adapter = communicator().createObjectAdapter("CSharp");
  179. foreach (var e in proxys)
  180. {
  181. adapter.add(e.Value, communicator().stringToIdentity(e.Key));
  182. }
  183. adapter.activate();
  184. log.Info("ICE started !");
  185. communicator().waitForShutdown();
  186. return 0;
  187. }
  188. public int Start(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
  189. {
  190. this.proxys = proxys;
  191. Ice.InitializationData initData = new Ice.InitializationData();
  192. //设置日志
  193. initData.logger = new IceLogger();
  194. initData.logger.print(Json.Encode(config));
  195. //设置参数
  196. initData.properties = Ice.Util.createProperties();
  197. initData.properties.setProperty("CSharp.Endpoints", "tcp -p " + config.port);
  198. initData.properties.setProperty("Ice.Default.Host", config.host);
  199. initData.properties.setProperty("Ice.Warn.Connections", config.isWarnConnections ? "1" : "0");
  200. initData.properties.setProperty("Ice.ACM.Close", "0");
  201. //Ice.ACM.Timeout没有,相当于无用设置
  202. initData.properties.setProperty("Ice.ACM.Heartbeat", "3");
  203. initData.properties.setProperty("Ice.ThreadPool.Server.Size", "4");
  204. initData.properties.setProperty("Ice.ThreadPool.Server.SizeMax", "8");
  205. initData.properties.setProperty("Ice.MessageSizeMax", "10240");
  206. //initData.properties.setProperty("Ice.RetryIntervals", "0 500 2000 5000");
  207. initData.properties.setProperty("Ice.Trace.Network", config.isTraceNetwork ? "1" : "0");
  208. initData.properties.setProperty("Ice.Trace.Protocol", config.isTraceProtocol ? "1" : "0");
  209. //initData.properties.setProperty("Ice.Trace.Retry", "2");
  210. var thread = new System.Threading.Thread(() =>
  211. {
  212. log.Warn("ICE starting at " + config.host + ":" + config.port);
  213. this.main(new string[0], initData);
  214. });
  215. thread.Name = "ICE run";
  216. thread.Start();
  217. return 0;
  218. }
  219. public void Stop()
  220. {
  221. communicator().shutdown();
  222. communicator().destroy();
  223. }
  224. //public void eventNotify(string eventType, string msg)
  225. //{
  226. // if (callback != null)
  227. // {
  228. // callback.eventNotify(eventType, msg);
  229. // }
  230. //}
  231. //private IceManager(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
  232. //{
  233. // this.config = config;
  234. // this.proxys = proxys;
  235. //}
  236. private IceManager()
  237. {
  238. }
  239. }
  240. }