bytesCompleted for torrents with multiple files

This commit is contained in:
orignal
2026-08-23 14:32:20 -04:00
parent 6184157d3c
commit 895d10ed23
3 changed files with 34 additions and 1 deletions
+27
View File
@@ -659,6 +659,33 @@ namespace torrents
}
}
std::vector<size_t> Torrent::GetFilesCompleted () const
{
std::vector<size_t> completed;
if (!m_Files.empty ())
{
auto filesIT = m_Files.begin ();
size_t currentSize = 0, currentCompletedSize = 0;
for (const auto& piece: m_Pieces)
{
if (currentSize + m_PieceLength < filesIT->second)
{
currentSize += m_PieceLength;
if (piece.IsComplete ()) currentCompletedSize += m_PieceLength;
}
else
{
if (piece.IsComplete ()) currentCompletedSize += (filesIT->second - currentSize);
completed.push_back (currentCompletedSize);
currentSize = 0; currentCompletedSize = 0;
filesIT++;
if (filesIT == m_Files.end ()) break;
}
}
}
return completed;
}
PeerConnection::PeerConnection (std::shared_ptr<i2p::client::I2PService> owner,
std::shared_ptr<i2p::stream::Stream> stream): i2p::client::I2PServiceHandler (owner),
m_Stream (stream), m_ReceiveBufferOffset (0), m_NextMsgLength (0),
+1
View File
@@ -186,6 +186,7 @@ namespace torrents
const std::unordered_set<i2p::data::IdentHash>& GetPeers () const { return m_Peers; }
std::tuple<uint32_t, uint32_t, uint32_t> GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn, bool skipRequested = true); // return (index, offset, len)
std::vector<PieceFileFragment> GetPieceFileFragments (int index) const;
std::vector<size_t> GetFilesCompleted () const; // completed size per file
bool UpdateStatus (uint64_t ts); // return true if complete
uint64_t GetNextTrackerRequestTime () const { return m_NextTrackerRequestTime; }
+6 -1
View File
@@ -292,14 +292,19 @@ namespace torrents
files.push_back (file);
}
else
{
auto filesCompleted = torrent->GetFilesCompleted ();
size_t ind = 0;
for (const auto& [filePath, fileSize]: torrentFiles)
{
boost::json::object file;
file["name"] = filePath.string ();
file["length"] = fileSize;
file["bytesCompleted"] = 0; // TODO:
file["bytesCompleted"] = (ind < filesCompleted.size ()) ? filesCompleted[ind] : 0;
files.push_back (file);
ind++;
}
}
return files;
}
},