1
0

ProgressiveHTTPReader.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. /* implementation ideas, notes and todos
  9. Memory mapped file to use as temporary storage
  10. mark downloaded pages with bitfield vector
  11. use content length header
  12. use content-disposition for filename, if available (instead of temp filename)
  13. */
  14. #include "ProgressiveHTTPReader.h"
  15. #include <windows.h>
  16. int ProgressiveHTTPReader::isMine(const wchar_t *filename, int mode)
  17. {
  18. return 0; // only want to use the progressive downloader/reader on demand.
  19. }
  20. int ProgressiveHTTPReader::open(const wchar_t *filename, int mode)
  21. {
  22. pagePosition.QuadPart=0;
  23. SYSTEM_INFO info;
  24. GetSystemInfo(&info);
  25. pageSize = info.dwPageSize;
  26. char tempPath[MAX_PATH];
  27. GetTempPathA(MAX_PATH, tempPath);
  28. GetTempFileNameA(tempPath, "phr", 0, tempPath);
  29. hFile=CreateFile((LPCWSTR)tempPath, GENERIC_WRITE|GENERIC_READ, 0, 0, CREATE_ALWAYS, 0, 0);
  30. LARGE_INTEGER contentLength;
  31. contentLength.QuadPart = 100*1024*1024; // TODO: use content length header for filesize
  32. hMap=CreateFileMapping(hFile, 0, PAGE_READWRITE, contentLength.HighPart, contentLength.LowPart, 0);
  33. // TODO: spawn a thread and start downloading data..
  34. // thread should get a duplicate file handle
  35. return 1;
  36. }
  37. void ProgressiveHTTPReader::IncrementPosition(uint64_t inc)
  38. {
  39. pagePosition.QuadPart += offset;
  40. pagePosition.QuadPart += inc;
  41. offset = (uint32_t)(pagePosition.QuadPart & (uint64_t)pageSize);
  42. pagePosition.QuadPart-=offset;
  43. }
  44. size_t ProgressiveHTTPReader::GetPageNumber() const
  45. {
  46. return (size_t)(pagePosition.QuadPart / (uint64_t)pageSize);
  47. }
  48. size_t ProgressiveHTTPReader::read(__int8 *buffer, size_t length)
  49. {
  50. /* TODO:
  51. is this area of the file downloaded yet? If so, just return it as is
  52. otherwise, what should we do? return what we can? or sit and wait until we get the data?
  53. */
  54. while (length)
  55. {
  56. // if page is available
  57. {
  58. // TODO: calculate maximum length we can map
  59. MapViewOfFile(hMap, FILE_MAP_READ, pagePosition.HighPart, pagePosition.LowPart, pageSize);
  60. // map page
  61. // copy to buffer
  62. // increment buffer
  63. // decrement length
  64. // increment position
  65. }
  66. //else
  67. {
  68. // queue up read request to background thread
  69. // wait for signal
  70. continue;
  71. }
  72. }
  73. return 0;
  74. }