file_upload_task.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #include <map>
  3. #include <string>
  4. #include "Poco/Runnable.h"
  5. #include "request/object_req.h"
  6. #include "trsf/transfer_handler.h"
  7. #include "util/base_op_util.h"
  8. #include "util/semaphore.h"
  9. #include "util/task.h"
  10. namespace qcloud_cos {
  11. class FileUploadTask : public Poco::Runnable {
  12. public:
  13. FileUploadTask(const std::string& host,
  14. const std::string& path,
  15. const bool is_https,
  16. const BaseOpUtil& op_util,
  17. uint64_t conn_timeout_in_ms,
  18. uint64_t recv_timeout_in_ms, unsigned char* pbuf = NULL,
  19. const size_t data_len = 0,
  20. bool verify_cert = true,
  21. const std::string& ca_location = "",
  22. SSLCtxCallback ssl_ctx_cb = nullptr,
  23. void *user_data = nullptr);
  24. FileUploadTask(const std::string& host,
  25. const std::string& path,
  26. const bool is_https,
  27. const BaseOpUtil& op_util,
  28. const std::map<std::string, std::string>& headers,
  29. const std::map<std::string, std::string>& params,
  30. uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
  31. const SharedTransferHandler& handler,
  32. bool verify_cert = true,
  33. const std::string& ca_location = "",
  34. SSLCtxCallback ssl_ctx_cb = nullptr,
  35. void *user_data = nullptr);
  36. FileUploadTask(const std::string& host,
  37. const std::string& path,
  38. const bool is_https,
  39. const BaseOpUtil& op_util,
  40. const std::map<std::string, std::string>& headers,
  41. const std::map<std::string, std::string>& params,
  42. uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
  43. unsigned char* pbuf = NULL, const size_t data_len = 0,
  44. bool verify_cert = true,
  45. const std::string& ca_location = "",
  46. SSLCtxCallback ssl_ctx_cb = nullptr,
  47. void *user_data = nullptr);
  48. ~FileUploadTask() {}
  49. void run();
  50. void UploadTask();
  51. void SetUploadBuf(unsigned char* pdatabuf, size_t data_len);
  52. std::string GetTaskResp() const;
  53. bool IsTaskSuccess() const;
  54. void SetTaskSuccess() { m_is_task_success = true; }
  55. int GetHttpStatus() const;
  56. std::map<std::string, std::string> GetRespHeaders() const;
  57. void AddParams(const std::map<std::string, std::string>& params);
  58. void SetParams(const std::map<std::string, std::string>& params);
  59. void AddHeaders(const std::map<std::string, std::string>& headers);
  60. void SetHeaders(const std::map<std::string, std::string>& headers);
  61. std::string GetErrMsg() const { return m_err_msg; }
  62. void SetResume(const bool is_resume) { m_is_resume = is_resume; }
  63. bool IsResume() const { return m_is_resume; }
  64. void SetResumeEtag(const std::string& etag) { m_resume_etag = etag; }
  65. std::string GetResumeEtag() const { return m_resume_etag; }
  66. void SetPartNumber(uint64_t part_number);
  67. uint64_t GetPartNumber() const { return m_part_number; }
  68. void SetVerifyCert(bool verify_cert);
  69. void SetCaLocation(const std::string& ca_location);
  70. void SetSslCtxCb(SSLCtxCallback cb, void *data);
  71. void SetCheckCrc64(bool check_crc64) {
  72. mb_check_crc64 = check_crc64;
  73. }
  74. // 设置信号量,用于任务完成时自动释放资源槽位
  75. void SetSemaphore(Semaphore* semaphore) { m_semaphore = semaphore; }
  76. // 设置当前任务在上传序列中的顺序号
  77. void SetSequence(uint64_t sequence) { m_task_info.sequence = sequence; }
  78. uint64_t GetCrc64Value() const {
  79. return m_crc64_value;
  80. }
  81. // 获取任务序号
  82. uint64_t GetSequence() const { return m_task_info.sequence; }
  83. // 重置任务状态为IDLE,供主线程在处理完TASK_COMPLETED后调用以复用任务槽
  84. void ResetTaskStatus() { m_task_info.status = TASK_IDLE; }
  85. void SetTaskRunning() { m_task_info.status = TASK_RUNNING; }
  86. TaskStatus GetTaskStatus() const { return m_task_info.status; }
  87. private:
  88. std::string m_host;
  89. std::string m_path;
  90. bool m_is_https;
  91. std::map<std::string, std::string> m_headers;
  92. std::map<std::string, std::string> m_params;
  93. uint64_t m_conn_timeout_in_ms;
  94. uint64_t m_recv_timeout_in_ms;
  95. unsigned char* m_data_buf_ptr;
  96. size_t m_data_len;
  97. std::string m_resp;
  98. bool m_is_task_success;
  99. int m_http_status;
  100. std::map<std::string, std::string> m_resp_headers;
  101. std::string m_err_msg;
  102. bool m_is_resume;
  103. std::string m_resume_etag;
  104. uint64_t m_part_number;
  105. SharedTransferHandler m_handler;
  106. bool m_verify_cert;
  107. std::string m_ca_location;
  108. SSLCtxCallback m_ssl_ctx_cb;
  109. void *m_user_data;
  110. bool mb_check_crc64;
  111. uint64_t m_crc64_value;
  112. Semaphore* m_semaphore;
  113. TaskInfo m_task_info;
  114. BaseOpUtil m_op_util;
  115. void SendRequestOnce(std::string domain, std::string md5_str);
  116. };
  117. } // namespace qcloud_cos