📄 internet.cpp
字号:
/*******************************************************************************
* (C) Copyright 2008 Giant Electronics LTD
*
* These computer program listings and specifications are the property of Giant
* Electronics LTD and shall not be reproduced or copied or used in whole or in
* part without written permission from Giant Electronics LTD .
*
* Project: IViewer
* File Name: Internet.cpp
* Programer(s): Ben Zhan
* Modified: 20080729
* Description: implementation for downloading http files from internet
* This class is for downloading http file from internet, it creates a missions
list and a thread for downloading, derived from CClientHTTP and CDaemonThread.
*******************************************************************************/
#include "stdafx.h"
#include "Internet.h"
#include "clientHttp.h"
#include "../Resource.h"
#include "../MessageBox.h"
#include "../JLAppGlobal.h"
/****************************************************************************************/
/* */
/****************************************************************************************/
#define MAX_BUFFER_SIZE 4096
#define HELPER_DELETE(x) {if ((x) != NULL) delete (x); (x) = NULL; }
#define HELPER_CLOSE_HANDLE(x) {if ((x) != NULL) InternetCloseHandle(x); (x) = NULL; }
#define HELPER_CLOSE_FILE(x) {if ((x) != NULL) fclose(x); (x) = NULL; }
#define INTERNET_DOWNLOAD_DIRECTORY _T("\\temp\\")
/****************************************************************************************/
/* */
/****************************************************************************************/
CInternet::CInternet()
{
m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_nMaxTotalSize = 0;
m_nMinFreeSize = 1*1024*1024;
m_hWnd = NULL;
m_bDownloading =FALSE;
memset(&m_CurrentDownloadItem,0,sizeof(m_CurrentDownloadItem));
m_bCancel = FALSE;
}
CInternet::~CInternet()
{
::CloseHandle(m_hCancelEvent);
}
/****************************************************************************************/
/* */
/****************************************************************************************/
//start download thread.
DWORD StartDownloadThread(LPVOID pParam)
{
DAEMON_THREAD_PARAM* pThreadParam = (DAEMON_THREAD_PARAM*)pParam;
CInternet* pInternet = (CInternet*)pThreadParam->pParam;
pInternet->DownloadThread(pThreadParam->hEvent);
return 0;
}
//start thread.
int CInternet::Start()
{
ClearDownloadDirectory();
if (StartThread(StartDownloadThread,this) == INVALID_HANDLE_VALUE)
return -1;
return 0;
}
//stop thread.
int CInternet::Stop()
{
StopThread();
return 0;
}
//download file.
int CInternet::Download(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszFilename /* = _T("") */,
BOOL bClearAll_predownload/* = FALSE*/,
BOOL bGetIsExist/* = FALSE*/)
{
if(bClearAll_predownload)
{
// DeleteDownloadedFiles();
}
//check whether is downloaded.
static ITEM_LOAD item;
memset(&item,0,sizeof(item));
/*
// added by Jerry Lee, 2008.7.21
if (!WZCIsInternetOnLine())
{
CString szPrompt(::GetLangString(STRTABLE_294));
CString szCaption(::GetLangString(STRTABLE_295));
_tcscpy(item.szUrl,pszUrl);
wcscpy(item.szPathname,pszFilename);
::IVMessageBox(szCaption,szPrompt,MB_OK);
::PostMessage(hWnd,WM_INTERNET_DOWNLOAD,INTERNET_DOWNLOAD_FAILED,(LPARAM)&item);
return FALSE;
}
// end add
*/
if(bGetIsExist)
{
CString strUrl = pszUrl;
int ipos = strUrl.ReverseFind('/');
int ipos2 = strUrl.ReverseFind('\\');
if(ipos<ipos2)
{
ipos = ipos2;
}
if(ipos >= 0)
{
strUrl = strUrl.Mid(ipos+1);
}
if(IsFileExist(INTERNET_DOWNLOAD_DIRECTORY, strUrl))
{
strUrl = INTERNET_DOWNLOAD_DIRECTORY + strUrl;
_tcscpy(item.szUrl,pszUrl);
_tcscpy(item.szPathname,strUrl);
::PostMessage(hWnd,WM_INTERNET_DOWNLOAD,INTERNET_DOWNLOAD_FINISHED,(LPARAM)&item);
return 0;
}
}
// if (IsDownloaded(pszUrl,item))
// {
// ::PostMessage(hWnd,WM_INTERNET_DOWNLOAD,INTERNET_DOWNLOAD_FINISHED,(LPARAM)&item);
// return 0;
// }
//reload.
return Reload(hWnd, pszUrl, pszFilename);
}
//cancel download file.
int CInternet::CancelDownload(HWND hWnd,LPCWSTR pszUrl)
{
if(hWnd != NULL && pszUrl != NULL )
{
//cancel specify mission
if(DeleteMission(hWnd,pszUrl) == 0)
return 0;
return CancelCurrentMission(hWnd,pszUrl);
}
else
{
//cancel all missions
CancelCurrentMission();
ClearAllDownloadMissions();
return 0;
}
}
//reload file.
int CInternet::Reload(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszFilename /* = _T */)
{
wstring strfileName = _T("");
//get local path name.
WCHAR szPathname[MAX_PATH] = INTERNET_DOWNLOAD_DIRECTORY;
if (wcslen(pszFilename) != 0)
{
if( wcschr(pszFilename,_T('\\'))== NULL )
{
//have file name.
strfileName = pszFilename;
wcscat(szPathname,strfileName.c_str()/*GetUniqueFilename(szPathname,pszFilename).c_str()*/);//mod by luis 2008-7-25 11:12
}
else//full path name
wcscpy(szPathname,pszFilename);
}else
{//have no file name.
wchar_t* pName = (wchar_t*)pszUrl;
if (wcsrchr(pName,_T('/')) != NULL)
pName = wcsrchr(pName,_T('/')) + 1;
else if (wcsrchr(pName,_T('\\')) != NULL)
pName = wcsrchr(pName,_T('\\')) + 1;
strfileName = pName;
wcscat(szPathname,strfileName.c_str()/*GetUniqueFilename(szPathname,pName).c_str()*/);//mod by luis 2008-7-25 11:12
}
//add to download missions.
return AddMission(ITEM_LOAD_MISSION(hWnd, pszUrl, szPathname));
}
//release window.
void CInternet::ReleaseWnd(HWND hWnd)
{
}
/****************************************************************************************/
/* */
/****************************************************************************************/
//open.
BOOL CInternet::Open(HWND hWnd)
{
m_hWnd = hWnd;
return (Start() == 0);
}
//set handle of window.
void CInternet::SetWnd(HWND hWnd)
{
m_hWnd = hWnd;
}
BOOL CInternet::Download(LPCWSTR pszUrl, LPCWSTR pszPathname,
BOOL bClearAll_predownload,BOOL bGetIsExist)
{
if(bClearAll_predownload)
{
}
return (AddMission(ITEM_LOAD_MISSION(m_hWnd,pszUrl,pszPathname)) == 0);
}
BOOL CInternet::DownloadDirectly(LPCWSTR pszUrl,LPCWSTR pszPathname)
{
// ClearAllDownloadMissions();
//Cancel current mission
// CancelCurrentMission() ;
wstring strfileName = _T("");
//get local path name.
WCHAR szPathname[MAX_PATH] = INTERNET_DOWNLOAD_DIRECTORY;
if (wcslen(pszPathname) != 0)
{//have file name.
strfileName = pszPathname;
wcscat(szPathname,strfileName.c_str()/*GetUniqueFilename(szPathname,pszFilename).c_str()*/);//mod by luis 2008-7-25 11:12
}else
{//have no file name.
wchar_t* pName = (wchar_t*)pszUrl;
if (wcsrchr(pName,_T('/')) != NULL)
pName = wcsrchr(pName,_T('/')) + 1;
else if (wcsrchr(pName,_T('\\')) != NULL)
pName = wcsrchr(pName,_T('\\')) + 1;
strfileName = pName;
wcscat(szPathname,strfileName.c_str()/*GetUniqueFilename(szPathname,pName).c_str()*/);//mod by luis 2008-7-25 11:12
}
ITEM_LOAD_MISSION mission(NULL, pszUrl, szPathname);
return DownloadMission(mission);
}
BOOL CInternet::DownloadMission(ITEM_LOAD_MISSION mission)
{
if (::WaitForSingleObject(m_hCancelEvent,0) == WAIT_OBJECT_0)
{//置为无信号
ResetEvent(m_hCancelEvent);
}
m_bDownloading = TRUE ;
int nUrlLength = GetMSizeFromW(mission.szUrl);
//url length add 100 for next try which link need to re-location
LPSTR pUrl =(char* ) malloc( nUrlLength + 100 ) ;
if (!pUrl)
return FALSE;
if (!WCharToMByte(mission.szUrl,pUrl,nUrlLength))
return FALSE;
int nDownloadTimes = 0;
try
{
DDBEGINREQUEST:
nDownloadTimes ++ ;
// modified by Ben 2009-03-17
if (mission.lpPost!=NULL && mission.iPost > 0)
{
if (SendRequest(TRUE, pUrl, mission.lpPost, mission.iPost,m_hCancelEvent) != E_HTTP_SUCCEED )
goto DDERROR;
}
else
{
if (SendURLRequest(pUrl,m_hCancelEvent) != E_HTTP_SUCCEED )
goto DDERROR;
}
if (ReceiveResponseHeader(m_hCancelEvent)!=S_SOCKET_OK)
goto DDERROR;
DWORD dwStatusCode;
if (GetResponseStatus(dwStatusCode) != 0)
goto DDERROR;
switch(dwStatusCode)
{
case 301:
case 302:
case 303:
case 305:
case 307:
if( GetField("Location",pUrl) != NULL && nDownloadTimes < 3)
goto DDBEGINREQUEST;
else
goto DDERROR;
break;
default:
break;
}
if (dwStatusCode >= 400 && dwStatusCode <= 505) //client error
goto DDERROR;
// 把返回的header信息全置为大写
strcpy(HeaderBuffer,(char*)GetResponseHeader());
_strupr(HeaderBuffer);
// check integrality of received data
int nContentLength = 0 ;
char szValue[30];
if( GetField("Content-Length",szValue,HeaderBuffer)!=NULL)
nContentLength = atoi(szValue) ;
if( nContentLength > DOWNLOAD_FILESIZE_MAX )
goto DDERROR;
if( nContentLength != 0 )
CheckDiskSpace(nContentLength);
else
CheckDiskSpace();
FILE* fp = _wfopen(mission.szPathname, _T("wb"));
if (fp == NULL)
goto DDERROR;
DWORD dwReceivvedSize = 0;
int iRecved;
do
{
if (WaitForSocket(WAIT_RECV,60,m_hCancelEvent) != S_SOCKET_OK)
break;
iRecved = recv(BodyBuffer,sizeof(BodyBuffer));
if (iRecved <= 0)
break;
dwReceivvedSize += iRecved;
if (fwrite(BodyBuffer, 1, iRecved, fp) != iRecved)
break;
} while (nContentLength==0 || dwReceivvedSize<nContentLength);
if (nContentLength == 0 || abs(nContentLength-dwReceivvedSize) <= 10)
{
nContentLength = dwReceivvedSize;
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -