From 80f81685d10146350ef47557859fb38ab3c206fe Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 13 Feb 2016 23:02:58 -0500 Subject: [PATCH] use rtt for ack timeout --- Destination.cpp | 28 +++++++++++++++++++++------- Garlic.cpp | 7 ++++++- Garlic.h | 2 ++ Streaming.cpp | 4 ++-- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Destination.cpp b/Destination.cpp index 8e13db42..26330c29 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -505,18 +505,24 @@ namespace client } } - void ClientDestination::CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port) { - assert(streamRequestComplete); + void ClientDestination::CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port) + { + if (!streamRequestComplete) + { + LogPrint (eLogError, "Destination: request callback is not specified in CreateStream"); + return; + } auto leaseSet = FindLeaseSet (dest); if (leaseSet) streamRequestComplete(CreateStream (leaseSet, port)); else { + auto s = shared_from_this (); RequestDestination (dest, - [this, streamRequestComplete, port](std::shared_ptr ls) + [s, streamRequestComplete, port](std::shared_ptr ls) { if (ls) - streamRequestComplete(CreateStream (ls, port)); + streamRequestComplete(s->CreateStream (ls, port)); else streamRequestComplete (nullptr); }); @@ -597,7 +603,11 @@ namespace client { auto it = s->m_LeaseSetRequests.find (dest); if (it != s->m_LeaseSetRequests.end ()) - s->m_LeaseSetRequests.erase (it); + { + auto requestComplete = it->second->requestComplete; + s->m_LeaseSetRequests.erase (it); + if (requestComplete) requestComplete (nullptr); + } }); } @@ -615,8 +625,8 @@ namespace client if (!SendLeaseSetRequest (dest, floodfill, request)) { // request failed - if (request->requestComplete) request->requestComplete (nullptr); m_LeaseSetRequests.erase (dest); + if (request->requestComplete) request->requestComplete (nullptr); } } else // duplicate @@ -627,7 +637,10 @@ namespace client } } else + { LogPrint (eLogError, "Destination: Can't request LeaseSet, no floodfills found"); + if (requestComplete) requestComplete (nullptr); + } } bool ClientDestination::SendLeaseSetRequest (const i2p::data::IdentHash& dest, @@ -695,8 +708,9 @@ namespace client if (done) { - if (it->second->requestComplete) it->second->requestComplete (nullptr); + auto requestComplete = it->second->requestComplete; m_LeaseSetRequests.erase (it); + if (requestComplete) requestComplete (nullptr); } } } diff --git a/Garlic.cpp b/Garlic.cpp index f967a1e2..e11f8ec8 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -47,17 +47,22 @@ namespace garlic { if (!m_SharedRoutingPath) return nullptr; uint32_t ts = i2p::util::GetSecondsSinceEpoch (); - if (!m_SharedRoutingPath->outboundTunnel->IsEstablished () || + if (m_SharedRoutingPath->numTimesUsed >= ROUTING_PATH_MAX_NUM_TIMES_USED || + !m_SharedRoutingPath->outboundTunnel->IsEstablished () || ts*1000LL > m_SharedRoutingPath->remoteLease->endDate || ts > m_SharedRoutingPath->updateTime + ROUTING_PATH_EXPIRATION_TIMEOUT) m_SharedRoutingPath = nullptr; + if (m_SharedRoutingPath) m_SharedRoutingPath->numTimesUsed++; return m_SharedRoutingPath; } void GarlicRoutingSession::SetSharedRoutingPath (std::shared_ptr path) { if (path && path->outboundTunnel && path->remoteLease) + { path->updateTime = i2p::util::GetSecondsSinceEpoch (); + path->numTimesUsed = 0; + } else path = nullptr; m_SharedRoutingPath = path; diff --git a/Garlic.h b/Garlic.h index 1d8aa8cf..68041e1d 100644 --- a/Garlic.h +++ b/Garlic.h @@ -44,6 +44,7 @@ namespace garlic const int OUTGOING_TAGS_CONFIRMATION_TIMEOUT = 10; // 10 seconds const int LEASET_CONFIRMATION_TIMEOUT = 4000; // in milliseconds const int ROUTING_PATH_EXPIRATION_TIMEOUT = 30; // 30 seconds + const int ROUTING_PATH_MAX_NUM_TIMES_USED = 10; // how many times might be used struct SessionTag: public i2p::data::Tag<32> { @@ -64,6 +65,7 @@ namespace garlic std::shared_ptr remoteLease; int rtt; // RTT uint32_t updateTime; // seconds since epoch + int numTimesUsed; }; class GarlicDestination; diff --git a/Streaming.cpp b/Streaming.cpp index 79f202be..ab6c6879 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -113,7 +113,7 @@ namespace stream if (!m_IsAckSendScheduled) { m_IsAckSendScheduled = true; - m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(ACK_SEND_TIMEOUT)); + m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(m_RTT/10)); m_AckSendTimer.async_wait (std::bind (&Stream::HandleAckSendTimer, shared_from_this (), std::placeholders::_1)); } @@ -274,7 +274,7 @@ namespace stream if (!seqn && m_RoutingSession) // first message confirmed m_RoutingSession->SetSharedRoutingPath ( std::make_shared ( - i2p::garlic::GarlicRoutingPath{m_CurrentOutboundTunnel, m_CurrentRemoteLease, m_RTT, 0})); + i2p::garlic::GarlicRoutingPath{m_CurrentOutboundTunnel, m_CurrentRemoteLease, m_RTT, 0, 0})); } else break;