#pragma once #include "TorrentFile.hpp" #include #include "../Threading/Thread.hpp" #include "../Random.hpp" namespace Tesses::Framework::BitTorrent { class ActiveTorrent; class TorrentBitField { std::vector 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& 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 stream; std::vector cancel_requests; std::vector messages; TorrentBitField has; std::vector> blocksRequested; std::string ip; uint16_t port; }; class ActiveTorrent { public: Random rng; ActiveTorrent(TorrentFile file, std::shared_ptr vfs,Tesses::Framework::Filesystem::VFSPath directory, std::string peer_id); std::shared_ptr vfs; std::map 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> blocksAquired; TorrentBitField has; TorrentFile file; int64_t torrentSize; //to make it more efficient Tesses::Framework::Filesystem::VFSPath directory; std::vector> connections; std::shared_ptr torrent_disk; size_t pieceSize(size_t piece); std::array buffer; void process(); bool processMessages(std::shared_ptr peer); }; class TorrentManager { std::shared_ptr vfs; Tesses::Framework::Filesystem::VFSPath defaultDirectory; std::vector> downloading; std::vector> seeding; std::queue> torrentQueue; int torrentCount; std::atomic running=false; public: TorrentManager(std::shared_ptr 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(); }