BOOL CHttpDownloader::DownloadByAddr()
{
BOOL bResult = FALSE;
HINTERNET hWinInet = NULL ;
HINTERNET hSession = NULL ;
HINTERNET hFile = NULL;
HANDLE hSaveFile = INVALID_HANDLE_VALUE;
PBYTE pbBuffer = NULL;
DWORD dwContentLen = 0;
//FMC_LOG( _T("Enter HttpDownload Thread.\n"));
try {
do {
if (::PathFileExists(m_szDst))
{
bResult = TRUE;
break;
}
hWinInet = ::InternetOpen(_T("HttpDownloader"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == hWinInet) {
/*FMC_LOG( _T("InternetOpen failed,Error = %d.\n"),GetLastError() );*/
break;
}
hSession = InternetConnect(hWinInet, m_szServerAddr, m_dwServerPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, 0, 0);
if (NULL == hSession) {
/*FMC_LOG( _T("InternetConnect failed,SrvAddr = %s,Port = %d,Error = %d.\n"),
m_szServerAddr,m_dwServerPort,GetLastError() );*/
break;
}
const TCHAR *FAcceptTypes = _T("*/*");
const TCHAR* lplpszAcceptType[2];
lplpszAcceptType[0] = FAcceptTypes;
lplpszAcceptType[1] = NULL;
hFile = HttpOpenRequest(hSession,
_T("GET"),
m_szSrc,
_T("HTTP/1.0"),
NULL,
lplpszAcceptType,
INTERNET_FLAG_RELOAD,
NULL);
const TCHAR *szHead = _T("Accept: */*\r\n\r\n");
if (!HttpSendRequest(hFile, szHead, lstrlen(szHead), NULL, 0)) {
/* FMC_LOG( _T("HttpSendRequest failed,szSrc = %s,Error = %d.\n"),m_szSrc,GetLastError() );*/
break;
}
DWORD dwStatusCode = 0;
DWORD dwBufLen = sizeof(dwStatusCode);
if (!HttpQueryInfo(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
(LPVOID)&dwStatusCode, &dwBufLen, 0)) {
/* FMC_LOG( _T("HttpQueryInfo StatusCode failed,szSrc = %s,Error = %d.\n"),m_szSrc,GetLastError() );*/
break;
}
if (dwStatusCode != HTTP_STATUS_OK) {
break;
}
//----------------------------------------------
//
// Get download file length
//
//---------------------------------------------
//Although we get the file length,but for the case that it is
//omitted by Manifest file makers,we just get the file length
//with patience
dwBufLen = sizeof(dwContentLen);
if (!HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
(LPVOID)&dwContentLen, &dwBufLen, 0) || dwContentLen == 0) {
/* FMC_LOG( _T("HttpQueryInfo ContentLength failed,szSrc = %s,Error = %d.\n"),m_szSrc,GetLastError() );*/
break;
}
pbBuffer = new BYTE[HTTPDOWNLOAD_BLOCKSIZE];
if (NULL == pbBuffer) {
break;
}
PostMessage(m_hNotifyWnd, WM_DOWNLOADNOTIFY, HTTPDOWNLOADER_EVENT_SIZE, dwContentLen);
//-------------------------------
//
// Download file now....
//
//-------------------------------
DWORD dwTotalReadBytes = 0;
DWORD dwReadBytes = 0;
DWORD dwWriteBytes = 0;
hSaveFile = CreateFile(m_szDst, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hSaveFile) {
/* FMC_LOG( _T("CreateFile failed,FilePath = %s,Error = %d.\n"),m_szDst,GetLastError() );*/
break;
}
DWORD dwLastNotifyTime = GetTickCount();
while (dwTotalReadBytes < dwContentLen && WaitForSingleObject(m_evStop, 0) == WAIT_TIMEOUT && !m_bCancel) {
dwReadBytes = dwContentLen - dwTotalReadBytes > HTTPDOWNLOAD_BLOCKSIZE ? HTTPDOWNLOAD_BLOCKSIZE : dwContentLen - dwTotalReadBytes; //NOLINT(whitespace/line_length)
if (!InternetReadFile(hFile, pbBuffer, dwReadBytes, &dwReadBytes)) {
/* FMC_LOG( _T("InternetReadFile failed,FilePath = %s,Error = %d.\n"),m_szDst,GetLastError() );*/
break;
}
WriteFile(hSaveFile, pbBuffer, dwReadBytes, &dwWriteBytes, NULL);
dwTotalReadBytes += dwReadBytes;
//进度通知
DWORD dwCurTime = GetTickCount();
if (dwCurTime - dwLastNotifyTime >= 1000) {
int nProgress = (int)((double)(dwTotalReadBytes) * 100 / (double)dwContentLen);
PostMessage(m_hNotifyWnd, WM_DOWNLOADNOTIFY, HTTPDOWNLOADER_EVENT_PROGRESS, dwTotalReadBytes);
}
}
if (dwTotalReadBytes != dwContentLen)
break;
bResult = TRUE;
} while (FALSE);
}
catch (...) {
bResult = FALSE;
/*FMC_LOG( _T("occur exception.\n"));*/
}
if( INVALID_HANDLE_VALUE != hSaveFile ){
CloseHandle( hSaveFile );
hSaveFile = INVALID_HANDLE_VALUE;
}
if( NULL != hFile ){
InternetCloseHandle(hFile);
hFile = NULL;
}
if(NULL!=hSession){
InternetCloseHandle(hSession);
hSession=NULL;
}
if(NULL!=hWinInet){
InternetCloseHandle(hWinInet);
hWinInet = NULL;
}
if( NULL!=pbBuffer ){
delete[] pbBuffer;
pbBuffer = NULL;
}
if( WaitForSingleObject( m_evStop,0 ) == WAIT_TIMEOUT )
{
if (bResult)
{
meetingcore::UpdateInfo updateInfo;
updateInfo.download_url = m_szUrl;
updateInfo.download_file = m_szDst;
MFDC_INSTANCE->SaveDownloadUpdateInfo(updateInfo);
}
if (bResult && m_bInstallFile)
{
std::wstring strFilePath = GetModulePath();
auto nCount = 2;
do
{
int nFindPos = strFilePath.find_last_of( L"\\" );
if ( nFindPos == -1 )
break;
strFilePath = strFilePath.substr( 0, nFindPos );
nCount--;
} while ( nCount );
TCHAR szCmd[MAX_PATH] = { 0 };
_stprintf_s(szCmd, L"/VERYSILENT /SP- /NORESTART /NOMSG /dir=\"%s\"", strFilePath.c_str());
ShellExecute(NULL , _T("open") , m_szDst , szCmd, NULL, SW_HIDE);
}
//PostMessage( m_hNotifyWnd, WM_DOWNLOADNOTIFY, bResult ? HTTPDOWNLOADER_EVENT_COMPLETE : HTTPDOWNLOADER_EVENT_FAILED, 0 ); //NOLINT(whitespace/line_length)
if (bResult)
PostMessage(m_hNotifyWnd, WM_DOWNLOADNOTIFY, HTTPDOWNLOADER_EVENT_COMPLETE, 0);
else
CheckUpdatePresenter::GetInstance()->DoDownloadFailed();
}
if(m_bCancel)
{
DeleteFile(m_szDst);
}
/*FMC_LOG( _T("Leave HttpDownload Thread,Result = %d.\n"),bResult );*/
return bResult;
}//NOLINT(readability/fn_size)