reload torrents asynchronously when starting client

This commit is contained in:
arvidn 2017-05-04 22:46:30 -04:00 committed by Arvid Norberg
parent 8c57d11778
commit d2d96d6649
1 changed files with 34 additions and 37 deletions

View File

@ -1277,53 +1277,48 @@ MAGNETURL is a magnet link
else add_torrent(ses, i.to_string()); else add_torrent(ses, i.to_string());
} }
// load resume files std::thread resume_data_loader([&ses]
std::string const resume_dir = path_append(save_path, ".resume");
std::vector<std::string> ents = list_dir(resume_dir
, [](lt::string_view p) { return p.size() > 7 && p.substr(p.size() - 7) == ".resume"; }, ec);
if (ec)
{ {
// load resume files
error_code ec;
std::string const resume_dir = path_append(save_path, ".resume");
std::vector<std::string> ents = list_dir(resume_dir
, [](lt::string_view p) { return p.size() > 7 && p.substr(p.size() - 7) == ".resume"; }, ec);
if (ec)
{
std::fprintf(stderr, "failed to list resume directory \"%s\": (%s : %d) %s\n" std::fprintf(stderr, "failed to list resume directory \"%s\": (%s : %d) %s\n"
, resume_dir.c_str(), ec.category().name(), ec.value(), ec.message().c_str()); , resume_dir.c_str(), ec.category().name(), ec.value(), ec.message().c_str());
} }
else else
{
int idx = 0;
for (auto const& e : ents)
{ {
std::string const file = path_append(resume_dir, e); for (auto const& e : ents)
std::vector<char> resume_data;
load_file(file, resume_data, ec);
if (ec)
{ {
std::printf(" failed to load resume file \"%s\": %s\n" std::string const file = path_append(resume_dir, e);
, file.c_str(), ec.message().c_str());
continue;
}
add_torrent_params p = lt::read_resume_data(resume_data, ec);
if (ec)
{
std::printf(" failed to parse resume data \"%s\": %s\n"
, file.c_str(), ec.message().c_str());
continue;
}
// we're loading this torrent from resume data. There's no need to std::vector<char> resume_data;
// re-save the resume data immediately. load_file(file, resume_data, ec);
p.flags &= ~add_torrent_params::flag_need_save_resume; if (ec)
{
std::printf(" failed to load resume file \"%s\": %s\n"
, file.c_str(), ec.message().c_str());
continue;
}
add_torrent_params p = lt::read_resume_data(resume_data, ec);
if (ec)
{
std::printf(" failed to parse resume data \"%s\": %s\n"
, file.c_str(), ec.message().c_str());
continue;
}
ses.async_add_torrent(std::move(p)); // we're loading this torrent from resume data. There's no need to
// re-save the resume data immediately.
p.flags &= ~add_torrent_params::flag_need_save_resume;
++idx; ses.async_add_torrent(std::move(p));
if ((idx % 32) == 0)
{
// regularly, pop and handle alerts, to avoid the alert queue from
// filling up with add_torrent_alerts
pop_alerts(view, ses_view, ses, events);
} }
} }
} });
// main loop // main loop
std::vector<peer_info> peers; std::vector<peer_info> peers;
@ -1938,6 +1933,8 @@ COLUMN OPTIONS
} }
} }
resume_data_loader.join();
ses.pause(); ses.pause();
std::printf("saving resume data\n"); std::printf("saving resume data\n");