void RetryToGetConfig(string& strURL, long lCode, long& reslen, unsigned char **ppContent) { static int nWaitTime = RETRY_CONFIG_WAIT_TIME; static int nTotalTime = 0; if(lCode < 0) { nWaitTime = RETRY_CONFIG_WAIT_TIME; } else { nWaitTime = nWaitTime * 2; } nTotalTime += nWaitTime; if(nTotalTime > 25*60*1000) { LogFunctionalMsg("DOWNCONF","FAILED:-1"); return; } Sleep( nWaitTime ); YL_CHTTPRequest client; bool bret = client.Request(strURL.c_str(), YL_CHTTPRequest::REQUEST_GET, 10*1000, NULL, 0); if(bret) { LogFunctionalMsg("DOWNCONF","FAILED:1"); unsigned char *p = NULL; p = client.GetContent(reslen); if(p != NULL && reslen > 0) { *ppContent = new unsigned char[reslen + 1]; memcpy(*ppContent, p, reslen); (*ppContent)[reslen] = 0; } } else { long lCode = 0; client.GetReturnCode(lCode); LogFunctionalMsg("DOWNCONF","FAILED:0"); RetryToGetConfig(strURL, lCode, reslen, ppContent); } }
//去服务器获取配置文件,中间会wait 45秒 bool UpdateUserConfig(const char* szDesFilePath) { //获取服务器上的配置文件 bool bret = false; YL_CHTTPRequest client; char len[NUM_LENGTH], szID[INT_LENGTH], szVersion[VERSION_LENGTH], szInstallSRC[SRC_LENGTH], szTransfer[1024]; CLhcImg::GetUserID( szID, INT_LENGTH ); CLhcImg::GetSoftwareVersion( szVersion, VERSION_LENGTH ); CLhcImg::GetInstallSRC( szInstallSRC, SRC_LENGTH ); char szsvr[SRC_LENGTH]; memset(szsvr, 0, SRC_LENGTH); CLhcImg::GetConfigServer(szsvr, SRC_LENGTH); string strURL = string(szsvr) + "/uc/s?m="; //经过压缩的日志 char* pEncodeTransfer = NULL; memset(len, 0, NUM_LENGTH); memset( szTransfer, 0, 1024 ); _snprintf(szTransfer, 1023, "%s;%s,%s", szID, szVersion, szInstallSRC); //使用Base64加密字符串 unsigned int length = (unsigned int)strlen(szTransfer); YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "UpdateUserConfig", "Encrypt string in. szTransfer is %s, length is %u.", szTransfer, length); if(!MyMusic::Encryption(szTransfer, length, &pEncodeTransfer)) { YL_Log(UPDATE_LOG_FILE, LOG_WARNING, "UpdateUserConfig", "Encrypt string Error! string is %s, length is %u.", szTransfer, length); } YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "UpdateUserConfig", "Encrypt string Success! string is %s, length is %u.", szTransfer, length); strURL = strURL + itoa((int)strlen(szTransfer), len, 10) + ";" + pEncodeTransfer; YL_Log("UpLog.txt", LOG_NOTICE, "Update", "Wait GetConfig Event"); WaitForSingleObject( g_EventGetConfig,45*1000 ); YL_Log("UpLog.txt", LOG_NOTICE, "Update", "Wait GetConfig Event---DONE"); bret = client.Request(strURL.c_str(), YL_CHTTPRequest::REQUEST_GET, 10*1000, NULL, 0); if(pEncodeTransfer != NULL) delete[] pEncodeTransfer; long reslen = 0; unsigned char *pContent = NULL; if(!bret) { LogFunctionalMsg("DOWNCONF","FAILED:0"); LogFunctionalMsg(STR_ACT_FATAL_ERROR, "TYPE:NET_ERR|DES:UPLOAD_LOG_FAIL"); YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "UpdateUserConfig", "Get Config Request: HTTP Request is error!" ); //return false; long lCode = 0; client.GetReturnCode(lCode); RetryToGetConfig(strURL, lCode, reslen, &pContent); } else { LogFunctionalMsg("DOWNCONF","FAILED:1"); pContent = client.GetContent(reslen); } YL_Log( UPDATE_LOG_FILE, LOG_NOTICE, "UpdateUserConfig", "Get Config Request Success." ); //得不到配置文件 if(pContent == NULL || reslen <= 0) { YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "UpdateUserConfig", "Get Config Error: No Return Content!" ); return false; } YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "UpdateUserConfig", "result config content is %s", pContent); char *szContent = new char[reslen+1], *szConfig = new char[BASE64_LENGTH(reslen)+1]; memset( szContent, 0, reslen+1 ); memset( szConfig, 0, BASE64_LENGTH(reslen)+1 ); memcpy(szContent, pContent, reslen); YL_Base64Decode(szConfig, szContent, "langhua "); if( strlen( szConfig ) > 10 ) //配置文件至少得有超过10个的字符,因为svr端没有配置该版升级文件时,可能返回\n { YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "UpdateUserConfig", "Config file: content is %s", (char*)szConfig); FILE* f = fopen(szDesFilePath, "wb"); if(f != NULL) { fprintf(f, "%s", (char*)szConfig); fflush(f); fclose(f); bret = true; } } if(szConfig != NULL) delete[] szConfig; if(szContent != NULL) delete[] szContent; if(bret == false) return false; UpdateConfigFile(szDesFilePath); //更新lhpcmd.img和lhplog.img YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "update", "update 5."); return true; }