2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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-11-14 05:11:48 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
2023-10-19 13:39:34 +00:00
|
|
|
#include <memory>
|
2022-09-06 04:43:59 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <string>
|
2022-09-06 04:43:59 +00:00
|
|
|
#include <thread>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <utility> // std::move
|
|
|
|
|
|
|
|
#include "libtransmission/transmission.h"
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
#include "libtransmission/torrent-metainfo.h"
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2022-02-09 18:02:59 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
class tr_verify_worker
|
|
|
|
{
|
|
|
|
public:
|
2023-10-19 13:39:34 +00:00
|
|
|
class Mediator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Mediator() = default;
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
[[nodiscard]] virtual tr_torrent_metainfo const& metainfo() const = 0;
|
|
|
|
[[nodiscard]] virtual std::optional<std::string> find_file(tr_file_index_t file_index) const = 0;
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
virtual void on_verify_queued() = 0;
|
|
|
|
virtual void on_verify_started() = 0;
|
|
|
|
virtual void on_piece_checked(tr_piece_index_t piece, bool has_piece) = 0;
|
|
|
|
virtual void on_verify_done(bool aborted) = 0;
|
|
|
|
};
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
~tr_verify_worker();
|
|
|
|
|
|
|
|
void add(std::unique_ptr<Mediator> mediator, tr_priority_t priority);
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
void remove(tr_sha1_digest_t const& info_hash);
|
2022-09-06 04:43:59 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node
|
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
Node(std::unique_ptr<Mediator> mediator, tr_priority_t priority) noexcept
|
|
|
|
: mediator_{ std::move(mediator) }
|
|
|
|
, priority_{ priority }
|
|
|
|
{
|
|
|
|
}
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
[[nodiscard]] int compare(Node const& that) const noexcept; // <=>
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
[[nodiscard]] auto operator<(Node const& that) const noexcept
|
2022-09-06 04:43:59 +00:00
|
|
|
{
|
|
|
|
return compare(that) < 0;
|
|
|
|
}
|
2009-05-29 19:17:12 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
[[nodiscard]] bool matches(tr_sha1_digest_t const& info_hash) const noexcept
|
2022-09-06 04:43:59 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
return mediator_->metainfo().info_hash() == info_hash;
|
2022-09-06 04:43:59 +00:00
|
|
|
}
|
2023-10-19 13:39:34 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Mediator> mediator_;
|
|
|
|
tr_priority_t priority_;
|
|
|
|
};
|
|
|
|
|
2023-12-17 17:16:48 +00:00
|
|
|
static void verify_torrent(Mediator& verify_mediator, std::atomic<bool> const& abort_flag);
|
2022-02-09 18:02:59 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void verify_thread_func();
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
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_;
|
2022-11-14 05:11:48 +00:00
|
|
|
|
|
|
|
std::atomic<bool> stop_current_ = false;
|
|
|
|
std::condition_variable stop_current_cv_;
|
2022-09-06 04:43:59 +00:00
|
|
|
};
|