From a3cafd3cfd6f19c30ec61eae58f89fcfdf1df5a0 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 5 Mar 2026 21:11:40 -0500 Subject: [PATCH] replace deadline_timer by steady_timer --- libi2pd_client/I2PService.cpp | 21 ++++++------ libi2pd_client/I2PService.h | 60 ++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/libi2pd_client/I2PService.cpp b/libi2pd_client/I2PService.cpp index 4ec2648a..01914b52 100644 --- a/libi2pd_client/I2PService.cpp +++ b/libi2pd_client/I2PService.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2025, The PurpleI2P Project +* Copyright (c) 2013-2026, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -32,8 +32,7 @@ namespace client I2PService::I2PService (i2p::data::SigningKeyType kt): m_LocalDestination (i2p::client::context.CreateNewLocalDestination (false, kt)), m_ReadyTimer(m_LocalDestination->GetService()), - m_ConnectTimeout(0), - isUpdated (true) + m_ConnectTimeout(0), isUpdated (true) { m_LocalDestination->Acquire (); } @@ -54,15 +53,15 @@ namespace client m_Handlers.clear(); } - void I2PService::SetConnectTimeout(uint32_t timeout) + void I2PService::SetConnectTimeout(uint64_t timeout) { m_ConnectTimeout = timeout; } void I2PService::AddReadyCallback(ReadyCallback cb) { - uint32_t now = i2p::util::GetSecondsSinceEpoch(); - uint32_t tm = (m_ConnectTimeout) ? now + m_ConnectTimeout : NEVER_TIMES_OUT; + uint64_t now = i2p::util::GetMonotonicSeconds (); + uint64_t tm = (m_ConnectTimeout) ? now + m_ConnectTimeout : NEVER_TIMES_OUT; LogPrint(eLogDebug, "I2PService::AddReadyCallback() ", tm, " ", now); m_ReadyCallbacks.push_back({cb, tm}); @@ -71,24 +70,24 @@ namespace client void I2PService::TriggerReadyCheckTimer() { - m_ReadyTimer.expires_from_now(boost::posix_time::seconds (1)); + m_ReadyTimer.expires_after(std::chrono::seconds (I2P_SERVICE_READINESS_CHECK_INTERVAL)); m_ReadyTimer.async_wait(std::bind(&I2PService::HandleReadyCheckTimer, shared_from_this (), std::placeholders::_1)); m_ReadyTimerTriggered = true; - } void I2PService::HandleReadyCheckTimer(const boost::system::error_code &ec) { - if(ec || m_LocalDestination->IsReady()) + bool isReady = (!ec) ? m_LocalDestination->IsReady() : false; + if(ec || isReady) { for(auto & itr : m_ReadyCallbacks) itr.first(ec); m_ReadyCallbacks.clear(); } - else if(!m_LocalDestination->IsReady()) + else if(!isReady) { // expire timed out requests - uint32_t now = i2p::util::GetSecondsSinceEpoch (); + uint64_t now = i2p::util::GetMonotonicSeconds (); auto itr = m_ReadyCallbacks.begin(); while(itr != m_ReadyCallbacks.end()) { diff --git a/libi2pd_client/I2PService.h b/libi2pd_client/I2PService.h index d19c28e2..328ae29b 100644 --- a/libi2pd_client/I2PService.h +++ b/libi2pd_client/I2PService.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2025, The PurpleI2P Project +* Copyright (c) 2013-2026, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -22,6 +22,8 @@ namespace i2p { namespace client { + constexpr int I2P_SERVICE_READINESS_CHECK_INTERVAL = 1; // in seconds + class I2PServiceHandler; class I2PService : public std::enable_shared_from_this { @@ -47,7 +49,7 @@ namespace client } void ClearHandlers (); - void SetConnectTimeout(uint32_t timeout); + void SetConnectTimeout(uint64_t timeout); void AddReadyCallback(ReadyCallback cb); @@ -78,10 +80,10 @@ namespace client std::shared_ptr m_LocalDestination; std::unordered_set > m_Handlers; std::mutex m_HandlersMutex; - std::vector > m_ReadyCallbacks; - boost::asio::deadline_timer m_ReadyTimer; + std::vector > m_ReadyCallbacks; + boost::asio::steady_timer m_ReadyTimer; bool m_ReadyTimerTriggered; - uint32_t m_ConnectTimeout; + uint64_t m_ConnectTimeout; const size_t NEVER_TIMES_OUT = 0; @@ -99,7 +101,7 @@ namespace client virtual ~I2PServiceHandler() { } //If you override this make sure you call it from the children virtual void Handle() {}; //Start handling the socket - virtual void Start () {}; + virtual void Start () {}; void Terminate () { Kill (); }; @@ -124,7 +126,7 @@ namespace client // bidirectional pipe for 2 stream sockets template - class SocketsPipe: public I2PServiceHandler, + class SocketsPipe: public I2PServiceHandler, public std::enable_shared_from_this > { public: @@ -135,14 +137,14 @@ namespace client boost::asio::socket_base::receive_buffer_size option(SOCKETS_PIPE_BUFFER_SIZE); upstream->set_option(option); downstream->set_option(option); - } + } ~SocketsPipe() { Terminate(); } - + void Start() override { Transfer (m_up, m_down, m_upstream_to_down_buf, SOCKETS_PIPE_BUFFER_SIZE); // receive from upstream Transfer (m_down, m_up, m_downstream_to_up_buf, SOCKETS_PIPE_BUFFER_SIZE); // receive from upstream - } + } private: @@ -163,7 +165,7 @@ namespace client } Done(SocketsPipe::shared_from_this()); } - + template void Transfer (std::shared_ptr from, std::shared_ptr to, uint8_t * buf, size_t len) { @@ -186,20 +188,20 @@ namespace client { LogPrint(eLogWarning, "SocketsPipe: Write error:" , ecode.message()); s->Terminate(); - } - }); - } + } + }); + } else { LogPrint(eLogWarning, "SocketsPipe: Read error:" , ecode.message()); s->Terminate(); - } - }); + } + }); } - + private: - uint8_t m_upstream_to_down_buf[SOCKETS_PIPE_BUFFER_SIZE], m_downstream_to_up_buf[SOCKETS_PIPE_BUFFER_SIZE]; + uint8_t m_upstream_to_down_buf[SOCKETS_PIPE_BUFFER_SIZE], m_downstream_to_up_buf[SOCKETS_PIPE_BUFFER_SIZE]; std::shared_ptr m_up; std::shared_ptr m_down; }; @@ -208,8 +210,8 @@ namespace client std::shared_ptr CreateSocketsPipe (I2PService * owner, std::shared_ptr upstream, std::shared_ptr downstream) { return std::make_shared >(owner, upstream, downstream); - } - + } + //This is a service that listens for connections on the IP network or local socket and interacts with I2P template class ServiceAcceptor: public I2PService @@ -218,7 +220,7 @@ namespace client ServiceAcceptor (const typename Protocol::endpoint& localEndpoint, std::shared_ptr localDestination = nullptr) : I2PService(localDestination), m_LocalEndpoint (localEndpoint) {} - + virtual ~ServiceAcceptor () { Stop(); } void Start () override { @@ -227,9 +229,9 @@ namespace client m_LocalEndpoint = m_Acceptor->local_endpoint(); m_Acceptor->listen (); Accept (); - } + } void Stop () override - { + { if (m_Acceptor) { m_Acceptor->close(); @@ -266,14 +268,14 @@ namespace client else newSocket->close(); Accept(); - } + } else LogPrint (eLogError, "ServiceAcceptor: ", GetName(), " closing socket on accept because: ", ecode.message ()); - }); - } - + }); + } + private: - + typename Protocol::endpoint m_LocalEndpoint; std::unique_ptr m_Acceptor; }; @@ -284,7 +286,7 @@ namespace client TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr localDestination = nullptr) : ServiceAcceptor (boost::asio::ip::tcp::endpoint (boost::asio::ip::make_address(address), port), localDestination) {} - }; + }; } }