⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 internet.h

📁 wince 下socket实现的HTTP类
💻 H
字号:
/*******************************************************************************
* (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.h 
* Programer(s):	Ben Zhan
* Modified:     20080729 
* Description:	interface 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.
*******************************************************************************/

#ifndef _INTERNET_H_
#define _INTERNET_H_

#include <afxmt.h>

#include<wininet.h>
#pragma comment(lib, "Wininet.lib")

#include "DaemonThread.h"
#include "ClientHTTP.h"

/****************************************************************************************/
/*                                                                                      */
/****************************************************************************************/
#define DOWNLOAD_FILESIZE_MAX 5*1024*1024

#define WM_INTERNET_DOWNLOAD WM_USER+110 //message when downloaded.
                                         //WPARAM: enum INTERNET_DOWNLOAD_STATUS.
                                         //LPARAM: struct ITEM_LOAD.

//internet download status.
enum INTERNET_DOWNLOAD_STATUS{
	INTERNET_CONNECT_FAILED,
	INTERNET_DOWNLOAD_FINISHED,
	INTERNET_DOWNLOAD_CANCEL,
	INTERNET_CREATEFILE_FAILED,
	INTERNET_SERVER_INVALID,
	INTERNET_FILELENGTH_INVALID,	
	INTERNET_UNKNOWN_ERROR,
	INTERNET_DOWNLOAD_FAILED,
	INTERNET_DOWNLOAD_TIMEOUT
};

//item downloaded.
typedef struct _item_load{
	WCHAR szUrl[MAX_PATH*10]; //URL.
	WCHAR szPathname[MAX_PATH*10]; //local file name.
	_item_load()
	{
		memset(this,0,sizeof(_item_load));
	}
}ITEM_LOAD,*PITEM_LOAD;

/****************************************************************************************/
/*                                                                                      */
/****************************************************************************************/
//item download mission.
typedef struct _item_load_mission : public _item_load
{
	HWND hWnd; //handle of window.

	LPSTR lpPost;  // added by Ben 2009-03-17
	int   iPost;

	_item_load_mission()
	{
		memset(this,0,sizeof(_item_load_mission));		
		iPost  = 0;
		lpPost = NULL;
	}

	// modified by Ben 2009-03-17
	_item_load_mission(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszPathname,LPCSTR lpszPost = NULL,int Post = 0)
	{
		this->hWnd = hWnd;
		wcscpy(this->szUrl,pszUrl);
		wcscpy(this->szPathname,pszPathname);
		iPost  = 0;
		lpPost = NULL;

		if (Post > 0 && lpszPost != NULL)
		{
			iPost = Post;
			lpPost = new char[iPost+1];
			memset(lpPost,0,iPost+1);
			memcpy(lpPost,lpszPost,iPost);
		}
		
	}
}ITEM_LOAD_MISSION,*PITEM_LOAD_MISSION;

//item downloaded ext.
typedef struct _item_load_ex : public _item_load
{
	int nFilesize; //file size.
	
	_item_load_ex()
	{
		memset(this,0,sizeof(_item_load));
	}

	_item_load_ex(LPCWSTR pszUrl, LPCWSTR pszPathname, int nFilesize)
	{
		wcscpy(this->szUrl,pszUrl);
		wcscpy(this->szPathname,pszPathname);
		this->nFilesize = nFilesize;
	}
}ITEM_LOAD_EX,*PITEM_LOAD_EX;

/****************************************************************************************/
/*                                                                                      */
/****************************************************************************************/
//internet

class CInternet :public CDaemonThread,public CClientHTTP
{
	//start download thread.
	friend DWORD StartDownloadThread(LPVOID pParam);

public:
	CInternet();
	~CInternet();

public:
	//start thread.
	//return values: 0 - success,-1 - fail.
	int Start();

	//stop thread.
	//return values: 0 - success,-1 - fail.
	int Stop();

//	del by luis 2008-7-4 15:05
// 	//download file.			
// 	//parameters: hWnd(in) - handle of window.
// 	//            pszUrl(in) - URL.
// 	//            pszFilename(in) - local file name.
// 	//return values: 0 - success,-1 - fail.
// 	int Download(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszFilename = _T(""));
	//download file
	//parameters: hWnd(in) - handle of window
	//			  pszUrl(in) -URL
	//			  pszFilename(in) -local file name
	//			  bClearAll_predownload(in) - clear all pre_download files
	int Download(HWND hWnd,LPCWSTR pszUrl,
		LPCWSTR pszFilename = _T(""),BOOL bClearAll_predownload = FALSE,
		BOOL bGetIsExist = FALSE);


	//cancel download file.
	//parameters: hWnd(in) - handle of window.
	//            pszUrl(in) - URL.
	//return values: 0 - success,-1 - fail.
	int CancelDownload(HWND hWnd = NULL,LPCWSTR pszUrl = NULL);

	//Clear all download mission.
	//parameters: none
	//
	void ClearAllDownloadMissions();
	
