/// <summary> /// 初始化服务实例 /// </summary> /// <returns></returns> public override bool Init(string[] args) { if (base.Init(args) == false) { return(false); } #region 初始化服务配置 //初始化LOG日志配置 SvLogger.Init(XmlConfigure.Instance.GetAppConfigString("LogFile"), XmlConfigure.Instance.GetAppConfigString("LogFileLevel"), XmlConfigure.Instance.GetAppConfigString("LogConsoleLevel")); ServerConfigData.ListenPort = XmlConfigure.Instance.GetAppConfigInt("ListenPort"); ServerConfigData.DBIP = XmlConfigure.Instance.GetAppConfigString("DBIP"); ServerConfigData.DBPort = XmlConfigure.Instance.GetAppConfigInt("DBPort"); ServerConfigData.DBName = XmlConfigure.Instance.GetAppConfigString("DBName"); ServerConfigData.DBUser = XmlConfigure.Instance.GetAppConfigString("DBUser"); ServerConfigData.DBPassword = XmlConfigure.Instance.GetAppConfigString("DBPassword"); ServerConfigData.OSSPath = XmlConfigure.Instance.GetAppConfigString("OSSPath"); ServerConfigData.OSSUser = XmlConfigure.Instance.GetAppConfigString("OSSUser"); ServerConfigData.OSSPassword = XmlConfigure.Instance.GetAppConfigString("OSSPassword"); #endregion #region 网络组更新启动 try { SvLogger.Info("<< Start Network Listen : OutSidePort={0}.", ServerConfigData.ListenPort); ServerCommon.Network.XDNetworkManager.Instance.InitConfig(ServerConfigData.ListenPort); ServerCommon.Network.XDNetworkManager.Instance.StartRun(); } catch (Exception ex) { SvLogger.Fatal(ex, ">> ****** Network Listen Fail! Msg={0}", ex.Message); return(false); } SvLogger.Info(">> Network Listen Succ !!!"); #endregion #region 数据库连接 bool dbConnectBack = false; dbConnectBack = DBAccessCfg.Instance.InitDBConnection(eDbConnFlag.Game, ServerConfigData.DBIP, ServerConfigData.DBPort, ServerConfigData.DBName, ServerConfigData.DBUser, ServerConfigData.DBPassword); if (dbConnectBack == false) { SvLogger.Error("DB Access Config Deal Error!"); return(false); } #endregion #region 从数据库加载服务器组配置数据 //LoadDataFromMySqlManager.Instance.m_OnLoadAllTableSucc = onLoadConfigFromDBFinish; //LoadDataFromMySqlManager.Instance.LoadData(); #endregion return(true); }
private void run() { while (m_runThread) { m_stopwatch.Restart(); if (m_redisCliDisconnect == false) { try { if (commandDeal() > 0) { try { if (m_redisAccess.Reconnect() == true) { m_redisCliDisconnect = false; } } catch (Exception ex) { m_redisCliDisconnect = true; SvLogger.Fatal(ex, "{0}\n{1}.", ex.Message, ex.StackTrace); } } } catch (Exception ex) { if (m_redisAccess.RedisCli.IsSocketConnected() == false) { m_redisCliDisconnect = true; } SvLogger.Fatal(ex, "{0}\n{1}.", ex.Message, ex.StackTrace); } } else { try { if (m_redisAccess.Reconnect()) { m_redisCliDisconnect = false; } else { Thread.Sleep(1000); } } catch (Exception ex) { Thread.Sleep(1000); } } m_stopwatch.Stop(); if (m_stopwatch.Elapsed < m_maxSleepTS) { Thread.Sleep(m_maxSleepTS - m_stopwatch.Elapsed); } else { Thread.Sleep(1); } } commandDeal(); m_redisAccess.Close(); }
private eRedisAccessError commandDeal() { if (m_redisCliDisconnect == true) { return(eRedisAccessError.Connection_Is_Disconnect); } eRedisAccessError errID = eRedisAccessError.None; lock (m_mainThreadCommandListLuckObj) { if (m_mainThreadCommandList.Count > 0) { m_selfThreadCommandList.AddRange(m_mainThreadCommandList); m_mainThreadCommandList.Clear(); } } if (m_selfThreadCommandList.Count > 0) { int i = 0; for (; i < m_selfThreadCommandList.Count; ++i) { try { errID = m_selfThreadCommandList[i].DoCommand(m_redisAccess.RedisCli); if (errID != 0) { if (m_redisAccess.RedisCli.Ping() == false) { m_redisCliDisconnect = true; if (i > 0) { m_selfThreadCommandList.RemoveRange(0, i); i = 0; } break; } } if (m_selfThreadCommandList[i].NeedDealCallBack == true) { m_selfThreadDealedCommandList.Add(m_selfThreadCommandList[i]); } } catch (Exception ex) { errID = eRedisAccessError.Command_Exec_Exception; SvLogger.Fatal(ex, "Redis Command Deal Exception: {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace); } } if (i == m_selfThreadCommandList.Count) { m_selfThreadCommandList.Clear(); } } if (m_selfThreadDealedCommandList.Count > 0) { lock (m_mainThreadDealdCommandListLuckObj) { m_mainThreadDealedCommandList.AddRange(m_selfThreadDealedCommandList); } m_selfThreadDealedCommandList.Clear(); } return(errID); }
/// <summary> /// 重新向CenterServer注册失败 /// </summary> /// <param name="errCode">错误ID</param> /// <param name="errMsg">错误消息</param> public void OnReRegisterSvrFail(int errCode, string errMsg) { SvLogger.Error("OnReRegisterSvrFail: ErrCode={0}, ErrMsg={1}.", errCode, errMsg); ServerCommon.Network.NetworkManager.Instance.CloseServerConnection(m_protoData.m_SessionID); }
/// <summary> /// 初始化数据库连接 /// </summary> /// <param name="dbConnFlag">连接名</param> /// <param name="serverType">数据库类型</param> /// <param name="serverIp">数据库服务器名</param> /// <param name="serverPort">数据库端口</param> /// <param name="databaseName">数据库名</param> /// <param name="userName">数据库用户名</param> /// <param name="userPass">数据库用户密码</param> /// <param name="childConnCount">子连接个数</param> /// <returns>返回错误信息</returns> public int InitilizeDbConn(eDbConnFlag dbConnFlag, string serverType, string serverIp, string serverPort, string databaseName, string userName, string userPass, int childConnCount) { int errCode = 0; //最终Connect Flag生成 m_connFlagMakeLines[dbConnFlag] += 1; int lineIndex = Convert.ToInt32(m_connFlagMakeLines[dbConnFlag]); int connFlag = Convert.ToInt32(dbConnFlag) | lineIndex; DbAccessPool dapool = DbAccessPool.GetPool(connFlag); if (dapool != null) { errCode = 1111; SvLogger.Error("Had Same DB Connections : ConnectType={0}, ConnectFlag={1}.", dbConnFlag, connFlag); return(errCode); } //获取SQL日志记录池 DbAccessPool sqlogdapool = null; //if (dbConnFlag != eDbConnFlag.SqlLog) // sqlogdapool = DbAccessPool.GetPool(EnumDbConnName.SqlLog.ToString()); //建立数据库连接 DbAccessPoolVistor vistor = new DbAccessPoolVistor(connFlag); dapool = new DbAccessPool(connFlag, SvLogger.GetLogger(), vistor, sqlogdapool); try { dapool.Start(serverType, serverIp, serverPort, databaseName, userName, userPass); } catch (Exception ex) { SvLogger.Fatal(ex, "{0}\n{1}.", ex.Message, ex.StackTrace); errCode = 1111; return(errCode); } int mainFlag = dapool.Flag; if (dbConnFlag == eDbConnFlag.Game) { m_gameDBMainConnFlagList.Add(dapool.Flag); } else if (dbConnFlag == eDbConnFlag.Log) { m_logDbAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.Secd) { m_secdDbAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.GM) { m_gmDBAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.Platform) { m_platformDBAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.Moniteor) { m_moniteorDBAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.Entry) { m_entryDBAccPool = dapool; } else if (dbConnFlag == eDbConnFlag.GiftCode) { m_giftCodeDBAccPool = dapool; } //加入池 DbAccessPool.AddPool(dapool); //Game数据库子连接建立 if (dbConnFlag == eDbConnFlag.Game) { for (int i = 0; i < childConnCount; ++i) { //最终Connect Flag生成 m_connFlagMakeLines[dbConnFlag] += 1; lineIndex = Convert.ToInt32(m_connFlagMakeLines[dbConnFlag]); connFlag = Convert.ToInt32(dbConnFlag) | lineIndex; dapool = DbAccessPool.GetPool(connFlag); if (dapool != null) { errCode = 1111; SvLogger.Error("Had Same DB Connections : ConnectType={0}, ConnectFlag={1}.", dbConnFlag, connFlag); return(errCode); } //获取SQL日志记录池 sqlogdapool = null; //if (dbConnFlag != eDbConnFlag.SqlLog) // sqlogdapool = DbAccessPool.GetPool(EnumDbConnName.SqlLog.ToString()); //建立数据库连接 vistor = new DbAccessPoolVistor(connFlag); dapool = new DbAccessPool(connFlag, SvLogger.GetLogger(), vistor, sqlogdapool); try { dapool.Start(serverType, serverIp, serverPort, databaseName, userName, userPass); } catch (Exception ex) { SvLogger.Fatal(ex, "{0}\n{1}.", ex.Message, ex.StackTrace); errCode = 1111; return(errCode); } List <int> childList; if (m_gameDBChildConnFlagDic.TryGetValue(mainFlag, out childList) == false) { childList = new List <int>(); m_gameDBChildConnFlagDic.Add(mainFlag, childList); } childList.Add(dapool.Flag); //加入池 DbAccessPool.AddPool(dapool); } } return(errCode); }
/// <summary> /// 连接服务器失败 /// </summary> /// <param name="errcode">错误信息</param> /// <param name="svrinfo">连接的服务器信息</param> public void OnConnectSvrFail(int errcode, ref com.tieao.mmo.interval.PtServerInfo svrinfo) { InternalProtocolDealDelegate.Instance.OnConnectServerFail(svrinfo, ""); SvLogger.Info("OnConnectSvrFail : ServerType={0}, IP={1}, Port={2}, ErrCode={3}.", svrinfo.m_Type, svrinfo.m_Address, svrinfo.m_Port, errcode); }
/// <summary> /// 通知服务器关闭 /// </summary> /// <param name="param">关闭参数</param> /// <param name="sessionID"></param> public void OnShutDownSvr(string param) { //关闭服务 SvLogger.Info("OnShutDownSvr : param={0}", param); BasicService.RecvCenterServerCloseCommand(); }
/// <summary> /// 注册服务成功 /// </summary> /// <param name="svrinfo">注册服务信息</param> /// <param name="regedsvrlist">已经注册的服务信息列表</param> /// <param name="sessionID"></param> public void OnRegisterSvrOK(ref com.tieao.mmo.interval.PtServerInfo svrinfo, ref com.tieao.mmo.interval.PtServerList regedsvrlist) { SvLogger.Info("OnRegisterSvrOK Begin: ServerType={0}, ServerName={1}, RegedServerCount={2}, sessionID={3}.", svrinfo.m_Type, svrinfo.m_Name, regedsvrlist.GetElements().Count, m_protoData.m_SessionID); List <eServerType> connectSvrTypeList = new List <eServerType>(); svrinfo.m_SessionID = m_protoData.m_SessionID; int errCode = RegServerManager.Instance.RegServer(ref svrinfo); if (errCode != 0) { SvLogger.Error(" RegisterFail : ErrCode={0}.", errCode); InternalProtocolDealDelegate.Instance.OnRegistServerSucc(svrinfo, errCode.ToString()); } else { if (svrinfo.m_Type == eServerType.CENTER) { SvrCommCfg.Instance.ServerInfo.m_ServerID = svrinfo.m_ServerID; } switch (RegServerManager.Instance.m_SelfSvrInfo.m_Type) { case eServerType.GATEWAY: { connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.WORLDBOSS); connectSvrTypeList.Add(eServerType.CHAT); //connectSvrTypeList.Add(eServerType.PUBLIC); } break; case eServerType.GAME: { connectSvrTypeList.Add(eServerType.GATEWAY); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.RANK); connectSvrTypeList.Add(eServerType.LOG); connectSvrTypeList.Add(eServerType.WORLDBOSS); connectSvrTypeList.Add(eServerType.CHAT); } break; case eServerType.CENTER: { } break; case eServerType.GAMEDATA: { connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.DATABASE); connectSvrTypeList.Add(eServerType.LOG); connectSvrTypeList.Add(eServerType.WORLDBOSS); connectSvrTypeList.Add(eServerType.CHAT); } break; case eServerType.PUBLIC: { //connectSvrTypeList.Add(eServerType.GATEWAY); connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.DATABASE); connectSvrTypeList.Add(eServerType.RANK); connectSvrTypeList.Add(eServerType.WORLDBOSS); connectSvrTypeList.Add(eServerType.LOG); connectSvrTypeList.Add(eServerType.CHAT); } break; case eServerType.DATABASE: { connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.LOG); } break; case eServerType.RANK: { connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.PUBLIC); } break; case eServerType.LOG: { connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.DATABASE); connectSvrTypeList.Add(eServerType.CHAT); } break; case eServerType.CROSSREALM: break; case eServerType.CROSSREALM_BATTLE: break; case eServerType.PROTAL: break; case eServerType.MONITEORNODE: break; case eServerType.GM: break; case eServerType.PLATFORM_DOCKING: break; case eServerType.MONITEORSERVER: break; case eServerType.WORLDBOSS: { connectSvrTypeList.Add(eServerType.GATEWAY); connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.LOG); connectSvrTypeList.Add(eServerType.CHAT); } break; case eServerType.CHAT: { connectSvrTypeList.Add(eServerType.GATEWAY); connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.LOG); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.WORLDBOSS); } break; case eServerType.COMMANDSENDER: { connectSvrTypeList.Add(eServerType.DATABASE); connectSvrTypeList.Add(eServerType.GAMEDATA); connectSvrTypeList.Add(eServerType.GAME); connectSvrTypeList.Add(eServerType.PUBLIC); connectSvrTypeList.Add(eServerType.RANK); connectSvrTypeList.Add(eServerType.GATEWAY); connectSvrTypeList.Add(eServerType.CHAT); connectSvrTypeList.Add(eServerType.WORLDBOSS); } break; case eServerType.UNKNOW: SvLogger.Error(" This Server Type Unknow!"); break; } for (int i = 0; i < regedsvrlist.GetElements().Count; ++i) { if (connectSvrTypeList.Contains(regedsvrlist.GetElements()[i].m_Type)) { bool result = Network.NetworkManager.Instance.OpenRegServerConnection(regedsvrlist.GetElements()[i]); if (result == true) { SvLogger.Info(" Start To Connect Server : ServerType={0}, IP={1}, Port={2}.", regedsvrlist.GetElements()[i].m_Type.ToString(), regedsvrlist.GetElements()[i].m_Address, regedsvrlist.GetElements()[i].m_Port); } else { SvLogger.Error(" Connect To Server Fail : ServerType={0}, IP={1}, Port={2}.", regedsvrlist.GetElements()[i].m_Type.ToString(), regedsvrlist.GetElements()[i].m_Address, regedsvrlist.GetElements()[i].m_Port); } } } if (Network.NetworkManager.Instance.IsNeedRegistServer(svrinfo.m_Type)) { Network.NetworkManager.Instance.RegistToServerSucc(m_protoData.m_SessionID, ref svrinfo); } InternalProtocolDealDelegate.Instance.OnRegistServerSucc(svrinfo, "0"); } SvLogger.Info("OnRegisterSvrOK Done: ServerType={0}, ServerName={1}, NeedConnectServerCount={2}, sessionID={3}.", svrinfo.m_Type, svrinfo.m_Name, connectSvrTypeList.Count, m_protoData.m_SessionID); }
/// <summary> /// 初始化连接 /// </summary> /// <returns>是否成功</returns> public bool InitDBConnections() { //配置载入 XmlDocument doc = new XmlDocument(); try { doc.Load(@"Config\DatabaseCfg.xml"); XmlNode root = doc.SelectSingleNode("XmlAppConfig"); if (root == null) { return(false); } else { XmlNodeList dbNodeList = root.SelectNodes("DBItem"); foreach (XmlNode dbNode in dbNodeList) { eDbConnFlag connFlag = eDbConnFlag.None; string dbTypeName = dbNode.Attributes["name"].Value; switch (dbTypeName) { case "GameDB": connFlag = eDbConnFlag.Game; break; case "LogDB": connFlag = eDbConnFlag.Log; break; case "SecdDB": connFlag = eDbConnFlag.Secd; break; case "GMDB": connFlag = eDbConnFlag.GM; break; } if (connFlag != eDbConnFlag.None) { string dbType = "", serverIP = "", serverPort = "", databaseName = "", userName = "", UserPass = "", childConnCount = ""; XmlNodeList cfgNodeList = dbNode.SelectNodes("config"); foreach (XmlNode cfgNode in cfgNodeList) { switch (cfgNode.Attributes["key"].Value) { case "DbType": dbType = cfgNode.Attributes["value"].Value; break; case "ServerIP": serverIP = cfgNode.Attributes["value"].Value; break; case "ServerPort": serverPort = cfgNode.Attributes["value"].Value; break; case "DatabaseName": databaseName = cfgNode.Attributes["value"].Value; break; case "UserName": userName = cfgNode.Attributes["value"].Value; break; case "UserPass": UserPass = cfgNode.Attributes["value"].Value; break; case "ChildConnCount": childConnCount = cfgNode.Attributes["value"].Value; break; } } int errCode = DbAccess.Instance.InitilizeDbConn(connFlag, dbType, serverIP, serverPort, databaseName, userName, UserPass, Convert.ToInt32(childConnCount)); if (errCode != 0) { return(false); } } } } } catch (Exception ex) { SvLogger.Fatal(ex, "Load Fail Fatal: ErrMsg={0}, Stack={1}.", ex.Message, ex.StackTrace); return(false); } return(true); }
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { SvLogger.Fatal((Exception)e.ExceptionObject, "UnhandledException"); }