1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #pragma once #include "smb_session_transfer.h"
namespace smb {
class TransferClientManager { public: static TransferClientManager& instance() { static TransferClientManager inst; return inst; }
// 初始化线程池 TransferClientManager(size_t threads = std::thread::hardware_concurrency()) { startPool_(threads); }
// 上传任务 inline void upload(const ShareTarget& target, const std::string& localPath) { enqueue_([=]() { TransferSession sess(target); if (!sess.connect()) return; sess.doUpload(localPath); }); }
// 下载任务 inline void download(const ShareTarget& target, const std::string& saveTo) { enqueue_([=]() { TransferSession sess(target); if (!sess.connect()) return; sess.doDownload(saveTo); }); }
private: std::vector<std::thread> workers_; std::queue<std::function<void()>> tasks_; std::mutex mu_; std::condition_variable cv_; bool stop_ = false;
inline void startPool_(size_t n) { for (size_t i = 0; i < n; ++i) { workers_.emplace_back([this]() { while (true) { std::function<void()> task;
{ std::unique_lock<std::mutex> lk(mu_); cv_.wait(lk, [&](){ return stop_ || !tasks_.empty(); }); if (stop_ && tasks_.empty()) return; task = std::move(tasks_.front()); tasks_.pop(); }
task(); } }); } }
inline void enqueue_(std::function<void()> task) { { std::lock_guard<std::mutex> lk(mu_); tasks_.push(std::move(task)); } cv_.notify_one(); }
~TransferClientManager() { { std::lock_guard<std::mutex> lk(mu_); stop_ = true; } cv_.notify_all(); for (auto& w : workers_) w.join(); } };
}
|