void SendInstallMsgToSvr(int iType) { if (iType < 0 || iType > 5) return; //http://g.najiuwan.com/i/i.php?a=1&id=6c626dff145e283564&qd=ldt011&v=1.49 //用户情况: $a (0:安装 1:在线 2:卸载 3:新用户第一次启动 //4:覆盖安装 5:离线卸载[卸载时没有网络] std::string strID, strInstallSrc, strVersion; YL_UserId::Get(YL_UserId::USER_ID, strID); YL_UserId::Get(YL_UserId::INSTALL_SRC, strInstallSrc); YL_UserId::Get(YL_UserId::VERSION, strVersion); YL_CHTTPRequest client; std::string strUrl; YL_StringUtil::Format(strUrl, "http://g.najiuwan.com/i/i.php?a=%d&id=%s&qd=%s&v=%s", iType, strID.c_str(), strInstallSrc.c_str(), strVersion.c_str()); client.Request(strUrl, YL_CHTTPRequest::REQUEST_POST, 10*1000); }
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); } }
//上传日志文件act.log bool UploadFile(LPCTSTR lpszFile, LPCTSTR lpszURL) { //load file DWORD dwFileSize = 0; WIN32_FIND_DATA filedata; char* szContent; YL_CHTTPRequest client; //如果日志文件不存在, 创建一个新文件 HANDLE hFind = FindFirstFile(lpszFile, &filedata); if(hFind == INVALID_HANDLE_VALUE) { //创建日志文件目录 string strLogFilePath(lpszFile); string::size_type nTok = strLogFilePath.find_last_of("\\/"); string strLogDir = strLogFilePath.substr(0, nTok); YL_DirInfo::MakeDir(strLogDir); YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "upload log", "Can't Find act.log File." ); //创建日志文件 HANDLE hFile = CreateFile(lpszFile, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if( hFile == INVALID_HANDLE_VALUE ) { YL_Log(UPDATE_LOG_FILE, LOG_FATAL, "upload log", "Create act.log Error."); } CloseHandle( hFile ); szContent = new char[1]; szContent[0] = '\0'; } else { FindClose(hFind); dwFileSize = filedata.nFileSizeLow; //如果超过 300K, 只读取前 300K if( dwFileSize > UPDATE_LOG_FILE_LENGTH_LIMIT) { YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "upload log", "UploadFile Get too Large log File, Size is %d.", dwFileSize ); dwFileSize = UPDATE_LOG_FILE_LENGTH_LIMIT; } szContent = new char[dwFileSize+1]; if( dwFileSize > 0) { FILE* file; //读出上传文件的内容 file = fopen( lpszFile,"rb" ); if( file == NULL ) { return false; } BYTE* byContent = new BYTE[dwFileSize]; if( fread( byContent, dwFileSize, 1, file ) == 0 ) { delete[] byContent; fclose(file); return false; } fclose(file); memcpy(szContent, byContent, dwFileSize); delete[] byContent; } szContent[dwFileSize] = '\0'; } char szID[INT_LENGTH], szVersion[VERSION_LENGTH], szInstallSRC[SRC_LENGTH]; CLhcImg::GetUserID( szID, INT_LENGTH ); CLhcImg::GetSoftwareVersion( szVersion, VERSION_LENGTH ); CLhcImg::GetInstallSRC( szInstallSRC, SRC_LENGTH ); //发送给svr的日志,目前只是简单地只有日志内容 string strStat = string( szContent ); delete[] szContent; bool bRet = false; char* szBuffer = NULL; unsigned char *pzip = NULL; //修改日志上传协议 char len[NUM_LENGTH], szTransfer[1024]; memset(len, 0, NUM_LENGTH); memset( szTransfer, 0, 1024 ); string strURL; YL_StringUtil::Format( strURL, "%s/ulog?type=clog&ver=%s&id=%s&source=%s", lpszURL, szVersion, szID, szInstallSRC ); //压缩读出的日志 uLong ziplen = compressBound((uLong)strStat.length()); pzip = new unsigned char[ziplen]; if(pzip == NULL) { goto cleanup; } if(compress(pzip, &ziplen, (byte*)strStat.c_str(), (uLong)strStat.length()) != Z_OK) { YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "Upload", "Compress LogFile error!" ); goto cleanup; } strStat.clear(); YL_Log("Uplog.txt",LOG_DEBUG,"Updata","Wait upConfigLog Event"); WaitForSingleObject( g_EventUpConfigLog,600*1000 ); YL_Log("Uplog.txt",LOG_DEBUG,"Updata","Wait upConfigLog Event---DONE"); if(!client.Request(strURL.c_str(), YL_CHTTPRequest::REQUEST_POST, 10*60*1000, pzip, (unsigned int)ziplen)) { LogFunctionalMsg(STR_ACT_FATAL_ERROR, "TYPE:NET_ERR|DES:UPLOAD_LOG_FAIL"); YL_Log( UPDATE_LOG_FILE, LOG_WARNING, "Upload", "Upload Log:HTTP Request is error!" ); goto cleanup; } cleanup: if(pzip != NULL) delete[] pzip; YL_Log(UPDATE_LOG_FILE, LOG_NOTICE, "Upload", "Upload Log Complete."); return bRet; }
//去服务器获取配置文件,中间会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; }