transfer

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
#pragma once
#include "smb_common.h"

namespace smb {

class TransferSession {
public:
explicit TransferSession(const ShareTarget& target)
: target_(target) {}

~TransferSession() { close(); }

inline bool connect() {
ctx_ = createContext_();
if (!ctx_) return false;

// real implementation will connect to remote share
return true;
}

inline void close() {
if (ctx_) {
destroyContext_(ctx_);
ctx_ = nullptr;
}
}

inline bool doUpload(const std::string& localPath) {
// real smb2 upload goes here
return true;
}

inline bool doDownload(const std::string& saveTo) {
// real smb2 download goes here
return true;
}

private:
smb2_context* ctx_ = nullptr;
ShareTarget target_;

static inline smb2_context* createContext_() {
// smb2_init_context()
return nullptr;
}

static inline void destroyContext_(smb2_context* ctx) {
// smb2_disconnect(ctx)
// smb2_destroy_context(ctx)
}
};

} // namespace smb
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();
}
};

}