Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.0 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: download.h
  6. //
  7. // Owner: YanL
  8. //
  9. // Description:
  10. //
  11. // Internet download
  12. //
  13. //=======================================================================
  14. #ifndef _DOWNLOAD_H
  15. #include <wininet.h>
  16. // handle to internet
  17. struct auto_hinet_traits {
  18. static HINTERNET invalid() { return 0; }
  19. static void release(HINTERNET h) { InternetCloseHandle(h); }
  20. };
  21. typedef auto_resource<HANDLE, auto_hinet_traits> auto_hinet;
  22. #define MAX_THREADS_LIMIT 8
  23. class CLock : protected CRITICAL_SECTION
  24. {
  25. public:
  26. CLock()
  27. {
  28. InitializeCriticalSection(this);
  29. }
  30. LONG Increment(LONG* pl)
  31. {
  32. LONG l;
  33. EnterCriticalSection(this);
  34. l = ++ *pl;
  35. LeaveCriticalSection(this);
  36. return l;
  37. }
  38. LONG Decrement(LONG* pl)
  39. {
  40. LONG l;
  41. EnterCriticalSection(this);
  42. l = -- *pl;
  43. LeaveCriticalSection(this);
  44. return l;
  45. }
  46. ~CLock()
  47. {
  48. DeleteCriticalSection(this);
  49. }
  50. };
  51. // Notification
  52. struct IDownloadAdviceSink
  53. {
  54. virtual void JobStarted(int nJob) = 0;
  55. virtual void JobDownloadSize(int nJob, DWORD dwTotalSize) = 0;
  56. virtual void BlockDownloaded(int nJob, DWORD dwBlockSize, DWORD dwTimeSpan) = 0;
  57. virtual void JobDownloadTime(int nJob, DWORD dwTotalTimeSpan) = 0;
  58. virtual void JobDone(int nJob) = 0;
  59. virtual bool WantToAbortJob(int nJob) = 0;
  60. };
  61. class CDownload
  62. {
  63. public:
  64. CDownload(IN OPTIONAL int cnMaxThreads = 1);
  65. ~CDownload();
  66. // Connect to server and set the base URL
  67. bool Connect(IN LPCTSTR szURL);
  68. bool IsConnected() { return m_hConnection.valid(); }
  69. // Download one file to file. Calls CopyEx internally
  70. bool Copy(IN LPCTSTR szSourceFile, IN LPCTSTR szDestFile, IN OPTIONAL IDownloadAdviceSink* pSink = 0);
  71. // Download one file to memory. Calls CopyEx interanlly
  72. bool Copy(IN LPCTSTR szSourceFile, IN byte_buffer& bufDest, IN OPTIONAL IDownloadAdviceSink* pSink = 0);
  73. // Download set of files to specified destinations
  74. struct JOB
  75. {
  76. LPCTSTR pszSrvFile; // name file relative to szURL from Connect
  77. LPCTSTR pszDstFile; // path to destination file, if NULL use pDstBuf
  78. byte_buffer* pDstBuf; // pointer to memory buffer, to be used
  79. DWORD dwResult; // Win32 error code
  80. void* pUser; // for user to hookup
  81. };
  82. typedef JOB * PJOB;
  83. typedef const JOB * PCJOB;
  84. void CopyEx(IN PJOB pJobs, IN int cnJobs, IN OPTIONAL IDownloadAdviceSink* pSink = 0, IN bool fWaitComplete = true);
  85. private:
  86. static DWORD WINAPI StaticThreadProc(IN LPVOID lpParameter);
  87. void ThreadProc();
  88. void DoJob(int nJob);
  89. DWORD DoCopyToFile(IN int nJob, IN LPCTSTR szSourceFile, IN LPCTSTR szDestFile);
  90. DWORD DoCopyToBuf(IN int nJob, IN LPCTSTR szSourceFile, IN byte_buffer& bufDest);
  91. DWORD OpenInetFile(LPCTSTR szFullSourcePath, SYSTEMTIME* pstIfModifiedSince,
  92. auto_hinet& hInetFile, DWORD* pdwServerFileSize, SYSTEMTIME* pstServer);
  93. auto_hinet m_hSession; //handle to wininet library
  94. auto_hinet m_hConnection; //handle to server connection.
  95. auto_handle m_ahThreads[MAX_THREADS_LIMIT]; // threads pool
  96. int m_cnMaxThreads; // current max number of therads
  97. int m_cnThreads; // current number of running threads
  98. PJOB m_pJobsQueue; // jobs queue
  99. long m_cnJobsTotal; // total number of jobs in the queue
  100. long m_nJobTop; // top job in a queue
  101. long m_cnJobsToComplete; // m_cnJobsTotal - number of completed jobs
  102. CLock m_lock; // win95 and nt 4 don't support InterlockIncrement
  103. IDownloadAdviceSink* m_pSink; //progress report
  104. auto_handle m_hEventExit; // request to exit, set by main thread,
  105. // WaitFor... by working threads
  106. auto_handle m_hEventJob; // request to start download, set by main thread,
  107. // WaitFor... by working threads
  108. // reset when queue is empty
  109. auto_handle m_hEventDone; // set by working thread, WaitFor... by main thread
  110. TCHAR m_szRootPath[INTERNET_MAX_PATH_LENGTH]; //server that this context is connected to.
  111. };
  112. #define _DOWNLOAD_H
  113. #endif