diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 3a40482f..ce336fe7 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -660,8 +660,10 @@ namespace util } s << "
"; s << "Tunnel creation success rate: " << i2p::tunnel::tunnels.GetTunnelCreationSuccessRate () << "%
"; - s << "Received: " << i2p::transport::transports.GetTotalReceivedBytes ()/1000 << "K
"; - s << "Sent: " << i2p::transport::transports.GetTotalSentBytes ()/1000 << "K
"; + s << "Received: " << i2p::transport::transports.GetTotalReceivedBytes ()/1000 << "K"; + s << " (" << i2p::transport::transports.GetInBandwidth () <<" Bps)
"; + s << "Sent: " << i2p::transport::transports.GetTotalSentBytes ()/1000 << "K"; + s << " (" << i2p::transport::transports.GetOutBandwidth () <<" Bps)
"; s << "Data path: " << i2p::util::filesystem::GetDataDir().string() << "

"; s << "Our external address:" << "
" ; for (auto& address : i2p::context.GetRouterInfo().GetAddresses()) diff --git a/Transports.cpp b/Transports.cpp index 9d8a55e6..5afb6f36 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -97,7 +97,8 @@ namespace transport Transports::Transports (): m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service), m_PeerCleanupTimer (m_Service), m_NTCPServer (nullptr), m_SSUServer (nullptr), m_DHKeysPairSupplier (5), // 5 pre-generated keys - m_TotalSentBytes(0), m_TotalReceivedBytes(0) + m_TotalSentBytes(0), m_TotalReceivedBytes(0), m_InBandwidth (0), m_OutBandwidth (0), + m_LastInBandwidthUpdateBytes (0), m_LastOutBandwidthUpdateBytes (0), m_LastBandwidthUpdateTime (0) { } @@ -181,6 +182,23 @@ namespace transport } } + void Transports::UpdateBandwidth () + { + uint64_t ts = i2p::util::GetMillisecondsSinceEpoch (); + if (m_LastBandwidthUpdateTime > 0) + { + auto delta = ts - m_LastBandwidthUpdateTime; + if (delta > 0) + { + m_InBandwidth = (m_TotalReceivedBytes - m_LastInBandwidthUpdateBytes)*1000/ts; // per second + m_OutBandwidth = (m_TotalSentBytes - m_LastOutBandwidthUpdateBytes)*1000/ts; // per second + } + } + m_LastBandwidthUpdateTime = ts; + m_LastInBandwidthUpdateBytes = m_TotalReceivedBytes; + m_LastOutBandwidthUpdateBytes = m_TotalSentBytes; + } + void Transports::SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg) { @@ -471,6 +489,7 @@ namespace transport else it++; } + UpdateBandwidth (); // TODO: use separate timer(s) for it m_PeerCleanupTimer.expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT)); m_PeerCleanupTimer.async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1)); } diff --git a/Transports.h b/Transports.h index 35416329..173ff87d 100644 --- a/Transports.h +++ b/Transports.h @@ -92,8 +92,10 @@ namespace transport void UpdateSentBytes (uint64_t numBytes) { m_TotalSentBytes += numBytes; }; void UpdateReceivedBytes (uint64_t numBytes) { m_TotalReceivedBytes += numBytes; }; uint64_t GetTotalSentBytes () const { return m_TotalSentBytes; }; - uint64_t GetTotalReceivedBytes () const { return m_TotalReceivedBytes; }; - + uint64_t GetTotalReceivedBytes () const { return m_TotalReceivedBytes; }; + uint32_t GetInBandwidth () const { return m_InBandwidth; }; // bytes per second + uint32_t GetOutBandwidth () const { return m_OutBandwidth; }; // bytes per second + private: void Run (); @@ -109,6 +111,7 @@ namespace transport void HandleNTCPResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it, i2p::data::IdentHash ident, std::shared_ptr resolver); + void UpdateBandwidth (); void DetectExternalIP (); private: @@ -124,8 +127,12 @@ namespace transport std::map m_Peers; DHKeysPairSupplier m_DHKeysPairSupplier; + std::atomic m_TotalSentBytes, m_TotalReceivedBytes; - + uint32_t m_InBandwidth, m_OutBandwidth; + uint64_t m_LastInBandwidthUpdateBytes, m_LastOutBandwidthUpdateBytes; + uint64_t m_LastBandwidthUpdateTime; + public: // for HTTP only