Team Fortress 2 Source Code as on 22/4/2020
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.

109 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #ifndef CL_DOWNLOADER_H
  5. #define CL_DOWNLOADER_H
  6. #ifdef _WIN32
  7. #pragma once
  8. #endif
  9. //----------------------------------------------------------------------------------------
  10. #include "platform.h"
  11. #include "engine/http.h"
  12. #include "interface.h"
  13. #include "tier0/threadtools.h"
  14. //----------------------------------------------------------------------------------------
  15. struct RequestContext_t;
  16. class CHttpDownloader;
  17. class KeyValues;
  18. //----------------------------------------------------------------------------------------
  19. class IDownloadHandler
  20. {
  21. public:
  22. virtual void OnConnecting( CHttpDownloader *pDownloader ) = 0;
  23. virtual void OnFetch( CHttpDownloader *pDownloader ) = 0;
  24. // Called when the download is done successfully, errors out, or is aborted.
  25. // NOTE: pDownloader should NOT be deleted from within OnDownloadComplete().
  26. // pData contains the downloaded data for processing.
  27. virtual void OnDownloadComplete( CHttpDownloader *pDownloader, const unsigned char *pData ) = 0;
  28. };
  29. //----------------------------------------------------------------------------------------
  30. //
  31. // Generic downloader class - downloads a single file on its own thread, and maintains
  32. // state for that data.
  33. //
  34. // TODO: Derive from CBaseThinker and remove explicit calls to Think() - will make this
  35. // class less bug-prone (easy to forget to call Think()).
  36. //
  37. class CHttpDownloader
  38. {
  39. public:
  40. // Pass in a callback
  41. CHttpDownloader( IDownloadHandler *pHandler = NULL );
  42. ~CHttpDownloader();
  43. //
  44. // Download the file at the given URL (HTTP/HTTPS support only)
  45. // pGamePath - Game path where we should put the file - can be NULL if we don't
  46. // want to save to the file to disk
  47. // pUserData - Passed back to IDownloadHandler, if one has been set - can be NULL
  48. // pBytesDownloaded - If non-NULL, # of bytes downloaded written.
  49. // Returns true on success.
  50. //
  51. bool BeginDownload( const char *pURL, const char *pGamePath = NULL,
  52. void *pUserData = NULL, uint32 *pBytesDownloaded = NULL );
  53. //
  54. // Abort the download (if there is one), wait for the download to shutdown and
  55. // do cleanup.
  56. //
  57. void AbortDownloadAndCleanup();
  58. inline bool IsDone() const { return m_bDone; } // Download done?
  59. inline bool CanDelete() const { return m_pThreadState == NULL; } // Can free?
  60. inline HTTPStatus_t GetStatus() const { return m_nHttpStatus; }
  61. inline HTTPError_t GetError() const { return m_nHttpError; }
  62. inline void *GetUserData() const { return m_pUserData; }
  63. inline uint32 GetBytesDownloaded() const { return m_uBytesDownloaded; }
  64. inline uint32 GetSize() const { return m_uSize; } // File size in bytes - NOTE: Not valid until the download is complete, aborted, or errored out
  65. inline const char *GetURL() const { return m_szURL; }
  66. void Think();
  67. KeyValues *GetOgsRow( int nErrorCounter ) const;
  68. static const char *GetHttpErrorToken( HTTPError_t nError );
  69. private:
  70. bool CleanupThreadIfDone();
  71. void InvokeHandler();
  72. RequestContext_t *m_pThreadState;
  73. float m_flNextThinkTime;
  74. bool m_bDone;
  75. HTTPError_t m_nHttpError;
  76. HTTPStatus_t m_nHttpStatus;
  77. uint32 m_uBytesDownloaded;
  78. uint32 *m_pBytesDownloaded; // Passed into BeginDownload()
  79. uint32 m_uSize;
  80. IDownloadHandler *m_pHandler;
  81. void *m_pUserData;
  82. char m_szURL[512];
  83. // Use this to make sure that AbortDownloadAndCleanup isn't executed simultaneously
  84. // by two threads. This was causing use-after-free crashes during shutdown.
  85. CThreadMutex m_lock;
  86. };
  87. //----------------------------------------------------------------------------------------
  88. #endif // CL_DOWNLOADER_H