replace deadline_timer by steady_timer

This commit is contained in:
orignal
2026-03-05 21:11:40 -05:00
parent 33e7ffaa16
commit a3cafd3cfd
2 changed files with 41 additions and 40 deletions
+10 -11
View File
@@ -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())
{
+31 -29
View File
@@ -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<I2PService>
{
@@ -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<ClientDestination> m_LocalDestination;
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
std::mutex m_HandlersMutex;
std::vector<std::pair<ReadyCallback, uint32_t> > m_ReadyCallbacks;
boost::asio::deadline_timer m_ReadyTimer;
std::vector<std::pair<ReadyCallback, uint64_t> > 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<typename SocketUpstream, typename SocketDownstream>
class SocketsPipe: public I2PServiceHandler,
class SocketsPipe: public I2PServiceHandler,
public std::enable_shared_from_this<SocketsPipe<SocketUpstream, SocketDownstream> >
{
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<SocketUpstream, SocketDownstream>::shared_from_this());
}
template<typename From, typename To>
void Transfer (std::shared_ptr<From> from, std::shared_ptr<To> 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<SocketUpstream> m_up;
std::shared_ptr<SocketDownstream> m_down;
};
@@ -208,8 +210,8 @@ namespace client
std::shared_ptr<I2PServiceHandler> CreateSocketsPipe (I2PService * owner, std::shared_ptr<SocketUpstream> upstream, std::shared_ptr<SocketDownstream> downstream)
{
return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream);
}
}
//This is a service that listens for connections on the IP network or local socket and interacts with I2P
template<typename Protocol>
class ServiceAcceptor: public I2PService
@@ -218,7 +220,7 @@ namespace client
ServiceAcceptor (const typename Protocol::endpoint& localEndpoint, std::shared_ptr<ClientDestination> 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<typename Protocol::acceptor> m_Acceptor;
};
@@ -284,7 +286,7 @@ namespace client
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
ServiceAcceptor (boost::asio::ip::tcp::endpoint (boost::asio::ip::make_address(address), port), localDestination) {}
};
};
}
}