	//reload file.
	//parameters: hWnd(in) - handle of window.
	//            pszUrl(in) - URL.
	//            pszFilename(in) - local file name.
	//return values: 0 - success,-1 - fail.
	int Reload(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszFilename = _T(""));

	//release window.
	//parameters: hWnd(in) - handle of window.
	void ReleaseWnd(HWND hWnd);

	//open.
	//parameters: hWnd(in) - handle of window.
	//return values: open success or not.
	BOOL Open(HWND hWnd);

	//set handle of window.
	//parameters: hWnd(in) - handle of window.
	void SetWnd(HWND hWnd);

//  del by luis 2008-7-4 15:06
// 	//download file.
// 	//parameters: pszUrl(in) - URL.
// 	//            pszPathname(in) - local path name.
// 	//return values: add download mission success or not.
// 	BOOL Download(LPCWSTR pszUrl, LPCWSTR pszPathname);

	//download file.
	//parameters: pszUrl(in) - URL.
	//            pszPathname(in) - local path name.
	//			  bClearAll_predownload(in) - clear all pre_download files
	//return values: add download mission success or not.
	BOOL Download(LPCWSTR pszUrl,LPCWSTR pszPathname, 
		BOOL bClearAll_predownload = FALSE, BOOL bGetIsExist = FALSE);

	//download file.
	//parameters: 
	//			  pszUrl(in) - URL.
	//            pszPathname(in) - local path name.
	//			  bClearAllMissions(in) - clear all missions in the thread
	//			  bClearAll_predownload(in) - clear all pre_download files
	//return values: add download mission success or not.	

	// modified by Ben 2009-03-17 for support Post method
	BOOL DownloadUrgence(HWND hWnd,LPCWSTR pszUrl,LPCWSTR pszPathname, LPCSTR lpszPost = NULL,int iPostLength = 0);


	//close.
	//return values: close success or not.
	BOOL Close();

	//download file.
	//parameters: 
	//			  pszUrl(in) - URL.
	//            pszPathname(in) - local path name.
	//return values: add download mission success or not.
	BOOL DownloadDirectly(LPCWSTR pszUrl,LPCWSTR pszPathname);

	BOOL IsDownloadOK(){return m_bIsDownloadOK;}

	BOOL IsMissionEmpty();

private:
	BOOL DownloadMission(ITEM_LOAD_MISSION mission);

	//check whether download successful.
	BOOL	m_bIsDownloadOK ;

	//download thread.
	//parameters: hEvent(in) - exit event.
	void DownloadThread(HANDLE hEvent);

	//add mission to download missions.
	//parameters: mission(out) - download mission.
	//return values: 0 - success,-1 - fail.
	int AddMission(const ITEM_LOAD_MISSION &mission);

	//push mission to download missions.
	//parameters: mission(out) - download mission.
	//return values: 0 - success,-1 - fail.
	int PushMission(const ITEM_LOAD_MISSION &mission);
	//delete mission from download missions.
	//parameters: hWnd(in) - handle of window.
	//            pszUrl(in) - URL.
	//return values: 0 - success,-1 - fail.
	int DeleteMission(HWND hWnd,LPCWSTR pszUrl);

	//get download mission.
	//parameters: mission(out) - download mission.
	//return values: 0 - success,-1 - fail.
	int GetMission(ITEM_LOAD_MISSION &mission);

public://mod by luis 2008-12-10 方便外界调用
	//cancel current mission.
	//parameters: hWnd(in) - handle of window.
	//            pszUrl(in) - URL.
	//return values: 0 - success,-1 - fail.
	int CancelCurrentMission(HWND hWnd = NULL ,LPCWSTR pszUrl=NULL);

private:
	BOOL	IsDownloading() ;
	//clear download directory.
	void ClearDownloadDirectory();
	//add by Ben Zhan
	//Check disk free space 
	void CheckDiskSpace(int nFreeSize=1000000);

	//get unique file name.
	//parameters: pszDirectory(in) - directory.
	//            pszFilename(in) - file name.
	//return values: unique file name.
	wstring GetUniqueFilename(LPCWSTR pszDirectory,LPCWSTR pszFilename);

	//whether file is exist.
	//parameters: pszDirectory(in) - directory.
	//            pszFilename(in) - file name.
	BOOL IsFileExist(LPCWSTR pszDirectory,LPCWSTR pszFilename);

	//get file size.
	//parameters: pszFilename(in) - file name.
	//return values: file size.
	int GetFileSize(LPCWSTR pszFilename);




private:
	vector<ITEM_LOAD_MISSION> m_missions; //download missions.
	CSemaphore m_lockMissions; //locker of download missions.


private:
	int m_nMaxTotalSize; //delete some file if downloaded total size exceed this value.
	int m_nMinFreeSize; //delete some file if free disk space is under this value.

private:
	HWND m_hWnd; //handle of window.

	HANDLE		m_hCancelEvent; //for cancel current mission
	BOOL		m_bDownloading;
	ITEM_LOAD	m_CurrentDownloadItem;

	char		HeaderBuffer[2048],BodyBuffer[4096];

	BOOL m_bCancel;
};

#endif
//end of file.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -