Back to Site
Loading...
Searching...
No Matches
bittorrent.h
Go to the documentation of this file.
1#pragma once
2
27#include "librats/util/rats_export.h"
28#include "librats/node/peer_network.h" // Subsystem, NodeContext (fwd)
29#include "librats/bittorrent/client.h" // bittorrent::Client, TorrentInfo, InfoHash
30#include "librats/dht/dht.h" // DhtClient, SpiderAnnounceCallback
31
32#include <condition_variable>
33#include <cstddef>
34#include <cstdint>
35#include <functional>
36#include <memory>
37#include <mutex>
38#include <string>
39
40namespace librats {
41
42class ServiceRegistry;
43
44class RATS_API Bittorrent final : public Subsystem {
45public:
46 struct Config {
47 bittorrent::Client::Config client;
48 bool use_node_dht = true;
49 };
50
51 Bittorrent() = default;
52 explicit Bittorrent(Config config);
54 explicit Bittorrent(const bittorrent::Client::Config& client_config);
55 ~Bittorrent() override;
56
57 Bittorrent(const Bittorrent&) = delete;
58 Bittorrent& operator=(const Bittorrent&) = delete;
59
60 // — Subsystem —
61 void attach(NodeContext& ctx) override;
62 void start() override;
63 void stop() override;
64
68 bittorrent::Client* client() noexcept { return client_.get(); }
69 const bittorrent::Client* client() const noexcept { return client_.get(); }
70
71 bool is_running() const { return client_ && client_->is_running(); }
73 bool using_node_dht() const noexcept { return shared_dht_; }
74
75 //=========================================================================
76 // Spider mode (rats-search) — DHT-wide infohash crawling.
77 // Delegates to the active DHT (the shared node DHT). No-op before start()
78 // or when no DHT is shared.
79 //
80 // Guarded to match dht.h, which declares SpiderAnnounceCallback behind the
81 // same macro: the whole subsystem needs RATS_SEARCH_FEATURES to link, but the
82 // header must still *parse* without it (IDE indexers, a consumer probing the
83 // installed tree) rather than fail on an undeclared type.
84 //=========================================================================
85#ifdef RATS_SEARCH_FEATURES
86 void set_spider_mode(bool enable);
87 bool is_spider_mode() const;
88 void set_spider_announce_callback(SpiderAnnounceCallback callback);
89 void set_spider_ignore(bool ignore);
90 bool is_spider_ignoring() const;
92 size_t spider_pool_size() const;
93 size_t spider_visited_count() const;
95#endif
96
97 //=========================================================================
98 // Metadata-only fetch (BEP 9). Adds a temporary metadata-only magnet torrent,
99 // waits up to timeout_ms for its info dict to arrive, removes the temporary
100 // torrent, then invokes `callback` with the result (success=false on timeout).
101 // `callback` runs on an internal worker thread; it is invoked exactly once.
102 //=========================================================================
103 using MetadataCallback = std::function<void(const bittorrent::TorrentInfo& info, bool success,
104 const std::string& error)>;
105
107 void get_torrent_metadata(const std::string& info_hash_hex,
108 MetadataCallback callback, int timeout_ms = 60000);
109
112 void get_torrent_metadata_from_peer(const std::string& info_hash_hex,
113 const std::string& ip, uint16_t port,
114 MetadataCallback callback, int timeout_ms = 60000);
115
116private:
118 DhtClient* active_dht() const;
119
120 void fetch_metadata_impl(const std::string& info_hash_hex, const std::string& ip,
121 uint16_t port, bool direct, MetadataCallback callback, int timeout_ms);
122
123 Config config_;
124 ServiceRegistry* services_ = nullptr;
125 std::unique_ptr<bittorrent::Client> client_;
126 bool shared_dht_ = false;
127
128 // In-flight metadata watchers; stop() drains them before tearing down client_.
129 std::mutex meta_mutex_;
130 std::condition_variable meta_cv_;
131 std::condition_variable meta_drain_cv_;
132 bool meta_stopping_ = false;
133 int meta_inflight_ = 0;
134};
135
136} // namespace librats
bool using_node_dht() const noexcept
True when start() borrowed the node's DHT rather than running DHT-less.
Definition bittorrent.h:73
Bittorrent(Config config)
Bittorrent(const bittorrent::Client::Config &client_config)
Convenience: configure the underlying client directly, share the node DHT.
bool is_spider_mode() const
size_t spider_pool_size() const
std::function< void(const bittorrent::TorrentInfo &info, bool success, const std::string &error)> MetadataCallback
Definition bittorrent.h:104
bittorrent::Client * client() noexcept
The underlying client — drive torrents through it (add_magnet, add_torrent_file, add_torrent_for_seed...
Definition bittorrent.h:68
void start() override
Bittorrent & operator=(const Bittorrent &)=delete
void set_spider_mode(bool enable)
void attach(NodeContext &ctx) override
size_t spider_visited_count() const
void set_spider_ignore(bool ignore)
bool is_spider_ignoring() const
void set_spider_announce_callback(SpiderAnnounceCallback callback)
void get_torrent_metadata_from_peer(const std::string &info_hash_hex, const std::string &ip, uint16_t port, MetadataCallback callback, int timeout_ms=60000)
Fetch metadata directly from a known peer (the fast path used when a spider announce told us exactly ...
Bittorrent(const Bittorrent &)=delete
void get_torrent_metadata(const std::string &info_hash_hex, MetadataCallback callback, int timeout_ms=60000)
Fetch metadata by searching the DHT/trackers for peers that have it.
void stop() override
bool is_running() const
Definition bittorrent.h:71
~Bittorrent() override
const bittorrent::Client * client() const noexcept
Definition bittorrent.h:69
A pluggable network subsystem.
Definition node.h:73
The narrow contract a subsystem needs from the node — and nothing more.
bittorrent::Client::Config client
download path, listen port, …
Definition bittorrent.h:47