public unsafe static bool GetDBSockMsg(byte[] da, int nQueryID, ref int nIdent, ref int nRecog, ref string sStr, uint dwTimeOut, bool boLoadRcd, string sName) { bool result; bool boLoadDBOK; string s24 = string.Empty; string s28 = string.Empty; string s2C = string.Empty; string sCheckFlag = string.Empty; string sDefMsg; string s38; int nLen; int nCheckCode; TDefaultMessage DefMsg; boLoadDBOK = false; result = false; byte nCode = 0; const int DEFBLOCKSIZE = 32; while (true) { s24 = ""; try { fixed(byte *pb = da) { s24 = HUtil32.SBytePtrToString((sbyte *)pb, 0, da.Length); } s24 = System.Text.Encoding.Default.GetString(da); } finally { } if (s24 != "") { s28 = ""; s24 = HUtil32.ArrestStringEx(s24, "#", "!", ref s28); if (s28 != "") { s28 = HUtil32.GetValidStr3(s28, ref s2C, new string[] { "/" }); nLen = s28.Length; if ((nLen >= sizeof(TDefaultMessage)) && (HUtil32.Str_ToInt(s2C, 0) == nQueryID)) { nCheckCode = HUtil32.MakeLong(HUtil32.Str_ToInt(s2C, 0) ^ 170, nLen); byte[] data = new byte[sizeof(int)]; fixed(byte *by = data) { *(int *)by = nCheckCode; } sCheckFlag = EncryptUnit.EncodeBuffer(data, data.Length); if (HUtil32.CompareBackLStr(s28, sCheckFlag, sCheckFlag.Length)) { if (nLen == DEFBLOCKSIZE) { sDefMsg = s28; s38 = ""; } else { sDefMsg = s28.Substring(0, DEFBLOCKSIZE); s38 = s28.Substring(DEFBLOCKSIZE + 1 - 1, s28.Length - DEFBLOCKSIZE - 6); } DefMsg = EncryptUnit.DecodeMessage(sDefMsg); nIdent = DefMsg.Ident; nRecog = DefMsg.Recog; sStr = s38; boLoadDBOK = true; result = true; break; } } else { if ((nLen < Marshal.SizeOf(typeof(TDefaultMessage)))) { nCode = 2; } if ((HUtil32.Str_ToInt(s2C, 0) != nQueryID)) { nCode = 4; } break; } } else { nCode = 3; break; } } else { System.Threading.Thread.Sleep(1); } } if (!boLoadDBOK) { if (boLoadRcd) { //M2Share.MainOutMessage(sLoadDBTimeOut + sName + " Code:" + (nCode).ToString()); } else { //M2Share.MainOutMessage(sSaveDBTimeOut + sName + " Code:" + (nCode).ToString()); } } else { } return(result); }
/// <summary> /// 获取DBSERBER发送过来的消息 /// </summary> /// <param name="nQueryID"></param> /// <param name="nIdent"></param> /// <param name="nRecog"></param> /// <param name="sStr"></param> /// <param name="dwTimeOut"></param> /// <param name="boLoadRcd"></param> /// <param name="sName"></param> /// <returns></returns> public unsafe static bool GetDBSockMsg(int nQueryID, ref int nIdent, ref int nRecog, ref string sStr, uint dwTimeOut, bool boLoadRcd, string sName) { bool result; bool boLoadDBOK; uint dwTimeOutTick; string s24 = string.Empty; string s28 = string.Empty; string s2C = string.Empty; string sCheckFlag = string.Empty; string sDefMsg; string s38; int nLen; int nCheckCode; TDefaultMessage DefMsg; byte nCode; const string sLoadDBTimeOut = "[RunDB] 读取人物数据超时..."; const string sSaveDBTimeOut = "[RunDB] 保存人物数据超时..."; boLoadDBOK = false; result = false; dwTimeOutTick = HUtil32.GetTickCount(); nCode = 0; while (true) { if ((HUtil32.GetTickCount() - dwTimeOutTick) > dwTimeOut)//dwTimeOut--等待消息的时间 { nCode = 1; break; } s24 = ""; HUtil32.EnterCriticalSection(M2Share.UserDBSection); try { if (M2Share.g_Config.sDBSocketRecvText.IndexOf("!") > 0) { s24 = M2Share.g_Config.sDBSocketRecvText; M2Share.g_Config.sDBSocketRecvText = ""; } } finally { HUtil32.LeaveCriticalSection(M2Share.UserDBSection); } if (s24 != "") { s28 = ""; s24 = HUtil32.ArrestStringEx(s24, "#", "!", ref s28); if (s28 != "") { s28 = HUtil32.GetValidStr3(s28, ref s2C, new string[] { "/" }); nLen = s28.Length; if ((nLen >= sizeof(TDefaultMessage)) && (HUtil32.Str_ToInt(s2C, 0) == nQueryID)) { nCheckCode = HUtil32.MakeLong(HUtil32.Str_ToInt(s2C, 0) ^ 170, nLen); byte[] data = new byte[sizeof(int)]; fixed(byte *by = data) { *(int *)by = nCheckCode; } sCheckFlag = EncryptUnit.EncodeBuffer(data, data.Length); if (HUtil32.CompareBackLStr(s28, sCheckFlag, sCheckFlag.Length)) { if (nLen == Grobal2.DEFBLOCKSIZE) { sDefMsg = s28; s38 = ""; } else { sDefMsg = s28.Substring(0, Grobal2.DEFBLOCKSIZE); s38 = s28.Substring(Grobal2.DEFBLOCKSIZE + 1 - 1, s28.Length - Grobal2.DEFBLOCKSIZE - 6); } DefMsg = EncryptUnit.DecodeMessage(sDefMsg); nIdent = DefMsg.Ident; nRecog = DefMsg.Recog; sStr = s38; boLoadDBOK = true; result = true; break; } } else { if ((nLen < Marshal.SizeOf(typeof(TDefaultMessage)))) { nCode = 2; } if ((HUtil32.Str_ToInt(s2C, 0) != nQueryID)) { nCode = 4; } M2Share.g_Config.nLoadDBErrorCount++; break; } } else { nCode = 3; M2Share.g_Config.nLoadDBErrorCount++; break; } } else { System.Threading.Thread.Sleep(1); } } if (!boLoadDBOK) { M2Share.g_nSaveRcdErrorCount++; if (boLoadRcd) { M2Share.MainOutMessage(sLoadDBTimeOut + sName + " Code:" + nCode); } else { M2Share.MainOutMessage(sSaveDBTimeOut + sName + " Code:" + nCode); } } else { M2Share.g_nSaveRcdErrorCount = 0; } M2Share.g_Config.boDBSocketWorking = false; return(result); }
/// <summary> /// 处理服务器包 /// </summary> /// <param name="Buffer"></param> /// <param name="BufLen"></param> public void ProcessServerPacket(string Buffer, int BufLen) { bool bo25 = false; int nC = 0; //string sReceiveText = string.Empty; string sC; string s1C; string s20 = string.Empty; string s24 = string.Empty; int n14; int n18; ushort wE; ushort w10; TDefaultMessage DefMsg; try { m_sQueryID = ""; m_dwKeepAliveTick = GameFramework.HUtil32.GetTickCount(); string sReceiveText = Buffer; m_sReceiveText = m_sReceiveText + sReceiveText; #if DEBUG DBShare.MainOutMessage("ServerClient.m_sReceiveText:" + m_sReceiveText); #endif while (true) { if (m_sReceiveText.IndexOf('!') <= 0) { break; } //获取一条数据放入s20 m_sReceiveText = GameFramework.HUtil32.ArrestStringEx(m_sReceiveText, "#", "!", ref s20); if (s20 != "") //如果s20不为空 { if (s20.Length > 2) //并且大于2 { s20 = GameFramework.HUtil32.GetValidStr3(s20, ref s24, new string[] { "/" }); n14 = s20.Length; if ((n14 >= Grobal2.DEFBLOCKSIZE) && (s24 != "")) { m_sQueryID = s24; wE = (ushort)(GameFramework.HUtil32.Str_ToInt(s24, 0) ^ 170); w10 = (ushort)n14; n18 = GameFramework.HUtil32.MakeLong(wE, w10); sC = EncryptUnit.EncodeBuffer(BitConverter.GetBytes(n18), 4); if (HUtil32.CompareBackLStr(s20, sC)) { ProcessServerMsg(s20, n14); bo25 = true; m_sReceiveText = ""; break; } else { m_DefMsg = EncryptUnit.MakeDefaultMsg(Common.DBR_FAIL, 0, 0, 0, 0); SendSocket(EncryptUnit.EncodeMessage(m_DefMsg)); m_sReceiveText = ""; break; } } } else { m_sReceiveText = ""; break; } } else { // 防止DBS溢出攻击 if (nC >= 1) { m_sReceiveText = ""; break; } nC++; } } m_dwCheckServerTimeMin = GameFramework.HUtil32.GetTickCount() - m_dwCheckRecviceTick; if (m_dwCheckServerTimeMin > m_dwCheckServerTimeMax) { m_dwCheckServerTimeMax = m_dwCheckServerTimeMin; } m_dwCheckRecviceTick = GameFramework.HUtil32.GetTickCount(); if (m_Module != null) { m_Module.Buffer = string.Format("{0}/{1}", m_dwCheckServerTimeMin, m_dwCheckServerTimeMax); } } catch { DBShare.MainOutMessage("[Exception] TDBSERVER::ProcessServerPacket"); } }