-
-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathdht_tracker.h
More file actions
57 lines (40 loc) · 1.8 KB
/
dht_tracker.h
File metadata and controls
57 lines (40 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef LIBTORRENT_DHT_TRACKER_H
#define LIBTORRENT_DHT_TRACKER_H
#include <vector>
#include "net/address_list.h" // For SA.
#include "torrent/object_raw_bencode.h"
namespace torrent {
// Container for peers tracked in a torrent.
class DhtTracker {
public:
// Maximum number of peers we return for a GET_PEERS query (default value only).
// Needs to be small enough so that a packet with a payload of num_peers*6 bytes
// does not need fragmentation. Value chosen so that the size is approximately
// equal to a FIND_NODE reply (8*26 bytes).
static constexpr unsigned int max_peers = 32;
// Maximum number of peers we keep track of. For torrents with more peers,
// we replace the oldest peer with each new announce to avoid excessively
// large peer tables for very active torrents.
static constexpr unsigned int max_size = 128;
bool empty() const { return m_peers.empty(); }
size_t size() const { return m_peers.size(); }
void add_peer(uint32_t addr_n, uint16_t port);
raw_list get_peers(unsigned int maxPeers = max_peers);
// Remove old announces from the tracker that have not reannounced for
// more than the given number of seconds.
void prune(uint32_t maxAge);
private:
// We need to store the address as a bencoded string.
struct [[gnu::packed]] BencodeAddress {
char header[2];
SocketAddressCompact peer;
BencodeAddress(SocketAddressCompact p) : peer(p) { header[0] = '6'; header[1] = ':'; }
const char* bencode() const { return header; }
bool empty() const { return !peer.port; }
};
using PeerList = std::vector<BencodeAddress>;
PeerList m_peers;
std::vector<uint32_t> m_lastSeen;
};
} // namespace torrent
#endif