file_download_task.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/25/17
  6. // Description:
  7. #pragma once
  8. #include <stdint.h>
  9. #include <map>
  10. #include <string>
  11. #include "Poco/Runnable.h"
  12. #include "cos_config.h"
  13. #include "trsf/transfer_handler.h"
  14. #include "util/base_op_util.h"
  15. #include "util/semaphore.h"
  16. #include "util/task.h"
  17. namespace qcloud_cos {
  18. class FileDownTask : public Poco::Runnable {
  19. public:
  20. FileDownTask(const std::string& host,
  21. const std::string& path,
  22. const bool is_https,
  23. const BaseOpUtil& op_util,
  24. const std::map<std::string, std::string>& headers,
  25. const std::map<std::string, std::string>& params,
  26. uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
  27. const SharedTransferHandler& handler = nullptr,
  28. uint64_t offset = 0, unsigned char* pbuf = NULL,
  29. const size_t data_len = 0,
  30. bool verify_cert = true,
  31. const std::string& ca_lication = "",
  32. SSLCtxCallback ssl_ctx_cb = nullptr,
  33. void *user_data = nullptr,
  34. Semaphore* semaphore = nullptr);
  35. ~FileDownTask() {}
  36. void run();
  37. void DownTask();
  38. void SetDownParams(unsigned char* pdatabuf, size_t datalen, uint64_t offset);
  39. void SetVerifyCert(bool verify_cert);
  40. void SetCaLocation(const std::string& ca_location);
  41. void SetSslCtxCb(SSLCtxCallback cb, void *data);
  42. // 设置信号量,用于任务完成时自动释放资源
  43. void SetSemaphore(Semaphore* semaphore) { m_semaphore = semaphore; }
  44. // 设置当前任务在下载序列中的顺序号
  45. void SetSequence(uint64_t sequence) { m_task_info.sequence = sequence;}
  46. // 重置任务状态为IDLE,供主线程在处理完TASK_COMPLETED后调用以复用任务槽
  47. void ResetTaskStatus() { m_task_info.status = TASK_IDLE; }
  48. void SetTaskRunning() { m_task_info.status = TASK_RUNNING; }
  49. TaskStatus GetTaskStatus() const { return m_task_info.status; }
  50. std::string GetTaskResp() const { return m_resp; }
  51. size_t GetDownLoadLen() const { return m_real_down_len; }
  52. bool IsTaskSuccess() const { return m_is_task_success; }
  53. uint64_t GetSequence() const { return m_task_info.sequence; }
  54. int GetHttpStatus() const { return m_http_status; }
  55. std::map<std::string, std::string> GetRespHeaders() const { return m_resp_headers; }
  56. std::string GetErrMsg() const { return m_err_msg; }
  57. private:
  58. std::string m_host;
  59. std::string m_path;
  60. bool m_is_https;
  61. BaseOpUtil m_op_util;
  62. std::map<std::string, std::string> m_headers;
  63. std::map<std::string, std::string> m_params;
  64. uint64_t m_conn_timeout_in_ms;
  65. uint64_t m_recv_timeout_in_ms;
  66. SharedTransferHandler m_handler;
  67. uint64_t m_offset;
  68. unsigned char* m_data_buf_ptr;
  69. size_t m_data_len;
  70. std::string m_resp;
  71. bool m_is_task_success;
  72. size_t m_real_down_len;
  73. int m_http_status;
  74. std::map<std::string, std::string> m_resp_headers;
  75. std::string m_err_msg;
  76. bool m_verify_cert;
  77. std::string m_ca_location;
  78. SSLCtxCallback m_ssl_ctx_cb;
  79. void *m_user_data;
  80. // 信号量指针,用于任务完成时自动通知
  81. Semaphore* m_semaphore;
  82. TaskInfo m_task_info;
  83. SharedConfig m_config;
  84. void SendRequestOnce(std::string domain);
  85. };
  86. } // namespace qcloud_cos