Merge remote-tracking branch 'origin/master' into ipv6-tun

This commit is contained in:
Jeff 2019-07-05 08:33:13 -04:00
commit 0c5ee8a5a5

View File

@ -38,56 +38,68 @@ namespace llarp
ILinkLayer::ForEachSession(std::function< void(const ILinkSession*) > visit, ILinkLayer::ForEachSession(std::function< void(const ILinkSession*) > visit,
bool randomize) const bool randomize) const
{ {
Lock l(&m_AuthedLinksMutex); std::vector< std::shared_ptr< ILinkSession > > sessions;
if(m_AuthedLinks.size() == 0)
return;
const size_t sz = randint() % m_AuthedLinks.size();
auto itr = m_AuthedLinks.begin();
auto begin = itr;
if(randomize)
{ {
std::advance(itr, sz); Lock l(&m_AuthedLinksMutex);
begin = itr; if(m_AuthedLinks.size() == 0)
} return;
while(itr != m_AuthedLinks.end()) const size_t sz = randint() % m_AuthedLinks.size();
{ auto itr = m_AuthedLinks.begin();
visit(itr->second.get()); auto begin = itr;
++itr; if(randomize)
}
if(randomize)
{
itr = m_AuthedLinks.begin();
while(itr != begin)
{ {
visit(itr->second.get()); std::advance(itr, sz);
begin = itr;
}
while(itr != m_AuthedLinks.end())
{
sessions.emplace_back(itr->second);
++itr; ++itr;
} }
if(randomize)
{
itr = m_AuthedLinks.begin();
while(itr != begin)
{
sessions.emplace_back(itr->second);
++itr;
}
}
} }
for(const auto& session : sessions)
visit(session.get());
} }
bool bool
ILinkLayer::VisitSessionByPubkey(const RouterID& pk, ILinkLayer::VisitSessionByPubkey(const RouterID& pk,
std::function< bool(ILinkSession*) > visit) std::function< bool(ILinkSession*) > visit)
{ {
Lock l(&m_AuthedLinksMutex); std::shared_ptr< ILinkSession > session;
auto itr = m_AuthedLinks.find(pk);
if(itr != m_AuthedLinks.end())
{ {
return visit(itr->second.get()); Lock l(&m_AuthedLinksMutex);
auto itr = m_AuthedLinks.find(pk);
if(itr == m_AuthedLinks.end())
return false;
session = itr->second;
} }
return false; return visit(session.get());
} }
void void
ILinkLayer::ForEachSession(std::function< void(ILinkSession*) > visit) ILinkLayer::ForEachSession(std::function< void(ILinkSession*) > visit)
{ {
Lock l(&m_AuthedLinksMutex); std::vector< std::shared_ptr< ILinkSession > > sessions;
auto itr = m_AuthedLinks.begin();
while(itr != m_AuthedLinks.end())
{ {
visit(itr->second.get()); Lock l(&m_AuthedLinksMutex);
++itr; auto itr = m_AuthedLinks.begin();
while(itr != m_AuthedLinks.end())
{
sessions.emplace_back(itr->second);
++itr;
}
} }
for(const auto& s : sessions)
visit(s.get());
} }
bool bool