2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2007-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
|
|
|
#include <thread>
|
|
|
|
|
2022-02-09 18:02:59 +00:00
|
|
|
struct tr_session;
|
|
|
|
struct tr_torrent;
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
class tr_verify_worker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using callback_func = std::function<void(tr_torrent*, bool aborted)>;
|
|
|
|
|
|
|
|
~tr_verify_worker();
|
|
|
|
|
|
|
|
void addCallback(callback_func callback)
|
|
|
|
{
|
|
|
|
callbacks_.emplace_back(std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(tr_torrent* tor);
|
|
|
|
|
|
|
|
void remove(tr_torrent* tor);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
tr_torrent* torrent = nullptr;
|
|
|
|
uint64_t current_size = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] int compare(Node const& that) const;
|
|
|
|
|
|
|
|
[[nodiscard]] bool operator<(Node const& that) const
|
|
|
|
{
|
|
|
|
return compare(that) < 0;
|
|
|
|
}
|
|
|
|
};
|
2009-05-29 19:17:12 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
void callCallback(tr_torrent* tor, bool aborted)
|
|
|
|
{
|
|
|
|
for (auto& callback : callbacks_)
|
|
|
|
{
|
|
|
|
callback(tor, aborted);
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:02:59 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
void verifyThreadFunc();
|
|
|
|
[[nodiscard]] static bool verifyTorrent(tr_torrent* tor, bool const* stop_flag);
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
std::list<callback_func> callbacks_;
|
|
|
|
std::mutex verify_mutex_;
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
std::set<Node> todo_;
|
|
|
|
std::optional<Node> current_node_;
|
2008-10-28 15:13:07 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
std::optional<std::thread::id> verify_thread_id_;
|
|
|
|
bool stop_current_ = false;
|
|
|
|
};
|