mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-13 08:17:38 +01:00
more precise router selection
This commit is contained in:
parent
99c7d5c23a
commit
a6937c792f
1 changed files with 26 additions and 16 deletions
|
@ -1223,24 +1223,34 @@ namespace data
|
||||||
{
|
{
|
||||||
if (m_RouterInfos.empty())
|
if (m_RouterInfos.empty())
|
||||||
return 0;
|
return 0;
|
||||||
uint32_t ind = rand () % m_RouterInfos.size ();
|
|
||||||
for (int j = 0; j < 2; j++)
|
|
||||||
{
|
|
||||||
uint32_t i = 0;
|
|
||||||
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
||||||
for (const auto& it: m_RouterInfos)
|
auto ind = rand () % m_RouterInfos.size ();
|
||||||
|
auto it = m_RouterInfos.begin ();
|
||||||
|
std::advance (it, ind);
|
||||||
|
// try random router
|
||||||
|
if (it != m_RouterInfos.end () && !it->second->IsUnreachable () && filter (it->second))
|
||||||
|
return it->second;
|
||||||
|
// try closest routers
|
||||||
|
auto it1 = it; it1++;
|
||||||
|
// forward
|
||||||
|
while (it1 != m_RouterInfos.end ())
|
||||||
{
|
{
|
||||||
if (i >= ind)
|
if (!it1->second->IsUnreachable () && filter (it1->second))
|
||||||
|
return it1->second;
|
||||||
|
it1++;
|
||||||
|
}
|
||||||
|
// still not found, try from the beginning
|
||||||
|
if (ind)
|
||||||
{
|
{
|
||||||
if (!it.second->IsUnreachable () && filter (it.second))
|
it1 = m_RouterInfos.begin ();
|
||||||
return it.second;
|
while (it1 != it || it1 != m_RouterInfos.end ())
|
||||||
|
{
|
||||||
|
if (!it1->second->IsUnreachable () && filter (it1->second))
|
||||||
|
return it1->second;
|
||||||
|
it1++;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
// we couldn't find anything, try second pass
|
|
||||||
ind = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr; // seems we have too few routers
|
return nullptr; // seems we have too few routers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue