lokinet/llarp/service/endpoint_util.cpp

192 lines
4.4 KiB
C++
Raw Normal View History

#include "endpoint_util.hpp"
#include <llarp/exit/session.hpp>
#include "outbound_context.hpp"
#include "lookup.hpp"
#include <llarp/util/logging/logger.hpp>
namespace llarp
{
namespace service
{
void
EndpointUtil::ExpireSNodeSessions(llarp_time_t now, SNodeSessions& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
2021-04-07 12:03:04 +00:00
if (itr->second->ShouldRemove() && itr->second->IsStopped())
{
itr = sessions.erase(itr);
continue;
}
// expunge next tick
2021-04-07 12:03:04 +00:00
if (itr->second->IsExpired(now))
{
2021-04-07 12:03:04 +00:00
itr->second->Stop();
}
2019-04-30 21:36:27 +00:00
else
{
2021-04-07 12:03:04 +00:00
itr->second->Tick(now);
2019-04-30 21:36:27 +00:00
}
++itr;
}
}
void
EndpointUtil::ExpirePendingTx(llarp_time_t now, PendingLookups& lookups)
{
std::vector<std::unique_ptr<IServiceLookup>> timedout;
for (auto itr = lookups.begin(); itr != lookups.end();)
{
if (!itr->second->IsTimedOut(now))
{
++itr;
continue;
}
timedout.emplace_back(std::move(itr->second));
itr = lookups.erase(itr);
}
for (const auto& lookup : timedout)
{
2019-05-05 14:15:48 +00:00
LogWarn(lookup->name, " timed out txid=", lookup->txid);
lookup->HandleTimeout();
}
}
void
EndpointUtil::ExpirePendingRouterLookups(llarp_time_t now, PendingRouters& routers)
{
for (auto itr = routers.begin(); itr != routers.end();)
{
if (!itr->second.IsExpired(now))
{
++itr;
continue;
}
2019-05-05 14:15:48 +00:00
LogWarn("lookup for ", itr->first, " timed out");
2019-05-05 14:42:13 +00:00
itr->second.InformResult({});
itr = routers.erase(itr);
}
}
void
EndpointUtil::DeregisterDeadSessions(llarp_time_t now, Sessions& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second->IsDone(now))
{
itr = sessions.erase(itr);
}
else
{
++itr;
}
}
}
void
EndpointUtil::TickRemoteSessions(
llarp_time_t now, Sessions& remoteSessions, Sessions& deadSessions, ConvoMap& sessions)
{
auto itr = remoteSessions.begin();
while (itr != remoteSessions.end())
{
2019-04-30 16:49:34 +00:00
itr->second->Tick(now);
if (itr->second->Pump(now))
{
LogInfo(
"marking session as dead T=",
itr->second->currentConvoTag,
" to ",
itr->second->Addr());
itr->second->Stop();
sessions.erase(itr->second->currentConvoTag);
deadSessions.emplace(std::move(*itr));
itr = remoteSessions.erase(itr);
}
else
{
++itr;
}
}
for (auto& item : deadSessions)
{
item.second->Tick(now);
}
}
void
EndpointUtil::ExpireConvoSessions(llarp_time_t now, ConvoMap& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second.IsExpired(now))
{
LogInfo("Expire session T=", itr->first, " to ", itr->second.Addr());
itr = sessions.erase(itr);
}
else
++itr;
}
}
void
EndpointUtil::StopRemoteSessions(Sessions& remoteSessions)
{
for (auto& item : remoteSessions)
{
item.second->Stop();
}
}
void
EndpointUtil::StopSnodeSessions(SNodeSessions& sessions)
{
for (auto& item : sessions)
{
2021-04-07 12:03:04 +00:00
item.second->Stop();
}
}
bool
EndpointUtil::HasPathToService(const Address& addr, const Sessions& remoteSessions)
{
auto range = remoteSessions.equal_range(addr);
auto itr = range.first;
while (itr != range.second)
{
if (itr->second->ReadyToSend())
return true;
++itr;
}
return false;
}
2019-05-07 08:29:47 +00:00
bool
EndpointUtil::GetConvoTagsForService(
const ConvoMap& sessions, const Address& info, std::set<ConvoTag>& tags)
2019-05-07 08:29:47 +00:00
{
bool inserted = false;
auto itr = sessions.begin();
while (itr != sessions.end())
2019-05-07 08:29:47 +00:00
{
if (itr->second.remote.Addr() == info)
2019-05-07 08:29:47 +00:00
{
if (tags.emplace(itr->first).second)
2019-05-07 08:29:47 +00:00
{
inserted = true;
}
}
++itr;
}
return inserted;
}
} // namespace service
} // namespace llarp