NetManager.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/FileStream.h"
  4. #include "BeefySysLib/util/Deque.h"
  5. #include "BeefySysLib/util/ThreadPool.h"
  6. #include "BeefySysLib/util/Dictionary.h"
  7. NS_BF_BEGIN
  8. #ifdef BF_PLATFORM_WINDOWS
  9. #define BF_CURL
  10. #endif
  11. class DebugManager;
  12. class NetManager;
  13. class NetResult;
  14. #ifdef BF_CURL
  15. typedef void CURL;
  16. typedef void CURLM;
  17. #endif
  18. class NetRequest : public ThreadPool::Job
  19. {
  20. public:
  21. NetManager* mNetManager;
  22. String mURL;
  23. String mOutPath;
  24. String mOutTempPath;
  25. SysFileStream mOutFile;
  26. #ifdef BF_CURL
  27. CURL* mCURL;
  28. CURLM* mCURLMulti;
  29. #else
  30. #endif
  31. volatile bool mCancelling;
  32. bool mFailed;
  33. String mError;
  34. uint32 mLastUpdateTick;
  35. bool mShowTracking;
  36. NetResult* mResult;
  37. NetResult* mCancelOnSuccess;
  38. NetRequest()
  39. {
  40. mLastUpdateTick = 0;
  41. #ifdef BF_CURL
  42. mCURL = NULL;
  43. mCURLMulti = NULL;
  44. #else
  45. #endif
  46. mCancelling = false;
  47. mFailed = false;
  48. mShowTracking = false;
  49. mResult = NULL;
  50. mCancelOnSuccess = NULL;
  51. }
  52. ~NetRequest();
  53. void DoTransfer();
  54. void Cleanup();
  55. void Fail(const StringImpl& error);
  56. bool Cancel() override;
  57. void Perform() override;
  58. void ShowTracking();
  59. };
  60. class NetResult
  61. {
  62. public:
  63. String mURL;
  64. String mOutPath;
  65. bool mFailed;
  66. String mError;
  67. NetRequest* mCurRequest;
  68. bool mRemoved;
  69. SyncEvent* mDoneEvent;
  70. NetResult()
  71. {
  72. mFailed = false;
  73. mCurRequest = NULL;
  74. mRemoved = false;
  75. mDoneEvent = NULL;
  76. }
  77. ~NetResult()
  78. {
  79. delete mDoneEvent;
  80. }
  81. };
  82. class NetManager
  83. {
  84. public:
  85. Dictionary<String, NetResult*> mCachedResults;
  86. ThreadPool mThreadPool;
  87. DebugManager* mDebugManager;
  88. Deque<NetRequest*> mRequests;
  89. Array<NetResult*> mOldResults;
  90. SyncEvent mRequestDoneEvent;
  91. NetResult* mWaitingResult;
  92. NetRequest* mWaitingRequest;
  93. public:
  94. NetManager();
  95. ~NetManager();
  96. NetRequest* CreateGetRequest(const StringImpl& url, const StringImpl& destPath, bool useCache);
  97. NetResult* QueueGet(const StringImpl& url, const StringImpl& destPath, bool useCache);
  98. bool Get(const StringImpl& url, const StringImpl& destPath);
  99. void CancelAll();
  100. void Clear();
  101. void CancelCurrent();
  102. void Cancel(NetResult* netResult);
  103. void SetCancelOnSuccess(NetResult* dependentResult, NetResult* cancelOnSucess);
  104. };
  105. NS_BF_END