?? downloadpub.h
字號:
// DownloadPub.h: interface for the CDownloadPub class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DownloadPub_H__FF670BDB_1339_412D_AB6A_9B64FB29D2FE__INCLUDED_)
#define AFX_DownloadPub_H__FF670BDB_1339_412D_AB6A_9B64FB29D2FE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "SocketClient.h"
#include <Afxmt.h>
#include <afxtempl.h>
//缺省的重試次數
const UINT DEFAULT_RETRY_MAX = 10;
#define SLEEP_RETURN_Down(x)\
{\
if ( ::WaitForSingleObject ( m_hEvtEndModule, x ) == WAIT_OBJECT_0 )\
return DownloadEnd(FALSE);\
}
enum
{
NOTIFY_TYPE_GOT_REMOTE_FILENAME, // 取得遠程站點文件名, 當被下載的文件被重定向時才發送該通知,lpNotifyData 為 LPCTSTR 類型的文件名字符串指針
NOTIFY_TYPE_GOT_REMOTE_FILESIZE, // 取得遠程站點文件大小,lpNotifyData 為 int 類型的文件大小
NOTIFY_TYPE_START_DOWNLOAD, // 開始下載,lpNotifyData 無用
NOTIFY_TYPE_END_DOWNLOAD, // 結束下載,lpNotifyData 為 ENUM_DOWNLOAD_RESULT 類型的下載結果
NOTIFY_TYPE_WILL_DOWNLOAD_SIZE, // 本次需要下載的大小,lpNotifyData 為 int 類型的需要下載的大小
NOTIFY_TYPE_THREAD_DOWNLOADED_SIZE, // 某線程已下載的數據大小,lpNotifyData 為 int 類型的已下載的大小
};
typedef struct _DownloadNotifyPara
{
int nIndex;
UINT nNotityType;
LPVOID lpNotifyData;
LPVOID pDownloadMTR;
} t_DownloadNotifyPara;
typedef CArray<t_DownloadNotifyPara,t_DownloadNotifyPara&> t_Ary_DownloadNotifyPara;
// 消息通知回調函數
typedef void (*FUNC_DownloadNotify) ( t_DownloadNotifyPara *pDownloadNotifyPara, WPARAM wParam );
typedef void (*FUNC_SaveDownloadInfo) ( int nIndex, int nDownloadedSize, int nSimpleSaveSize, WPARAM wParam );
// 缺省端口號
#define DEFAULT_HTTP_PORT 80
#define DEFAULT_HTTPS_PORT 443
#define DEFAULT_FTP_PORT 21
#define DEFAULT_SOCKS_PORT 6815
#define RETRY_TIMES 999 // 重試多少次
typedef enum _DownloadResult
{
ENUM_DOWNLOAD_RESULT_SUCCESS,
ENUM_DOWNLOAD_RESULT_FAILED,
ENUM_DOWNLOAD_RESULT_CANCEL,
} ENUM_DOWNLOAD_RESULT;
class CDownloadPub
{
public:
CString GetDownloadObjectFileName ( CString *pcsExtensionName=NULL );
void Clear_Thread_Handle();
void ResetVar();
int GetUndownloadBytes ();
BOOL ThreadProc_Download();
BOOL SetSaveFileName ( LPCTSTR lpszSaveFileName );
int Get_WillDownloadSize();
void Set_WillDownloadSize ( int nWillDownloadSize );
int Get_DownloadedSize();
void Set_DownloadedSize ( int nDownloadedSize );
int Get_TempSaveBytes();
void Set_TempSaveBytes ( int nTempSaveBytes );
int m_nIndex;
CString GetRemoteFileName ();
virtual BOOL GetRemoteSiteInfo();
BOOL SetDownloadUrl ( LPCTSTR lpszDownloadUrl );
virtual BOOL Connect ();
CDownloadPub();
virtual ~CDownloadPub();
void SetAuthorization ( LPCTSTR lpszUsername, LPCTSTR lpszPassword );
void SetReferer(LPCTSTR lpszReferer);
void SetUserAgent(LPCTSTR lpszUserAgent);
void Set_SaveDownloadInfo_Callback ( FUNC_SaveDownloadInfo Proc_SaveDownloadInfo, WPARAM wParam );
virtual BOOL Download (
int nWillDownloadStartPos,
int nWillDownloadSize,
int nDownloadedSize
);
BOOL Is_SupportResume () { return m_bSupportResume; }
CString Get_ProtocolType () { return m_csProtocolType; }
time_t Get_TimeLastModified() { return m_TimeLastModified.GetTime(); }
int Get_FileTotalSize() { return m_nFileTotalSize; }
CString Get_UserName () { return m_csUsername; }
CString Get_GetPassword () { return m_csPassword; }
CString Get_DownloadUrl () { return m_csDownloadUrl; }
BOOL Is_DownloadSuccess() { return m_bDownloadSuccess; }
HANDLE Get_Thread_Handle() { return m_hThread; }
int Get_WillDownloadStartPos() { return m_nWillDownloadStartPos; }
CString Get_ServerName() { return m_csServer; }
void StopDownload ();
LPVOID m_pDownloadMTR;
protected:
virtual BOOL DownloadOnce();
CFile m_file;
int SaveDataToFile ( char *data, int size );
virtual BOOL RecvDataAndSaveToFile(CSocketClient &SocketClient,char *szTailData=NULL, int nTailSize=0);
BOOL DownloadEnd ( BOOL bRes );
// 模塊結束事件
HANDLE m_hEvtEndModule;
// 連接服務器的 Socket
CSocketClient m_SocketClient;
// 待下載URL
CString m_csDownloadUrl;
CString m_csSaveFileName;
// 保存下載信息的回調函數
FUNC_SaveDownloadInfo m_Proc_SaveDownloadInfo;
WPARAM m_wSaveDownloadInfo_Param;
// 是否支持斷點續傳
BOOL m_bSupportResume;
HANDLE m_hThread;
// 文件以及下載大小
int m_nFileTotalSize; // 文件總的大小,-1表示未知文件大小
int m_nWillDownloadStartPos; // 要下載文件的開始位置
int m_nWillDownloadSize; // 本次需要下載的大小,-1表示不知道文件大小,所以下載到服務器關閉連接為止
CCriticalSection m_CSFor_WillDownloadSize; // 訪問 m_nWillDownloadSize 變量的互斥鎖
int m_nTempSaveBytes; // 存放在臨時緩沖中的字節數
CCriticalSection m_CSFor_TempSaveBytes; // 訪問 m_nTempSaveBytes 變量的互斥鎖
int m_nDownloadedSize; // 已下載的字節數,指完全寫到文件中的字節數,不包含臨時緩沖里的數據
CCriticalSection m_CSFor_DownloadedSize; // 訪問 m_nDownloadedSize 變量的互斥鎖
// 文件日期(遠程文件的信息)
CTime m_TimeLastModified;
// Referer
CString m_csReferer;
// UserAgent
CString m_csUserAgent;
// 是否進行驗證 : Request-Header: Authorization
CString m_csUsername;
CString m_csPassword;
// 下載過程中所用的變量
CString m_csProtocolType; // 所使用的傳輸協議:http、ftp等
CString m_csServer;
CString m_csObject;
CString m_csFileName;
USHORT m_nPort;
private:
BOOL OpenFileForSave();
BOOL m_bDownloadSuccess;
};
int Base64Encode(LPCTSTR lpszEncoding, CString &strEncoded);
int Base64Decode(LPCTSTR lpszDecoding, CString &strDecoded);
BOOL ParseURL(LPCTSTR lpszURL,CString& strServer,CString& strObject,USHORT& nPort, CString &csProtocolType);
void Set_DownloadNotify_Callback ( FUNC_DownloadNotify Proc_DownloadNotify, WPARAM wParam );
#endif // !defined(AFX_DownloadPub_H__FF670BDB_1339_412D_AB6A_9B64FB29D2FE__INCLUDED_)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -