Progress: 35%.

This commit is contained in:
unrealdragon 2026-08-24 15:23:46 +05:30
commit 159d38e210

View file

@ -1,15 +1,26 @@
#include "torrentsession.h"
#include "mainscreen.h"
#include <QStandardPaths>
#include <sstream>
#include <fstream>
#include <libtorrent/session.hpp>
#include <libtorrent/session_params.hpp>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/torrent_status.hpp>
#include <libtorrent/read_resume_data.hpp>
#include <libtorrent/write_resume_data.hpp>
#include <libtorrent/error_code.hpp>
#include <libtorrent/magnet_uri.hpp>
TorrentSession::TorrentSession(SearchItem it, std::filesystem::path download_path, QObject *parent)
{
this->item = new SearchItem(it);
this->download_path = download_path;
}
int TorrentSession::start()
@ -64,7 +75,121 @@ void TorrentSession::remove()
//libtorrent stuff starts here.
void TorrentSession::launchSession()
{
using clk = std::chrono::steady_clock;
namespace fs = std::filesystem;
lt::settings_pack pack;
pack.set_int(lt::settings_pack::alert_mask
, lt::alert_category::error
| lt::alert_category::storage
| lt::alert_category::status
| lt::alert_category::piece_progress
);
lt::session ses(pack);
clk::time_point last_save_resume = clk::now();
std::string resume_files_location = fs::path(
QStandardPaths::writableLocation(
QStandardPaths::AppLocalDataLocation
).toStdString()) / "resumes";
if(!std::filesystem::exists(resume_files_location)) {
std::filesystem::create_directories(resume_files_location);
}
item->writeData((fs::path(resume_files_location) /
fs::path(item->title.toStdString())).string() + ".searchitem");
// load resume data from disk and pass it in as we add the magnet link
std::string resume_files_name = fs::path(resume_files_location) /
fs::path(item->title.toStdString() + ".resume_file");
std::ifstream ifs(resume_files_name, std::ios_base::binary);
ifs.unsetf(std::ios_base::skipws);
std::vector<char> buf{std::istream_iterator<char>(ifs)
, std::istream_iterator<char>()};
lt::add_torrent_params magnet = lt::parse_magnet_uri(item->magnet.toStdString());
if (buf.size()) {
lt::add_torrent_params atp = lt::read_resume_data(buf);
if (atp.info_hashes == magnet.info_hashes) magnet = std::move(atp);
}
magnet.save_path = download_path.string(); // save location
ses.async_add_torrent(std::move(magnet));
lt::torrent_handle h;
// set when we're exiting
bool done = false;
bool errored = false;
bool finished = false;
std::string errored_message = "";
for (;;) {
std::vector<lt::alert*> alerts;
ses.pop_alerts(&alerts);
bool quit_loop = false;
for (lt::alert const* a : alerts) {
if (auto at = lt::alert_cast<lt::add_torrent_alert>(a)) {
h = at->handle;
}
// if we receive the finished alert or an error, we're done
if (lt::alert_cast<lt::torrent_finished_alert>(a)) {
h.save_resume_data(lt::torrent_handle::only_if_modified
| lt::torrent_handle::save_info_dict);
done = true;
finished = true;
}
if (lt::alert_cast<lt::torrent_error_alert>(a)) {
done = true;
errored = true;
errored_message = a->message();
std::cout << errored_message << std::endl;
h.save_resume_data(lt::torrent_handle::only_if_modified
| lt::torrent_handle::save_info_dict);
}
// when resume data is ready, save it
if (auto rd = lt::alert_cast<lt::save_resume_data_alert>(a)) {
std::ofstream of(resume_files_name, std::ios_base::binary);
of.unsetf(std::ios_base::skipws);
auto const b = write_resume_data_buf(rd->params);
of.write(b.data(), int(b.size()));
if (done) {
quit_loop = true;
}
}
if (lt::alert_cast<lt::save_resume_data_failed_alert>(a)) {
if (done) {
quit_loop = true;
}
}
if (auto st = lt::alert_cast<lt::state_update_alert>(a)) {
if (st->status.empty()) continue;
// we only have a single torrent, so we know which one
// the status is for
lt::torrent_status const& s = st->status[0];
std::cout << '\r' << s.state << ' '
<< (s.download_payload_rate / 1000) << " kB/s "
<< (s.total_done / 1000) << " kB ("
<< (s.progress_ppm / 10000) << "%) downloaded ("
<< s.num_peers << " peers)\x1b[K";
std::cout.flush();
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
ses.post_torrent_updates();
QCoreApplication::processEvents();
// save resume data once every 30 seconds
if (clk::now() - last_save_resume > std::chrono::seconds(30)) {
h.save_resume_data(lt::torrent_handle::only_if_modified
| lt::torrent_handle::save_info_dict);
last_save_resume = clk::now();
}
if(quit_loop) {
qDebug() << "Done.";
break;
}
}
}