diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index 7cffe759..32fd5dbb 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -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 owner, std::shared_ptr stream): i2p::client::I2PServiceHandler (owner), m_Stream (stream), m_ReceiveBufferOffset (0), m_NextMsgLength (0), m_MaxNumRequests (MIN_NUM_REQUESTS), diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 5b036afa..366e94c5 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -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 > 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; diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index 2a0aa6c4..20e5a4e8 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -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 handler) mutable - { - if (handler) - { - auto conn = std::static_pointer_cast(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 diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 15340fee..5e9c6b4a 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -147,7 +147,6 @@ namespace torrents size_t ConnectToPeers (std::shared_ptr torrent); size_t ConnectToPeers (std::shared_ptr torrent, size_t trackerID); void UpdatePeersPerPiece (std::shared_ptr 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);