DownloadPub.h
上传用户:oadesign
上传日期:2013-12-25
资源大小:265k
文件大小:6k
- // 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_)