ThrDownloaderF.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "FrmMainF.h"
  4. #pragma hdrstop
  5. #include "ThrDownloaderF.h"
  6. #pragma package(smart_init)
  7. //---------------------------------------------------------------------------
  8. // Important: Methods and properties of objects in VCL can only be
  9. // used in a method called using Synchronize, for example:
  10. //
  11. // Synchronize(&UpdateCaption);
  12. //
  13. // where UpdateCaption could look like:
  14. //
  15. // void __fastcall TThrDownloader::UpdateCaption()
  16. // {
  17. // Form1->Caption = "Updated in a thread";
  18. // }
  19. //---------------------------------------------------------------------------
  20. long g_CompletedJobCnt = 0;
  21. __fastcall TThrDownloader::TThrDownloader(bool CreateSuspended, bool delOldMap)
  22. : TThread(CreateSuspended)
  23. {
  24. ::CoInitialize(NULL);
  25. FDelOldMap = delOldMap;
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TThrDownloader::Execute()
  29. {
  30. String sUrl;
  31. String sFile;
  32. TIdHTTP *IdHTTP1 = new TIdHTTP(NULL);
  33. TIdSSLIOHandlerSocketOpenSSL *IdSSLIOHandler = NULL;
  34. String tmpUrl = FURL.UpperCase();
  35. bool isDaum = tmpUrl.Pos("DAUM") ? true : false;
  36. int nSuccess = 0;
  37. if (tmpUrl.Pos("HTTPS://")) {
  38. IdSSLIOHandler = new TIdSSLIOHandlerSocketOpenSSL(NULL);
  39. IdSSLIOHandler->SSLOptions->Method = sslvSSLv23;//sslvSSLv3;//sslvSSLv2;//sslvTLSv1;//sslvSSLv23;
  40. IdSSLIOHandler->SSLOptions->Mode = sslmClient;//sslmUnassigned;//sslmClient;
  41. IdHTTP1->HandleRedirects = false;
  42. IdHTTP1->IOHandler = IdSSLIOHandler;
  43. }
  44. POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_START, FXMin, 0);
  45. for (int ii = FXMin; ii <= FXMax; ii++)
  46. {
  47. for(int jj = FYMin; jj <= FYMax; jj++)
  48. {
  49. //if (isDaum) {
  50. // sFile.printf(L"%s%d\\%d.%s", FPath.c_str(), jj, ii, FFileExt.c_str());
  51. //}
  52. //else {
  53. sFile.printf(L"%s%d\\%d.%s", FPath.c_str(), ii, jj, FFileExt.c_str());
  54. //}
  55. if (FDelOldMap && FileExists(sFile))
  56. {
  57. try {
  58. DeleteFileA(sFile);
  59. } catch(Exception &e) {
  60. }
  61. }
  62. if (!FileExists(sFile))
  63. {
  64. if (isDaum) {
  65. sUrl.printf(FURL.c_str(), FZoom, jj, ii);
  66. }
  67. else {
  68. sUrl.printf(FURL.c_str(), FZoom, ii, jj);
  69. }
  70. TFileStream *fs = new TFileStream(sFile, fmCreate);
  71. try
  72. {
  73. IdHTTP1->Get(sUrl, fs);
  74. delete fs;
  75. nSuccess++;
  76. }
  77. catch(Exception &e)
  78. {
  79. if (fs != NULL) delete fs;
  80. }
  81. }
  82. else nSuccess++;
  83. InterlockedIncrement(&g_CompletedJobCnt);
  84. Sleep(10);
  85. // POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_JOB, FX, nSuccess);
  86. #if 0
  87. int i=0;
  88. for(i=0; i<100; i++)
  89. {
  90. _asm
  91. {
  92. NOP;
  93. };
  94. }
  95. #endif
  96. }
  97. }
  98. if (IdSSLIOHandler != NULL) delete IdSSLIOHandler;
  99. delete IdHTTP1;
  100. POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_COMPLETE, FXMin, nSuccess);
  101. }
  102. //---------------------------------------------------------------------------