-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdaemon.cpp
More file actions
294 lines (255 loc) · 8.29 KB
/
Copy pathdaemon.cpp
File metadata and controls
294 lines (255 loc) · 8.29 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <sstream>
#include <chrono>
#include <stdexcept>
#include <spdlog/spdlog.h>
#include <vector>
#include <boost/asio/error.hpp>
#include <boost/asio/post.hpp>
#include "daemon.hpp"
#include "version.hpp"
namespace
{
constexpr int SLOW_START_INITIAL = 10 * 1024 * 1024; // 10 MB/s
constexpr int SLOW_START_STEP = 10 * 1024 * 1024; // +10 MB/s per period
constexpr int SLOW_START_CAP = 100 * 1024 * 1024; // 100 MB/s -> beyond = unlimited
} // namespace
namespace ezio
{
ezio::ezio(lt::session &session, bool slow_start, int slow_start_period) :
m_session(session),
m_shutdown(false),
m_work_guard(boost::asio::make_work_guard(m_ioc)),
m_reannounce_timer(m_ioc),
m_signals(m_ioc, SIGINT, SIGTERM),
m_slow_start_timer(m_ioc),
m_slow_start(slow_start),
m_slow_start_period(slow_start_period > 0 ? slow_start_period : 10),
m_slow_start_limit(0)
{
}
void ezio::stop()
{
request_shutdown();
}
void ezio::run()
{
m_signals.async_wait([this](const boost::system::error_code &ec, int /*signum*/) {
if (ec == boost::asio::error::operation_aborted) {
return;
}
// Re-arm before posting shutdown so a second SIGINT/SIGTERM is not
// silently swallowed while the shutdown lambda is still pending.
m_signals.async_wait([this](const boost::system::error_code &ec2, int) {
if (ec2 != boost::asio::error::operation_aborted) {
request_shutdown();
}
});
request_shutdown();
});
arm_reannounce();
if (m_slow_start) {
m_slow_start_limit = SLOW_START_INITIAL;
apply_session_upload_limit(m_slow_start_limit);
schedule_slow_start_step();
}
m_ioc.run();
}
void ezio::apply_session_upload_limit(int bytes_per_second)
{
lt::settings_pack p;
p.set_int(lt::settings_pack::upload_rate_limit, bytes_per_second);
m_session.apply_settings(p);
if (bytes_per_second == 0) {
spdlog::info("slow-start: upload rate limit cleared (unlimited)");
} else {
spdlog::info("slow-start: upload limit set to {} MB/s", bytes_per_second / (1024 * 1024));
}
}
void ezio::schedule_slow_start_step()
{
m_slow_start_timer.expires_after(std::chrono::seconds(m_slow_start_period));
m_slow_start_timer.async_wait([this](const boost::system::error_code &ec) {
if (ec == boost::asio::error::operation_aborted) {
return;
}
int next = m_slow_start_limit + SLOW_START_STEP;
if (next >= SLOW_START_CAP) {
apply_session_upload_limit(0);
spdlog::info("slow-start complete: upload limit removed");
return;
}
m_slow_start_limit = next;
apply_session_upload_limit(m_slow_start_limit);
schedule_slow_start_step();
});
}
void ezio::arm_reannounce()
{
m_reannounce_timer.expires_after(std::chrono::seconds(60));
m_reannounce_timer.async_wait([this](const boost::system::error_code &ec) {
if (ec == boost::asio::error::operation_aborted) {
return;
}
force_reannounce_all();
spdlog::debug("periodic force reannounce done");
arm_reannounce();
});
}
void ezio::request_shutdown()
{
// Set eagerly (before the post) so get_shutdown() readers (log, gRPC)
// see the flag immediately, even though the io_context is still running.
// Safe cross-thread: m_shutdown is std::atomic_bool.
m_shutdown = true;
boost::asio::post(m_ioc, [this] {
m_reannounce_timer.cancel();
m_slow_start_timer.cancel();
m_signals.cancel();
for (auto &hook : m_shutdown_hooks) {
hook();
}
m_work_guard.reset();
});
}
lt::io_context &ezio::get_io_context()
{
return m_ioc;
}
void ezio::register_shutdown_hook(std::function<void()> hook)
{
m_shutdown_hooks.push_back(std::move(hook));
}
void ezio::add_torrent(std::string torrent_body, std::string save_path, bool seeding_mode = false, int max_uploads = 3, int max_connections = 5, bool sequential_download = false)
{
lt::span<const char> torrent_byte(torrent_body);
lt::add_torrent_params atp;
lt::error_code ec;
const int depth_limit = 100;
const int token_limit = 10000000;
const lt::bdecode_node node =
lt::bdecode(torrent_byte, ec, nullptr, depth_limit, token_limit);
if (ec) {
throw std::invalid_argument("failed to decode node");
}
atp.ti = std::make_shared<lt::torrent_info>(node, std::ref(ec));
atp.save_path = save_path;
atp.max_uploads = max_uploads > 0 ? max_uploads : 3;
atp.max_connections = max_connections > 0 ? max_connections : 5;
//atp.flags = libtorrent::torrent_flags::default_flags & ~libtorrent::torrent_flags::auto_managed & ~libtorrent::torrent_flags::paused;
atp.flags = {};
if (seeding_mode) {
atp.flags |= libtorrent::torrent_flags::seed_mode;
// Pre-mark every piece as already verified so libtorrent skips the
// lazy async_hash that seed_mode normally fires on first peer
// request. Our seeder's source is the same raw image the torrent
// metadata was generated from (e.g. partclone -> torrent_create),
// so re-hashing it on every cold start only catches on-disk
// corruption we don't try to detect, and costs significant wall
// time (~48s on a 60 GiB torrent in 1-on-1 tests).
atp.verified_pieces.resize(atp.ti->num_pieces(), true);
}
if (sequential_download) {
atp.flags |= libtorrent::torrent_flags::sequential_download;
}
if (ec) {
throw std::invalid_argument("failed to save path");
}
lt::torrent_handle handle = m_session.add_torrent(std::move(atp));
spdlog::info("torrent added. save_path({})", save_path);
}
std::map<std::string, torrent_status> ezio::get_torrent_status(std::vector<std::string> hashes)
{
std::map<std::string, torrent_status> result;
// TODO we ignore request hashes first, always return all
for (auto h : hashes) {
spdlog::debug("hash: {}", h);
//std::cout << h << std::endl;
// do some filter for below
}
auto now = std::chrono::high_resolution_clock::now();
std::vector<lt::torrent_handle> torrents = m_session.get_torrents();
for (lt::torrent_handle const &h : torrents) {
std::stringstream ss;
ss << h.info_hash();
const std::string &hash = ss.str();
// disable those info we don't need
lt::torrent_status t_stat = h.status();
torrent_status status;
status.hash = hash;
status.name = t_stat.name;
status.progress = t_stat.progress;
status.download_rate = t_stat.download_payload_rate;
status.upload_rate = t_stat.upload_payload_rate;
status.active_time = 1;
status.is_finished = t_stat.is_finished;
status.num_peers = t_stat.num_peers;
status.active_time = std::chrono::duration_cast<std::chrono::seconds>(t_stat.active_duration).count();
status.state = t_stat.state;
status.total_done = t_stat.total_done;
status.total = t_stat.total;
status.num_pieces = t_stat.num_pieces;
status.finished_time = std::chrono::duration_cast<std::chrono::seconds>(t_stat.finished_duration).count();
status.seeding_time = std::chrono::duration_cast<std::chrono::seconds>(t_stat.seeding_duration).count();
status.total_payload_download = t_stat.total_payload_download;
status.total_payload_upload = t_stat.total_payload_upload;
status.is_paused = (t_stat.flags & libtorrent::torrent_flags::paused) != 0;
status.save_path = t_stat.save_path;
status.last_upload = -1;
if (t_stat.last_upload.time_since_epoch().count() != 0) {
status.last_upload = std::chrono::duration_cast<std::chrono::seconds>(now - t_stat.last_upload).count();
}
status.last_download = -1;
if (t_stat.last_download.time_since_epoch().count() != 0) {
status.last_download = std::chrono::duration_cast<std::chrono::seconds>(now - t_stat.last_download).count();
}
result.emplace(hash, status);
}
return result;
}
void ezio::pause_torrent(std::string hash)
{
spdlog::info("pause {}", hash);
std::stringstream ss(hash);
libtorrent::sha1_hash info_hash;
ss >> info_hash;
auto h = m_session.find_torrent(info_hash);
if (h.is_valid()) {
h.pause();
}
}
void ezio::resume_torrent(std::string hash)
{
spdlog::info("resume {}", hash);
std::stringstream ss(hash);
libtorrent::sha1_hash info_hash;
ss >> info_hash;
auto h = m_session.find_torrent(info_hash);
if (h.is_valid()) {
h.resume();
}
}
bool ezio::get_shutdown()
{
return m_shutdown;
}
void ezio::pop_alerts(std::vector<lt::alert *> *alerts)
{
m_session.pop_alerts(alerts);
}
void ezio::set_alert_notify(std::function<void()> const &callback)
{
m_session.set_alert_notify(callback);
}
void ezio::force_reannounce_all()
{
for (auto const &h : m_session.get_torrents()) {
if (h.is_valid()) {
h.force_reannounce(0, -1, lt::torrent_handle::ignore_min_interval);
}
}
}
std::string ezio::get_version()
{
return EZIO_VERSION;
}
} // namespace ezio