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="sMsg"></param> /// <param name="nLen"></param> private void ProcessServerMsg(string sMsg, int nLen) { string sDefMsg;//消息头 string sData; TDefaultMessage DefMsg; if (nLen == Grobal2.DEFBLOCKSIZE) { sDefMsg = sMsg; sData = ""; } else { sDefMsg = sMsg.Substring(1 - 1, Grobal2.DEFBLOCKSIZE); sData = sMsg.Substring(Grobal2.DEFBLOCKSIZE + 1 - 1, sMsg.Length - Grobal2.DEFBLOCKSIZE - 6); } DefMsg = EncryptUnit.DecodeMessage(sDefMsg); DBShare.g_nWorkStatus = DefMsg.Ident; switch (DefMsg.Ident) { case Common.DB_LOADHUMANRCD: LoadHumanRcd(sData); break; case Common.DB_SAVEHUMANRCD: SaveHumanRcd(DefMsg.Recog, sData); break; case Common.DB_LOADHERORCD: // DB_SAVEHUMANRCDEX: begin // SaveHumanRcdEx(sData, DefMsg.Recog, Socket); // end; // 读取英雄数据 LoadHeroRcd(sData); break; case Common.DB_NEWHERORCD: // 新建英雄 NewHeroRcd(sData); break; case Common.DB_DELHERORCD: // 删除英雄 DeleteHeroRcd(sData); break; case Common.DB_SAVEHERORCD: // 保存英雄数据 SaveHeroRcd(DefMsg.Recog, sData); break; case Common.DB_LOADRANKING: // 排行榜 LoadRanking(sData, DefMsg); break; case Common.DB_SAVEMAGICLIST: SaveMagicList(DefMsg.Recog, sData); break; case Common.DB_SAVESTDITEMLIST: SaveStdItemList(DefMsg.Recog, sData); break; case Common.DB_CLOSESOCKET: break; case Common.DB_SENDKEEPALIVE: break; default: m_DefMsg = EncryptUnit.MakeDefaultMsg(Common.DBR_FAIL, 0, 0, 0, 0, 0); SendSocket(EncryptUnit.EncodeMessage(m_DefMsg)); break; } }