123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System;
- using System.Collections.Generic;
- using Ice;
- using System.Web.Helpers;
- using CommonLang;
- namespace Pomelo
- {
- public class IceLogger : Ice.Logger
- {
- private log4net.ILog logger;
- public IceLogger()
- {
- this.logger = log4net.LogManager.GetLogger("Ice");
- }
- public Logger cloneWithPrefix(string prefix)
- {
- throw new NotImplementedException();
- }
- public void error(string message)
- {
- this.logger.Error(message);
- }
- public string getPrefix()
- {
- throw new NotImplementedException();
- }
- public void print(string message)
- {
- this.logger.Info(message);
- }
- public void trace(string category, string message)
- {
- //this.logger.Debug(message);
- }
- public void warning(string message)
- {
- this.logger.Warn(message);
- }
- }
- public class IceConfig
- {
- /// <summary>
- /// 启动host
- /// </summary>
- public string host = "127.0.0.1";
- /// <summary>
- /// 启动端口
- /// </summary>
- public int port;
- /// <summary>
- /// 是否输出连接警告
- /// </summary>
- public bool isWarnConnections = true;
- /// <summary>
- /// 是否输出网络日志
- /// </summary>
- public bool isTraceNetwork = false;
- /// <summary>
- /// 是否输出协议日志
- /// </summary>
- public bool isTraceProtocol = false;
- }
- public class ICEZoneSession
- {
- private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("ICEZoneSession");
- public ZoneManagerCallbackPrx callback;
- public IFastSession fastSession;
- public int failTimes = 0;
- public ICEZoneSession(ZoneManagerCallbackPrx callback)
- {
- try
- {
- this.callback = callback;
- this.failTimes = 0;
- }
- catch(System.Exception e)
- {
- log.Error("ICEZoneSession create catch :" + e);
- }
- }
- public bool setFastSession(IFastSession session)
- {
- this.fastSession = session;
- this.failTimes = 0;
- return true;
- }
- }
- public class IceManager : Ice.Application
- {
- private static IceManager _instance;
- private CommonLang.Log.Logger log = CommonLang.Log.LoggerFactory.GetLogger("IceManager");
- private Dictionary<string, Ice.ObjectImpl> proxys;
- /// <summary>
- /// 战斗服到游戏服ICE通知
- /// </summary>
- private HashMap<string, ICEZoneSession> callbacks = new HashMap<string, ICEZoneSession>();
- public ICEZoneSession getCallback(string gameServerId)
- {
- lock (callbacks)
- {
- return callbacks.Get(gameServerId);
- }
- }
- public int setCallback(string gameServerId, ZoneManagerCallbackPrx callback)
- {
- lock (callbacks)
- {
- ICEZoneSession cacheSession = callbacks.Get(gameServerId);
- if (cacheSession != null && cacheSession.callback != null)
- {
- try
- {
- cacheSession.callback.ice_ping();
- log.Error("已有服务器在线ice, 拒绝连接!");
- return 1;
- }
- catch(System.Exception)
- {
- log.Error("ice连入,远端已经失效,重新设置!");
- cacheSession.callback = callback;
- return 0;
- }
- }
- else
- {
- callbacks.Put(gameServerId, new ICEZoneSession(callback));
- }
- }
- return 0;
- }
- public bool setFastSession(string gameServerId, IFastSession session)
- {
- lock (callbacks)
- {
- ICEZoneSession cacheSession = callbacks.Get(gameServerId);
- if (cacheSession != null)
- {
- if(cacheSession.fastSession != null && cacheSession.fastSession.IsConnected())
- {
- ///sesion.fastSession.doClose();
- log.Error("已有服务器在线socket:" + gameServerId + ", " + cacheSession.fastSession.GetDescribe() + ", " + session.GetDescribe());
- return false;
- }
- return cacheSession.setFastSession(session);
- }
- else
- {
- log.Error("setFastSession 找不到ice信息:" + gameServerId + ", " + session.ConnectorId + ", " + session.GetDescribe());
- return false;
- }
- }
- }
- //private ZoneManagerCallbackPrx callback;
- //public ZoneManagerCallbackPrx Callback
- //{
- // get { return callback; }
- // set { callback = value; }
- //}
- /// <summary>
- /// 单件实例
- /// </summary>
- /// <returns></returns>
- public static IceManager instance()
- {
- if (_instance == null)
- {
- _instance = new IceManager();
- }
- return _instance;
- }
- public override int run(string[] args)
- {
- if (args.Length > 0)
- {
- log.Error(appName() + ": too many arguments");
- return 1;
- }
- Ice.ObjectAdapter adapter = communicator().createObjectAdapter("CSharp");
- foreach (var e in proxys)
- {
- adapter.add(e.Value, communicator().stringToIdentity(e.Key));
- }
- adapter.activate();
- log.Info("ICE started !");
- communicator().waitForShutdown();
- return 0;
- }
- public int Start(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
- {
- this.proxys = proxys;
- Ice.InitializationData initData = new Ice.InitializationData();
- //设置日志
- initData.logger = new IceLogger();
- initData.logger.print(Json.Encode(config));
- //设置参数
- initData.properties = Ice.Util.createProperties();
- initData.properties.setProperty("CSharp.Endpoints", "tcp -p " + config.port);
- initData.properties.setProperty("Ice.Default.Host", config.host);
- initData.properties.setProperty("Ice.Warn.Connections", config.isWarnConnections ? "1" : "0");
- initData.properties.setProperty("Ice.ACM.Close", "0");
- //Ice.ACM.Timeout没有,相当于无用设置
- initData.properties.setProperty("Ice.ACM.Heartbeat", "3");
- initData.properties.setProperty("Ice.ThreadPool.Server.Size", "4");
- initData.properties.setProperty("Ice.ThreadPool.Server.SizeMax", "8");
- initData.properties.setProperty("Ice.MessageSizeMax", "10240");
- //initData.properties.setProperty("Ice.RetryIntervals", "0 500 2000 5000");
- initData.properties.setProperty("Ice.Trace.Network", config.isTraceNetwork ? "1" : "0");
- initData.properties.setProperty("Ice.Trace.Protocol", config.isTraceProtocol ? "1" : "0");
- //initData.properties.setProperty("Ice.Trace.Retry", "2");
- var thread = new System.Threading.Thread(() =>
- {
- log.Warn("ICE starting at " + config.host + ":" + config.port);
- this.main(new string[0], initData);
- });
- thread.Name = "ICE run";
- thread.Start();
- return 0;
- }
- public void Stop()
- {
- communicator().shutdown();
- communicator().destroy();
- }
- //public void eventNotify(string eventType, string msg)
- //{
- // if (callback != null)
- // {
- // callback.eventNotify(eventType, msg);
- // }
- //}
- //private IceManager(IceConfig config, Dictionary<string, Ice.ObjectImpl> proxys)
- //{
- // this.config = config;
- // this.proxys = proxys;
- //}
- private IceManager()
- {
- }
- }
- }
|