Browse Source

fixes #1124 and calls the ready callbacks if the tunnel gets ready and no timeout is set

pull/1190/head
Veggie Monster 7 years ago
parent
commit
46283dc0ea
  1. 21
      libi2pd_client/I2PService.cpp
  2. 3
      libi2pd_client/I2PService.h

21
libi2pd_client/I2PService.cpp

@ -14,6 +14,7 @@ namespace client
m_LocalDestination (localDestination ? localDestination : m_LocalDestination (localDestination ? localDestination :
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)), i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)),
m_ReadyTimer(m_LocalDestination->GetService()), m_ReadyTimer(m_LocalDestination->GetService()),
m_ReadyTimerTriggered(false),
m_ConnectTimeout(0), m_ConnectTimeout(0),
isUpdated (true) isUpdated (true)
{ {
@ -47,29 +48,25 @@ namespace client
void I2PService::SetConnectTimeout(uint32_t timeout) void I2PService::SetConnectTimeout(uint32_t timeout)
{ {
if(timeout && !m_ConnectTimeout)
{
TriggerReadyCheckTimer();
}
else if (m_ConnectTimeout && !timeout)
{
m_ReadyTimer.cancel();
}
m_ConnectTimeout = timeout; m_ConnectTimeout = timeout;
} }
void I2PService::AddReadyCallback(ReadyCallback cb) void I2PService::AddReadyCallback(ReadyCallback cb)
{ {
uint32_t now = i2p::util::GetSecondsSinceEpoch(); uint32_t now = i2p::util::GetSecondsSinceEpoch();
uint32_t tm = now + m_ConnectTimeout; uint32_t tm = (m_ConnectTimeout) ? now + m_ConnectTimeout : NEVER_TIMES_OUT;
LogPrint(eLogDebug, "I2PService::AddReadyCallback() ", tm, " ", now); LogPrint(eLogDebug, "I2PService::AddReadyCallback() ", tm, " ", now);
m_ReadyCallbacks.push_back({cb, tm}); m_ReadyCallbacks.push_back({cb, tm});
if (!m_ReadyTimerTriggered) TriggerReadyCheckTimer();
} }
void I2PService::TriggerReadyCheckTimer() void I2PService::TriggerReadyCheckTimer()
{ {
m_ReadyTimer.expires_from_now(boost::posix_time::seconds (1)); m_ReadyTimer.expires_from_now(boost::posix_time::seconds (1));
m_ReadyTimer.async_wait(std::bind(&I2PService::HandleReadyCheckTimer, this, std::placeholders::_1)); m_ReadyTimer.async_wait(std::bind(&I2PService::HandleReadyCheckTimer, this, std::placeholders::_1));
m_ReadyTimerTriggered = true;
} }
void I2PService::HandleReadyCheckTimer(const boost::system::error_code &ec) void I2PService::HandleReadyCheckTimer(const boost::system::error_code &ec)
@ -87,7 +84,7 @@ namespace client
auto itr = m_ReadyCallbacks.begin(); auto itr = m_ReadyCallbacks.begin();
while(itr != m_ReadyCallbacks.end()) while(itr != m_ReadyCallbacks.end())
{ {
if(itr->second >= now) if(itr->second != NEVER_TIMES_OUT && now >= itr->second)
{ {
itr->first(boost::asio::error::timed_out); itr->first(boost::asio::error::timed_out);
itr = m_ReadyCallbacks.erase(itr); itr = m_ReadyCallbacks.erase(itr);
@ -96,8 +93,10 @@ namespace client
++itr; ++itr;
} }
} }
if(!ec) if(!ec && m_ReadyCallbacks.size())
TriggerReadyCheckTimer(); TriggerReadyCheckTimer();
else
m_ReadyTimerTriggered = false;
} }
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) { void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) {

3
libi2pd_client/I2PService.h

@ -67,8 +67,11 @@ namespace client
std::mutex m_HandlersMutex; std::mutex m_HandlersMutex;
std::vector<std::pair<ReadyCallback, uint32_t> > m_ReadyCallbacks; std::vector<std::pair<ReadyCallback, uint32_t> > m_ReadyCallbacks;
boost::asio::deadline_timer m_ReadyTimer; boost::asio::deadline_timer m_ReadyTimer;
bool m_ReadyTimerTriggered;
uint32_t m_ConnectTimeout; uint32_t m_ConnectTimeout;
const size_t NEVER_TIMES_OUT = 0;
public: public:
bool isUpdated; // transient, used during reload only bool isUpdated; // transient, used during reload only
}; };

Loading…
Cancel
Save