mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-02-08 15:55:46 +00:00
Push failed torrent code as backup before I remove it
This commit is contained in:
70
include/TessesFramework/BitTorrent/TorrentFile.hpp
Normal file
70
include/TessesFramework/BitTorrent/TorrentFile.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
#include "../Serialization/Bencode.hpp"
|
||||
#include "TorrentStream.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
constexpr int DEFAULT_PIECE_LENGTH = 524288;
|
||||
|
||||
class TorrentFileInfo {
|
||||
|
||||
Serialization::Bencode::BeDictionary info;
|
||||
public:
|
||||
TorrentFileInfo();
|
||||
TorrentFileInfo(const Serialization::Bencode::BeDictionary& tkn);
|
||||
|
||||
|
||||
int64_t piece_length=DEFAULT_PIECE_LENGTH;
|
||||
Serialization::Bencode::BeString pieces;
|
||||
int64_t isPrivate=0;
|
||||
Serialization::Bencode::BeString name;
|
||||
std::variant<int64_t, std::vector<TorrentFileEntry>> fileListOrLength;
|
||||
int64_t GetTorrentFileSize();
|
||||
/// @brief Generate the info
|
||||
/// @return the dictionary containing the info
|
||||
Serialization::Bencode::BeDictionary& GenerateInfoDictionary();
|
||||
/// @brief Get the info hash for info (if you are filling out the fields, you need to call GenerateInfoDictionary first)
|
||||
/// @return the 20 byte info hash
|
||||
Serialization::Bencode::BeString GetInfoHash();
|
||||
/// @brief Get the info (does not regenerate info from the fields)
|
||||
/// @return the dictionary containing the info
|
||||
Serialization::Bencode::BeDictionary& GetInfoDictionary();
|
||||
|
||||
/// @brief Used to load a dictionary in, will overwrite all the fields
|
||||
/// @param dict the dictionary to load
|
||||
void Load(const Serialization::Bencode::BeDictionary& dict);
|
||||
/// @brief Gets a read write based on this info object
|
||||
/// @param vfs the vfs object
|
||||
/// @param path file or directory inside vfs
|
||||
/// @return The read write at object
|
||||
std::shared_ptr<ReadWriteAt> GetStreamFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path);
|
||||
|
||||
/// @brief Create info from file/directory
|
||||
/// @param vfs the vfs object
|
||||
/// @param path file or directory inside vfs
|
||||
/// @param isPrivate whether you use private trackers
|
||||
/// @param pieceLength length of pieces
|
||||
void CreateFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate=false, int64_t pieceLength=DEFAULT_PIECE_LENGTH);
|
||||
};
|
||||
class TorrentFile {
|
||||
public:
|
||||
TorrentFile();
|
||||
TorrentFile(const Serialization::Bencode::BeDictionary& tkn);
|
||||
|
||||
|
||||
Serialization::Bencode::BeString announce;
|
||||
std::vector<Serialization::Bencode::BeString> announce_list;
|
||||
std::vector<Serialization::Bencode::BeString> url_list;
|
||||
Serialization::Bencode::BeString comment;
|
||||
Serialization::Bencode::BeString created_by;
|
||||
int64_t creation_date=0;
|
||||
TorrentFileInfo info;
|
||||
|
||||
|
||||
Serialization::Bencode::BeDictionary ToDictionary();
|
||||
|
||||
void Print(std::shared_ptr<TextStreams::TextWriter> writer);
|
||||
|
||||
static void CreateTorrent(std::shared_ptr<Tesses::Framework::Streams::Stream> torrent_file_stream,const std::vector<Serialization::Bencode::BeString>& trackers,const std::vector<Serialization::Bencode::BeString>& webseeds, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate=false, int64_t pieceLength=DEFAULT_PIECE_LENGTH, Serialization::Bencode::BeString comment="", Serialization::Bencode::BeString created_by = "TessesFrameworkTorrent");
|
||||
|
||||
};
|
||||
}
|
||||
95
include/TessesFramework/BitTorrent/TorrentManager.hpp
Normal file
95
include/TessesFramework/BitTorrent/TorrentManager.hpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include "TorrentFile.hpp"
|
||||
#include <queue>
|
||||
#include "../Threading/Thread.hpp"
|
||||
#include "../Random.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
|
||||
class ActiveTorrent;
|
||||
|
||||
class TorrentBitField {
|
||||
std::vector<uint8_t> bits;
|
||||
size_t no_bits=0;
|
||||
public:
|
||||
TorrentBitField();
|
||||
TorrentBitField(size_t bits);
|
||||
bool get(size_t index);
|
||||
void set(size_t index, bool val);
|
||||
void zero();
|
||||
size_t size();
|
||||
void resize(size_t len);
|
||||
std::vector<uint8_t>& data();
|
||||
bool allone();
|
||||
};
|
||||
struct CancelRequest {
|
||||
uint32_t piece;
|
||||
uint32_t begin;
|
||||
uint32_t length;
|
||||
};
|
||||
|
||||
class TorrentPeer {
|
||||
public:
|
||||
bool isChokingMe;
|
||||
bool isChoked;
|
||||
bool intrested;
|
||||
std::shared_ptr<Tesses::Framework::Streams::NetworkStream> stream;
|
||||
std::vector<CancelRequest> cancel_requests;
|
||||
std::vector<uint8_t> messages;
|
||||
TorrentBitField has;
|
||||
|
||||
std::vector<std::optional<TorrentBitField>> blocksRequested;
|
||||
std::string ip;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
class ActiveTorrent {
|
||||
public:
|
||||
Random rng;
|
||||
ActiveTorrent(TorrentFile file, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs,Tesses::Framework::Filesystem::VFSPath directory, std::string peer_id);
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
std::map<std::string,uint64_t> udp_connection_ids;
|
||||
int64_t downloaded;
|
||||
int64_t uploaded;
|
||||
int64_t getLeft();
|
||||
void addPeer(std::string ip, uint16_t port);
|
||||
Tesses::Framework::Serialization::Bencode::BeString info_hash;
|
||||
std::string peer_id;
|
||||
time_t lastTime;
|
||||
bool mustAnnounce();
|
||||
void udpAnounce(Tesses::Framework::Http::Uri uri);
|
||||
void httpAnounce(std::string url);
|
||||
Tesses::Framework::Threading::Mutex mtx;
|
||||
std::vector<std::optional<TorrentBitField>> blocksAquired;
|
||||
TorrentBitField has;
|
||||
TorrentFile file;
|
||||
int64_t torrentSize; //to make it more efficient
|
||||
Tesses::Framework::Filesystem::VFSPath directory;
|
||||
std::vector<std::shared_ptr<TorrentPeer>> connections;
|
||||
std::shared_ptr<ReadWriteAt> torrent_disk;
|
||||
|
||||
size_t pieceSize(size_t piece);
|
||||
std::array<uint8_t,1024> buffer;
|
||||
void process();
|
||||
bool processMessages(std::shared_ptr<TorrentPeer> peer);
|
||||
};
|
||||
class TorrentManager
|
||||
{
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
Tesses::Framework::Filesystem::VFSPath defaultDirectory;
|
||||
std::vector<std::shared_ptr<ActiveTorrent>> downloading;
|
||||
std::vector<std::shared_ptr<ActiveTorrent>> seeding;
|
||||
std::queue<std::pair<TorrentFile,Tesses::Framework::Filesystem::VFSPath>> torrentQueue;
|
||||
int torrentCount;
|
||||
std::atomic<bool> running=false;
|
||||
public:
|
||||
TorrentManager(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath defaultDirectory, int torrentCount);
|
||||
void AddTorrent(TorrentFile file);
|
||||
void AddTorrent(TorrentFile file, Tesses::Framework::Filesystem::VFSPath directory);
|
||||
void Start();
|
||||
void Stop();
|
||||
~TorrentManager();
|
||||
};
|
||||
std::string GeneratePeerId();
|
||||
|
||||
}
|
||||
46
include/TessesFramework/BitTorrent/TorrentStream.hpp
Normal file
46
include/TessesFramework/BitTorrent/TorrentStream.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "TessesFramework/Http/HttpClient.hpp"
|
||||
#include "TessesFramework/Streams/NetworkStream.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
class TorrentFileEntry {
|
||||
public:
|
||||
TorrentFileEntry();
|
||||
TorrentFileEntry(Tesses::Framework::Filesystem::VFSPath path, int64_t length);
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
int64_t length=0;
|
||||
};
|
||||
class ReadWriteAt {
|
||||
public:
|
||||
virtual bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len)=0;
|
||||
virtual void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len)=0;
|
||||
virtual ~ReadWriteAt();
|
||||
|
||||
};
|
||||
|
||||
class TorrentFileStream : public ReadWriteAt
|
||||
{
|
||||
Tesses::Framework::Threading::Mutex mtx;
|
||||
public:
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
uint64_t length;
|
||||
TorrentFileStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path, uint64_t length);
|
||||
bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len);
|
||||
void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len);
|
||||
|
||||
};
|
||||
class TorrentDirectoryStream : public ReadWriteAt
|
||||
{
|
||||
|
||||
std::vector<Tesses::Framework::Threading::Mutex> mtxs;
|
||||
public:
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
std::vector<TorrentFileEntry> entries;
|
||||
TorrentDirectoryStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path,std::vector<TorrentFileEntry> entries);
|
||||
bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len);
|
||||
void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user