transfer_handler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef __TRSF_HANDLER_H__
  2. #define __TRSF_HANDLER_H__
  3. #include "op/cos_result.h"
  4. #include "response/object_resp.h"
  5. #include <condition_variable>
  6. #include <exception>
  7. #include <functional>
  8. #include <istream>
  9. #include <map>
  10. #include <memory>
  11. #include <mutex>
  12. #include <ostream>
  13. namespace qcloud_cos {
  14. class ObjectReq;
  15. class TransferHandler;
  16. class AsyncContext;
  17. typedef std::shared_ptr<TransferHandler> SharedTransferHandler;
  18. typedef std::shared_ptr<AsyncContext> SharedAsyncContext;
  19. /// @brief 进度回调函数
  20. using TransferProgressCallback = std::function<void(
  21. uint64_t transferred_size, uint64_t total_size, void* user_data)>;
  22. /// @brief 完成回调函数
  23. using DoneCallback =
  24. std::function<void(const SharedAsyncContext& context, void* user_data)>;
  25. class PartState {
  26. public:
  27. PartState();
  28. PartState(int part_num, std::string& etag, size_t size,
  29. bool last_part = false);
  30. void SetPartNum(int number) { m_part_num = number; }
  31. int GetPartNum() const { return m_part_num; }
  32. void SetEtag(const std::string& etag) { m_etag = etag; }
  33. std::string GetEtag() const { return m_etag; }
  34. void SetSize(size_t size) { m_size_inbytes = size; }
  35. size_t GetSize() const { return m_size_inbytes; }
  36. void SetLastPart(bool lastpart) { m_lastpart = lastpart; }
  37. bool IsLastPart() { return m_lastpart; }
  38. private:
  39. int m_part_num;
  40. // current use the md5
  41. std::string m_etag;
  42. size_t m_size_inbytes;
  43. // TODO for now just care about the whole progress
  44. /* size_t m_current_progress_inbytes; */
  45. /* size_t m_range_begin; */
  46. bool m_lastpart;
  47. };
  48. typedef std::shared_ptr<PartState> PartPointer;
  49. // Key is partnumber
  50. typedef std::map<int, PartPointer> PartStateMap;
  51. enum class TransferStatus {
  52. NOT_START,
  53. // Operation is now running
  54. IN_PROGRESS,
  55. // Operation was canceled.
  56. CANCELED,
  57. // Operation failed
  58. FAILED,
  59. // Operation was successful
  60. COMPLETED,
  61. // Operation either failed or was canceled and a user deleted the multi-part
  62. // upload .
  63. ABORTED
  64. };
  65. class TransferHandler : public std::enable_shared_from_this<TransferHandler> {
  66. public:
  67. TransferHandler();
  68. ~TransferHandler() {}
  69. void SetBucketName(const std::string& bucket_name) {
  70. m_bucket_name = bucket_name;
  71. }
  72. std::string GetBucketName() const { return m_bucket_name; }
  73. void SetObjectName(const std::string& object_name) {
  74. m_object_name = object_name;
  75. }
  76. std::string GetObjectName() const { return m_object_name; }
  77. void SetLocalFilePath(const std::string& local_file_path) {
  78. m_local_file_path = local_file_path;
  79. }
  80. std::string GetLocalFilePath() const { return m_local_file_path; }
  81. void SetTotalSize(uint64_t total_size) { m_total_size = total_size; }
  82. uint64_t GetTotalSize() const { return m_total_size; }
  83. // Notice there can not backwards
  84. void UpdateProgress(uint64_t update_prog);
  85. // Get the current upload size(B).
  86. uint64_t GetProgress() const;
  87. void UpdateStatus(const TransferStatus& status);
  88. void UpdateStatus(const TransferStatus& status, const CosResult& result);
  89. void UpdateStatus(const TransferStatus& status, const CosResult& result,
  90. const std::map<std::string, std::string> headers,
  91. const std::string& body = "");
  92. TransferStatus GetStatus() const;
  93. std::string GetStatusString() const;
  94. void SetUploadID(const std::string& uploadid) { m_uploadid = uploadid; }
  95. // Get the init or resumed uploadid.
  96. std::string GetUploadID() const { return m_uploadid; }
  97. // Cancel the process of interface the uploadid can reuse.
  98. void Cancel();
  99. bool ShouldContinue() const;
  100. bool IsFinishStatus(TransferStatus status) const;
  101. bool IsAllowTransition(TransferStatus org, TransferStatus dst) const;
  102. // Block until finish.
  103. void WaitUntilFinish();
  104. /// @brief 设置进度回调函数
  105. void SetTransferProgressCallback(const TransferProgressCallback& callback) {
  106. m_progress_cb = callback;
  107. }
  108. /// @brief 设置状态回调函数
  109. void SetDoneCallback(const DoneCallback& callback) { m_done_cb = callback; }
  110. /// @brief 设置回调私有数据
  111. void SetUserData(void* user_data) { m_user_data = user_data; }
  112. /// @brief 设置请求信息
  113. void SetRequest(const void* req);
  114. ///////////////////////////////////////////////////////////////////////////
  115. // 用户调用的函数
  116. /// @brief 获取操作结果
  117. CosResult GetResult() const { return m_result; }
  118. /// @brief 获取响应
  119. AsyncResp GetAsyncResp() const;
  120. private:
  121. CosResult m_result;
  122. std::map<std::string, std::string> m_resp_headers;
  123. std::string m_resp_body;
  124. private:
  125. std::string m_bucket_name;
  126. std::string m_object_name;
  127. std::string m_local_file_path;
  128. uint64_t m_total_size;
  129. // The m_current_progress best to use the atomic. but can not support c11 for
  130. // now, so use the mutex.
  131. uint64_t m_current_progress;
  132. TransferStatus m_status;
  133. std::string m_uploadid;
  134. // Is cancel
  135. bool m_cancel;
  136. PartStateMap m_part_map;
  137. // Mutex lock for the progress
  138. mutable std::mutex m_lock_prog;
  139. // Mutex lock for the status
  140. mutable std::mutex m_lock_stat;
  141. // Condition
  142. mutable std::condition_variable m_cond;
  143. // callback function
  144. TransferProgressCallback m_progress_cb;
  145. DoneCallback m_done_cb;
  146. void* m_user_data;
  147. // Mutex lock for the part map
  148. // mutable boost::mutex m_lock_parts;
  149. };
  150. class HandleStreamCopier {
  151. public:
  152. static std::streamsize handleCopyStream(const SharedTransferHandler& handler,
  153. std::istream& istr,
  154. std::ostream& ostr,
  155. std::size_t bufferSize = 8192);
  156. };
  157. class UserCancelException : public std::exception {
  158. public:
  159. UserCancelException() {}
  160. ~UserCancelException() throw() {}
  161. };
  162. } // namespace qcloud_cos
  163. #endif