/// <summary> /// 检查调用保护,返回剩余秒数 /// </summary> /// <param name="campid">匹配ID</param> /// <param name="calledOper">调用操作</param> /// <param name="allowInterval">允许的间隔,毫秒级</param> /// <returns>返回剩余的间隔</returns> public int CheckCall(string campid, string calledOper, int allowInterval) { Hashtable callHash = (Hashtable)_callProtectList[campid]; if (callHash == null) { return(0); } else { if (callHash.Contains(calledOper)) { DateTime endCall = HTBaseFunc.NullToDateTime(callHash[calledOper]).AddMilliseconds(allowInterval); DateTime dtNow = HTBaseFunc.GetTime(0); if (endCall > dtNow) { TimeSpan ts = (TimeSpan)(endCall - dtNow); return((int)ts.TotalMilliseconds); } else { return(0); } } else { return(0); } } }
/// <summary> /// 处理SqlLog记录 /// </summary> /// <param name="dbi">要记录</param> /// <param name="pos"></param> /// <param name="translist"></param> /// <returns></returns> private void DoSqlLog(DbAccessItem dbi, int pos, ArrayList translist) { try { if (_sqlLogPool != null) { DateTime dtNow = HTBaseFunc.GetTime(0); string occurgroup = ((pos == 0) ? "" : Environment.TickCount.ToString() + "_" + pos.ToString()); ArrayList newtranslist = new ArrayList(); ArrayList oParams = new ArrayList(); HTDBBaseFunc.StoredProcedureParam lSPparam; lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_poolFlag", "int", _poolFlag, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recTime", "datetime", dtNow, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recSql", "text", GenSql(dbi), false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_execResult", "int", ((dbi.Result.ErrCode == 0) ? 0 : 1), false);//执行结果:0-成功,1-错误,2-未执行 oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_errMsg", "text", ((dbi.Result.ErrCode == 0) ? "" : dbi.Result.ErrMsg), false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurGroup", "string", occurgroup, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurPos", "int", pos, false); oParams.Add(lSPparam); newtranslist.Add(new DbAccessItem("xsp_AddSqlLog", oParams, null, null, true)); if (translist != null && translist.Count > 0) { for (int i = pos + 1; i < translist.Count; i++) { DbAccessItem ldbi = (DbAccessItem)translist[i]; oParams = new ArrayList(); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_poolFlag", "int", _poolFlag, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recTime", "datetime", dtNow, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_recSql", "blob", GenSql(ldbi), false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_execResult", "int", 2, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_errMsg", "text", "", false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurGroup", "string", occurgroup, false); oParams.Add(lSPparam); lSPparam = DbAccessPool._sqlLogPool.DbComponent.GenStoredProcedureParam("p_occurPos", "int", i, false); oParams.Add(lSPparam); newtranslist.Add(new DbAccessItem("xsp_AddSqlLog", oParams, null, null, true)); } } _sqlLogPool.Push(newtranslist); } } catch (Exception ex) { _logger.Fatal(ex, "DoSqlLog error : {0}.", ex.Message); } }
private Hashtable _callProtectList = new Hashtable();//以campid为key存储Hashtable(以操作名为key存储上次操作时间) /// <summary> /// 添加调用保护 /// </summary> /// <param name="campid">匹配ID</param> /// <param name="calledOper">调用操作</param> public void AddCall(string campid, string calledOper) { Hashtable callHash = (Hashtable)_callProtectList[campid]; if (callHash == null) { callHash = new Hashtable(); _callProtectList.Add(campid, callHash); } callHash[calledOper] = HTBaseFunc.GetTime(0); }
private void CheckTimeOut(int times, long elapsed, long TimerID, object param) { if (_requestaddlist.Count > 0) { DateTime dtnow = HTBaseFunc.GetTime(0); looprpt: DateTime expiretime = _requestaddlist.Keys[0]; List <long> protectidlist = _requestaddlist[expiretime]; if (expiretime <= dtnow) { for (int i = protectidlist.Count - 1; i >= 0; i--) { long pid = protectidlist[i]; DelRequestProtect(pid); } _requestaddlist.Remove(expiretime); if (_requestaddlist.Count > 0) { goto looprpt; } } } }
/// <summary> /// 服务逻辑更新 /// </summary> public void Update() { try { #region 协议处理 ClientProtocolDealManager.Instance.TransferRecvClientData(ref m_protocolList); if (m_protocolList.Count > 0) { for (int i = 0; i < m_protocolList.Count; i++) { try { //ServerProtocolDealManager.Instance.ServerSend2ClientData(clientSessionID, protocolData); } catch (Exception ex) { SvLogger.Fatal(ex, string.Format("Param Proto Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace)); } } m_protocolList.Clear(); } #endregion #region 数据库回调处理 List <DbAccessPoolVistor> dapvistorlist = DbAccessPool.GetVistorList(); for (int i = 0; i < dapvistorlist.Count; i++) { DbAccessPoolVistor dapvistor = dapvistorlist[i]; int callbackNum = dapvistor.GetCallbackNum(); int doBackNum = 0; int vStartTick = Environment.TickCount; int doStartTick; DateTime doStartTime; while (doBackNum < callbackNum) { doStartTick = Environment.TickCount; doStartTime = HTBaseFunc.GetTime(0); DbAccessItem dbi = dapvistor.PopupCallback(); if (dbi != null) { dbi.OnFinish(dbi); } ++doBackNum; int doEndTick = Environment.TickCount; int doSpendTicks = (doEndTick >= doStartTick ? doEndTick - doStartTick : (int.MaxValue + doEndTick) + (int.MaxValue - doStartTick)); if (doSpendTicks >= 1000) { SvLogger.Warn("Update TimeOut : DbAccessPoolCallback {0}, SpendMs = {1}, SpName = {2}, StartTime = {3}.", dapvistor.Flag, doSpendTicks, ((dbi != null) ? dbi.SpName : ""), doStartTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); } int vSpendTicks = (doEndTick >= vStartTick ? doEndTick - vStartTick : doEndTick + (int.MaxValue - vStartTick)); if (vSpendTicks >= SvrCommCfg.Instance.DbAccessPoolCallbackDealInterval) { break; } } } #endregion #region 定时打印服务器监控信息 m_svrCheckEndTicket = Environment.TickCount; m_svrCheckSpendTicket = (m_svrCheckEndTicket >= m_svrCheckStartTicket ? m_svrCheckEndTicket - m_svrCheckStartTicket : (int.MaxValue + m_svrCheckEndTicket) + (int.MaxValue - m_svrCheckStartTicket)); if (m_svrCheckSpendTicket >= 60000) { m_svrCheckStartTicket = m_svrCheckEndTicket; ServerCommon.DbAccess.Instance.ShowDBAccCountInPool(); } #endregion #region 内存回收 if (m_lastMemoryCheck.AddSeconds(30) < HTBaseFunc.GetTime(0)) { if (m_lastGcTime.AddSeconds(30) < HTBaseFunc.GetTime(0) && System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 > 128 * 1024 * 1024) { GC.Collect(); m_lastGcTime = HTBaseFunc.GetTime(0); } m_lastMemoryCheck = HTBaseFunc.GetTime(0); } #endregion } catch (Exception ex) { SvLogger.Fatal(ex, string.Format("Main Logic Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace)); } }
/// <summary> /// 获取当前时间加上addInterval以后的时间 /// </summary> /// <param name="addInterval">增加间隔,毫秒级</param> /// <returns>返回当前时间加上addInterval以后的时间</returns> public static DateTime GetTime(long addInterval) { return(HTBaseFunc.GetTime(addInterval)); }
/// <summary> /// 添加请求保护(持久保护直到删除) /// </summary> /// <param name="userid">玩家ID</param> /// <param name="calledOper">调用操作</param> /// <returns>成功返回大于0的ProtectID,否则返回0</returns> public long AddRequestProtect(int userid, string calledOper, object[] args) { return(AddRequestProtect(userid, calledOper, HTBaseFunc.GetTime(0), -1, args)); }
/// <summary> /// 添加请求保护 /// </summary> /// <param name="userid">玩家ID</param> /// <param name="calledOper">调用操作</param> /// <param name="callTime">调用时间</param> /// <param name="holdIntervals">保持时长,毫秒级</param> /// <param name="args">参数列表,注意最后面要留2个空位以内部填入保护ID和调用时间</param> /// <returns>成功返回大于0的ProtectID,否则返回0</returns> private long AddRequestProtect(int userid, string calledOper, DateTime callTime, int holdIntervals, object[] args) { long protectid = 0; DateTime dtnow = HTBaseFunc.GetTime(0); DateTime expiretime; if (holdIntervals < 0) { expiretime = HTBaseFunc.NullDateTime; } else { expiretime = callTime.AddMilliseconds(holdIntervals); } Dictionary <string, object[]> operlist = null; if (_requestProtectList.ContainsKey(userid)) { operlist = (Dictionary <string, object[]>)_requestProtectList[userid]; } else { operlist = new Dictionary <string, object[]>(); _requestProtectList.Add(userid, operlist); } object[] arglist = null; if (operlist.ContainsKey(calledOper)) { arglist = operlist[calledOper]; long oldprotectid = (long)arglist[arglist.Length - 2]; DateTime oldexpiretime = Convert.ToDateTime(arglist[arglist.Length - 1]); if (oldexpiretime <= dtnow && oldexpiretime > HTBaseFunc.NullDateTime) { //删除原来过期的 operlist.Remove(calledOper); _requestidlist.Remove(oldprotectid); if (_requestaddlist.ContainsKey(oldexpiretime)) { _requestaddlist[oldexpiretime].Remove(oldprotectid); if (_requestaddlist[oldexpiretime].Count == 0) { _requestaddlist.Remove(oldexpiretime); } } //添加新的 arglist = args; protectid = GetProtectID(); if (protectid > 0) { arglist[arglist.Length - 2] = protectid; arglist[arglist.Length - 1] = expiretime; operlist.Add(calledOper, arglist); } } } else { arglist = args; protectid = GetProtectID(); if (protectid > 0) { arglist[arglist.Length - 2] = protectid; arglist[arglist.Length - 1] = expiretime; operlist.Add(calledOper, arglist); } } if (operlist.Count == 0) { _requestProtectList.Remove(userid); } if (protectid > 0) { _requestidlist[protectid] = new object[] { userid, calledOper }; if (expiretime > HTBaseFunc.NullDateTime) { List <long> protectidlist = null; if (_requestaddlist.ContainsKey(expiretime)) { protectidlist = _requestaddlist[expiretime]; } else { protectidlist = new List <long>(); _requestaddlist.Add(expiretime, protectidlist); } protectidlist.Add(protectid); } } return(protectid); }
private long GetProtectID() { long lastprotectid = _currprotectid; bool bfind = false; while (_currprotectid <= long.MaxValue) { if (_requestidlist.ContainsKey(_currprotectid) || _currprotectid == 0) { _currprotectid++; } else { bfind = true; break; } } if (!bfind) { _currprotectid = 0; while (_currprotectid < lastprotectid) { if (_requestidlist.ContainsKey(_currprotectid) || _currprotectid == 0) { _currprotectid++; } else { bfind = true; break; } } } if (!bfind) { DateTime expiretime = _requestaddlist.Keys[0]; List <long> protectidlist = _requestaddlist[expiretime]; DateTime dtnow = HTBaseFunc.GetTime(0); if (expiretime <= dtnow) { long pid = protectidlist[0]; DelRequestProtect(pid); _currprotectid = pid; bfind = true; } } long protectid = 0; if (bfind) { protectid = _currprotectid; if (_currprotectid == int.MaxValue) { _currprotectid = 0; } else { _currprotectid++; } } return(protectid); }
public string AddStatusProtect(string campid, string calledOper) { return(AddStatusProtect(campid, calledOper, HTBaseFunc.GetTime(0))); }