calculate stats in real time

This commit is contained in:
orignal
2026-09-13 19:09:36 -04:00
parent b8a1c56d25
commit 2ff4eb7c57
4 changed files with 40 additions and 38 deletions
+36 -1
View File
@@ -368,7 +368,6 @@ namespace torrents
m_Length (0), m_PieceLength (0), m_IsComplete (false), m_IsStopped (false), m_IsSingleFile (true),
m_Uploaded (0), m_Downloaded (0), m_NextUpdateStatusTime (0), m_NextReconnectTime (0)
{
ResetStats ();
}
Torrent::Torrent (std::string_view buf): Torrent ()
@@ -1015,6 +1014,42 @@ namespace torrents
return false;
}
uint64_t Torrent::GetDownloadRate ()
{
uint64_t downloadRate = 0;
auto conns = GetConnections ();
for (auto it: conns)
downloadRate += it->GetDownloadRate ();
return downloadRate;
}
uint64_t Torrent::GetUploadRate ()
{
uint64_t uploadRate = 0;
auto conns = GetConnections ();
for (auto it: conns)
uploadRate += it->GetUploadRate ();
return uploadRate;
}
int Torrent::GetNumDownloadingFromPeers ()
{
int numDownloadingFromPeers = 0;
auto conns = GetConnections ();
for (auto it: conns)
if (it->IsDownloading ()) numDownloadingFromPeers++;
return numDownloadingFromPeers;
}
int Torrent::GetNumUploadingToPeers ()
{
int numUploadingToPeers = 0;
auto conns = GetConnections ();
for (auto it: conns)
if (it->IsUploading ()) numUploadingToPeers++;
return numUploadingToPeers;
}
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), m_MaxNumRequests (MIN_NUM_REQUESTS),
+4 -12
View File
@@ -253,15 +253,10 @@ namespace torrents
void ApplyPeerRemoteBitfield (const boost::dynamic_bitset<>& peerRemoteBitfield);
bool HasIncompletePieces (const boost::dynamic_bitset<>& peerRemoteBitfield) const; // if remote bitfie;d has incomplete pieces
void ResetStats () { m_DownloadRate = 0; m_UploadRate = 0; m_NumDownloadingFromPeers = 0; m_NumUploadingToPeers = 0; }
uint64_t GetDownloadRate () const { return m_DownloadRate; }
void SetDownloadRate (uint64_t downloadRate) { m_DownloadRate = downloadRate; }
uint64_t GetUploadRate () const { return m_UploadRate; }
void SetUploadRate (uint64_t uploadRate) { m_UploadRate = uploadRate; }
int GetNumDownloadingFromPeers () const { return m_NumDownloadingFromPeers; }
void SetNumDownloadingFromPeers (int numDownloadingFromPeers) { m_NumDownloadingFromPeers = numDownloadingFromPeers; }
int GetNumUploadingToPeers () const { return m_NumUploadingToPeers; }
void SetNumUploadingToPeers (int numUploadingToPeers) { m_NumUploadingToPeers = numUploadingToPeers; }
uint64_t GetDownloadRate ();
uint64_t GetUploadRate ();
int GetNumDownloadingFromPeers ();
int GetNumUploadingToPeers ();
int GetNumSeeders (size_t trackerID) const { return (trackerID < m_TrackerStats.size ()) ? std::get<3>(m_TrackerStats[trackerID]) : 0; }
int GetNumLeechers (size_t trackerID) const { return (trackerID < m_TrackerStats.size ()) ? std::get<4>(m_TrackerStats[trackerID]) : 0; }
@@ -293,9 +288,6 @@ namespace torrents
std::list<std::shared_ptr<TorrentFile> > m_Files;
size_t m_Uploaded, m_Downloaded;
uint64_t m_NextUpdateStatusTime, m_NextReconnectTime; // in monotonic seconds
// stats
uint64_t m_DownloadRate, m_UploadRate; // B/sec
int m_NumDownloadingFromPeers, m_NumUploadingToPeers; // by us
};
class TorrentsTunnel;
-24
View File
@@ -820,7 +820,6 @@ namespace torrents
}
it.second->SetNextUpdateStatusTime (ts + TORRENTS_STATUS_UPDATE_INTERVAL + GetLocalDestination ()->GetRng ()() % TORRENTS_STATUS_UPDATE_INTERVAL_VARIANCE);
}
UpdateStats ();
ScheduleStatusUpdate ();
}
}
@@ -840,29 +839,6 @@ namespace torrents
});
}
void TorrentsTunnel::UpdateStats ()
{
for (auto it: m_Torrents)
it.second->ResetStats ();
IterateHandlers ([](std::shared_ptr<i2p::client::I2PServiceHandler> handler) mutable
{
if (handler)
{
auto conn = std::static_pointer_cast<PeerConnection>(handler);
auto torrent = conn->GetTorrent ();
if (torrent)
{
torrent->SetDownloadRate (torrent->GetDownloadRate () + conn->GetDownloadRate ());
torrent->SetUploadRate (torrent->GetUploadRate () + conn->GetUploadRate ());
if (conn->IsDownloading ())
torrent->SetNumDownloadingFromPeers (torrent->GetNumDownloadingFromPeers () + 1);
if (conn->IsUploading ())
torrent->SetNumUploadingToPeers (torrent->GetNumUploadingToPeers () + 1);
}
}
});
}
void TorrentsTunnel::HandleRecvFromI2PRaw (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
{
// response from tracker
-1
View File
@@ -147,7 +147,6 @@ namespace torrents
size_t ConnectToPeers (std::shared_ptr<Torrent> torrent);
size_t ConnectToPeers (std::shared_ptr<Torrent> torrent, size_t trackerID);
void UpdatePeersPerPiece (std::shared_ptr<Torrent> torrent);
void UpdateStats ();
void HandleRecvFromI2PRaw (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
void ConnectToDatagramTracker (size_t trackerID, std::string_view dest, uint16_t port);