transfer_handler.h 6.2 KB

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