Furrent
torrent.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <cstdint>
5 #include <deque>
6 #include <peer.hpp>
7 #include <random>
8 #include <string>
9 #include <types.hpp>
10 #include <vector>
11 
13 #include "hash.hpp"
14 
15 namespace fur {
16 
18 struct File {
21  std::vector<std::string> filepath;
23  int64_t length;
24 
25  // =======================================================
26 
28  [[nodiscard]] std::string filename() const;
29 };
30 
32 struct TorrentFile {
35  std::string announce_url;
40  std::vector<hash::hash_t> piece_hashes;
42  int64_t piece_length = 0;
44  int64_t length = 0;
46  int64_t pieces_count = 0;
48  std::string name;
49 
51  std::string folder_name;
53  std::vector<File> files;
54 
56  explicit TorrentFile() = default;
57 
60  explicit TorrentFile(const bencode::BencodeValue& tree);
61 };
62 
64 struct Subpiece {
66  std::string filepath;
68  int64_t file_offset;
70  int64_t len;
71 };
72 
75 struct Piece {
77  int64_t index;
79  std::vector<Subpiece> subpieces;
80 };
81 
82 enum class TorrentState {
83  Loading,
85  Paused,
86  Completed,
87  Stopped,
88  Error,
89 };
90 
92 class Torrent {
93  // Unique identifier for each torrent
94  TorrentID _tid;
96  TorrentFile _descriptor;
97 
99  std::vector<peer::Peer> _peers;
101  std::deque<std::atomic_int64_t> _peers_score;
103  int64_t _update_interval;
104 
105  public:
108  std::atomic<TorrentState> state;
109 
112  std::atomic_int64_t pieces_processed;
113 
114  public:
116  explicit Torrent();
117 
122 
124  void announce();
125 
128  void atomic_add_peer_score(int64_t peer_index);
129 
131  [[nodiscard]] TorrentID tid() const;
132 
134  [[nodiscard]] const TorrentFile& descriptor() const;
135 
137  [[nodiscard]] std::vector<peer::Peer> peers() const;
138 
140  [[nodiscard]] std::discrete_distribution<int64_t> distribution() const;
141 
143  [[nodiscard]] std::vector<Piece> pieces() const;
144 };
145 
146 } // namespace fur
fur::Torrent::tid
TorrentID tid() const
Returns unique id.
Definition: torrent.cpp:171
hash.hpp
fur::TorrentFile::info_hash
hash::hash_t info_hash
Definition: torrent.hpp:38
fur::File::filename
std::string filename() const
Definition: torrent.cpp:16
fur::TorrentFile::TorrentFile
TorrentFile()=default
Construct an empty TorrentFile instance.
fur::Piece::subpieces
std::vector< Subpiece > subpieces
Mapping piece-files.
Definition: torrent.hpp:79
fur::Subpiece::file_offset
int64_t file_offset
Offset from the beginning of the file.
Definition: torrent.hpp:68
types.hpp
fur::Piece::index
int64_t index
Global download index.
Definition: torrent.hpp:77
fur::Subpiece::len
int64_t len
Size in bytes.
Definition: torrent.hpp:70
fur::TorrentState::Stopped
@ Stopped
fur::hash::hash_t
std::array< uint8_t, 20 > hash_t
Type for a SHA1 hash.
Definition: hash.hpp:13
fur::TorrentID
int64_t TorrentID
Definition: types.hpp:8
fur::Torrent::peers
std::vector< peer::Peer > peers() const
Returns the loaded peers.
Definition: torrent.cpp:175
peer.hpp
fur::TorrentFile::piece_hashes
std::vector< hash::hash_t > piece_hashes
SHA1 hashes, one for each piece of the shared file.
Definition: torrent.hpp:40
fur::Torrent::atomic_add_peer_score
void atomic_add_peer_score(int64_t peer_index)
Definition: torrent.cpp:155
fur::TorrentFile::folder_name
std::string folder_name
Name of the folder containing all torrent files.
Definition: torrent.hpp:51
fur::File::length
int64_t length
Number of bytes in the file.
Definition: torrent.hpp:23
fur::Torrent::Torrent
Torrent()
Construct empty temporary torrent.
Definition: torrent.cpp:118
fur::TorrentFile::files
std::vector< File > files
Describe the structure of the file.
Definition: torrent.hpp:53
fur::TorrentState::Loading
@ Loading
fur::TorrentFile::piece_length
int64_t piece_length
The length, in bytes, of each piece.
Definition: torrent.hpp:42
fur::TorrentFile
Represents a parsed .torrent file.
Definition: torrent.hpp:32
fur::Torrent::pieces_processed
std::atomic_int64_t pieces_processed
Definition: torrent.hpp:112
fur::Subpiece::filepath
std::string filepath
Path to the file this subpiece belongs to.
Definition: torrent.hpp:66
fur::TorrentState
TorrentState
Definition: torrent.hpp:82
fur::Subpiece
Describes a subsection of a Piece, it is mapped to a single file.
Definition: torrent.hpp:64
bencode_value.hpp
fur::bencode::BencodeValue
Definition: bencode_value.hpp:14
fur::File
Describes a file inside a torrent.
Definition: torrent.hpp:18
fur::TorrentState::Downloading
@ Downloading
fur::Torrent::announce
void announce()
Announces the client to the tracker and sets the list of peers.
Definition: torrent.cpp:133
fur::TorrentFile::pieces_count
int64_t pieces_count
Total number of pieces.
Definition: torrent.hpp:46
fur::TorrentFile::length
int64_t length
The length, in bytes, of the entire shared file.
Definition: torrent.hpp:44
fur::TorrentState::Completed
@ Completed
fur::Torrent::descriptor
const TorrentFile & descriptor() const
Returns the .torrent descriptor.
Definition: torrent.cpp:173
fur::TorrentFile::name
std::string name
The name of the shared file.
Definition: torrent.hpp:48
fur::TorrentFile::announce_url
std::string announce_url
Definition: torrent.hpp:35
fur::TorrentState::Error
@ Error
fur::Torrent::distribution
std::discrete_distribution< int64_t > distribution() const
Returns a peer distribution.
Definition: torrent.cpp:161
fur::Torrent
Completely describes a torrent in furrent.
Definition: torrent.hpp:92
fur::File::filepath
std::vector< std::string > filepath
Definition: torrent.hpp:21
fur::Piece
Definition: torrent.hpp:75
fur::Torrent::state
std::atomic< TorrentState > state
Definition: torrent.hpp:108
fur
Common types used in all the project.
Definition: bencode_parser.cpp:7
fur::TorrentState::Paused
@ Paused
fur::Torrent::pieces
std::vector< Piece > pieces() const
Generate all pieces of this torrent.
Definition: torrent.cpp:178