1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

only have llarp::path::PathSet inherit std::enable_shared_from_this

many subtypes use std::enable_shared_from_this when only PathSet needs
to. we make only PathSet inherit this and use std::static_pointer_cast
for any subtypes that need to.
This commit is contained in:
Jeff Becker 2023-04-18 10:11:46 -04:00
parent eca944c427
commit b8b60a4d97
No known key found for this signature in database
GPG key ID: 025C02EE3A092F2D
15 changed files with 42 additions and 90 deletions

View file

@ -7,6 +7,7 @@
#include <llarp/quic/tunnel.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <llarp/util/meta/memfn.hpp>
#include <memory>
#include <utility>
namespace llarp
@ -142,7 +143,8 @@ namespace llarp
if (success)
{
auto self = shared_from_this();
auto self = std::static_pointer_cast<BaseSession>(shared_from_this());
for (auto& f : m_PendingCallbacks)
f(self);
}
@ -189,7 +191,6 @@ namespace llarp
}
};
ForEachPath(sendExitClose);
m_router->pathContext().RemovePathSet(shared_from_this());
return path::Builder::Stop();
}

View file

@ -30,8 +30,7 @@ namespace llarp
static constexpr auto LifeSpan = path::default_lifetime;
/// a persisting exit session with an exit router
struct BaseSession : public llarp::path::Builder,
public std::enable_shared_from_this<BaseSession>
struct BaseSession : public llarp::path::Builder
{
static constexpr size_t MaxUpstreamQueueLength = 256;
@ -45,18 +44,6 @@ namespace llarp
~BaseSession() override;
std::shared_ptr<path::PathSet>
GetSelf() override
{
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
void
BlacklistSNode(const RouterID snode) override;

View file

@ -161,12 +161,6 @@ namespace llarp
return m_OwnedRanges;
}
llarp_time_t
PathAlignmentTimeout() const override
{
return m_PathAlignmentTimeout;
}
/// ip packet against any exit policies we have
/// returns false if this traffic is disallowed by any of those policies
/// returns true otherwise

View file

@ -27,7 +27,7 @@ namespace llarp
{
Path::Path(
const std::vector<RouterContact>& h,
std::weak_ptr<PathSet> pathset,
const std::shared_ptr<PathSet>& pathset,
PathRole startingRoles,
std::string shortName)
: m_PathSet{std::move(pathset)}, _role{startingRoles}, m_shortName{std::move(shortName)}
@ -56,8 +56,8 @@ namespace llarp
// initialize parts of the introduction
intro.router = hops[hsz - 1].rc.pubkey;
intro.pathID = hops[hsz - 1].txID;
if (auto parent = m_PathSet.lock())
EnterState(ePathBuilding, parent->Now());
EnterState(ePathBuilding, pathset->Now());
}
void

View file

@ -95,7 +95,7 @@ namespace llarp
Path(
const std::vector<RouterContact>& routers,
std::weak_ptr<PathSet> parent,
const std::shared_ptr<PathSet>& parent,
PathRole startingRoles,
std::string shortName);

View file

@ -421,10 +421,6 @@ namespace llarp
return nullptr;
}
void
PathContext::RemovePathSet(PathSet_ptr)
{}
void
PathContext::periodic_tick()
{

View file

@ -111,9 +111,6 @@ namespace llarp
void
AddOwnPath(PathSet_ptr set, Path_ptr p);
void
RemovePathSet(PathSet_ptr set);
using TransitHopsMap_t = std::unordered_multimap<PathID_t, TransitHop_ptr>;
struct SyncTransitMap_t

View file

@ -14,6 +14,7 @@
#include <llarp/link/link_manager.hpp>
#include <functional>
#include <memory>
namespace llarp
{
@ -448,14 +449,17 @@ namespace llarp
LogWarn(Name(), " building too fast to edge router ", edge);
return;
}
LogTrace("build one aligning to ", RouterID{hops.back().pubkey});
// async generate keys
auto ctx = std::make_shared<AsyncPathKeyExchangeContext>();
ctx->router = m_router;
auto self = GetSelf();
auto self = shared_from_this();
ctx->pathset = self;
std::string path_shortName = "[path " + m_router->ShortName() + "-";
path_shortName = path_shortName + std::to_string(m_router->NextPathBuildNumber()) + "]";
auto path = std::make_shared<path::Path>(hops, GetWeak(), roles, std::move(path_shortName));
LogTrace("make ", path_shortName);
auto path = std::make_shared<path::Path>(hops, self, roles, std::move(path_shortName));
LogInfo(Name(), " build ", path->ShortName(), ": ", path->HopsString());
path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); });

View file

@ -5,6 +5,7 @@
#include <llarp/util/decaying_hashset.hpp>
#include <atomic>
#include <memory>
#include <set>
namespace llarp

View file

@ -5,6 +5,7 @@
#include <llarp/routing/dht_message.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <memory>
#include <random>
namespace llarp
@ -67,7 +68,7 @@ namespace llarp
{
const auto now = llarp::time_now_ms();
Lock_t l{m_PathsMutex};
for (auto& item : m_Paths)
for (const auto& item : m_Paths)
{
item.second->Tick(now, r);
}
@ -77,17 +78,20 @@ namespace llarp
PathSet::Tick(llarp_time_t)
{
std::unordered_set<RouterID> endpoints;
for (auto& item : m_Paths)
for (const auto& item : m_Paths)
{
endpoints.emplace(item.second->Endpoint());
}
if (endpoints.empty())
return;
m_PathCache.clear();
for (const auto& ep : endpoints)
{
if (auto path = GetPathByRouter(ep))
{
m_PathCache[ep] = path->weak_from_this();
m_PathCache.try_emplace(ep, std::weak_ptr{path});
}
}
}
@ -112,6 +116,7 @@ namespace llarp
else
++itr;
}
m_PathCache.clear();
}
Path_ptr
@ -173,7 +178,8 @@ namespace llarp
{
if (auto itr = m_PathCache.find(id); itr != m_PathCache.end())
{
return itr->second.lock();
if (auto ptr = itr->second.lock())
chosen = ptr;
}
}
auto itr = m_Paths.begin();
@ -192,6 +198,7 @@ namespace llarp
}
++itr;
}
m_PathCache[id] = chosen;
return chosen;
}

View file

@ -12,6 +12,7 @@
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <tuple>
#include <unordered_set>
@ -99,7 +100,7 @@ namespace llarp
using PathSet_ptr = std::shared_ptr<PathSet>;
/// a set of paths owned by an entity
struct PathSet
struct PathSet : std::enable_shared_from_this<PathSet>
{
/// maximum number of paths a path set can maintain
static constexpr size_t max_paths = 32;
@ -107,14 +108,6 @@ namespace llarp
/// @params numDesiredPaths the number of paths to maintain
PathSet(size_t numDesiredPaths);
/// get a shared_ptr of ourself
virtual PathSet_ptr
GetSelf() = 0;
/// get a weak_ptr of ourself
virtual std::weak_ptr<PathSet>
GetWeak() = 0;
virtual void
BuildOne(PathRole roles = ePathRoleAny) = 0;
@ -319,7 +312,7 @@ namespace llarp
PathMap_t m_Paths;
private:
std::unordered_map<RouterID, std::weak_ptr<path::Path>> m_PathCache;
mutable std::unordered_map<RouterID, std::weak_ptr<path::Path>> m_PathCache;
};
} // namespace path

View file

@ -74,18 +74,6 @@ namespace llarp
m_quic = std::make_unique<quic::TunnelManager>(*this);
}
path::PathSet_ptr
Endpoint::GetSelf()
{
return std::static_pointer_cast<path::PathSet>(shared_from_this());
}
std::weak_ptr<path::PathSet>
Endpoint::GetWeak()
{
return std::weak_ptr<path::PathSet>{weak_from_this()};
}
std::string_view
Endpoint::endpoint_name() const
{
@ -258,7 +246,7 @@ namespace llarp
LookupNameAsync(
name,
[ptr = std::static_pointer_cast<Endpoint>(GetSelf()),
[ptr = std::static_pointer_cast<Endpoint>(shared_from_this()),
name,
auth = AuthInfo{token},
ranges,

View file

@ -66,8 +66,7 @@ namespace llarp
struct Endpoint : public path::Builder,
public ILookupHolder,
public IDataHandler,
public EndpointBase,
private std::enable_shared_from_this<Endpoint>
public EndpointBase
{
explicit Endpoint(AbstractRouter& r);
~Endpoint() override;
@ -330,7 +329,7 @@ namespace llarp
bool
ShouldBuildMore(llarp_time_t now) const override;
virtual llarp_time_t
constexpr llarp_time_t
PathAlignmentTimeout() const
{
constexpr auto DefaultPathAlignmentTimeout = 10s;
@ -444,12 +443,6 @@ namespace llarp
virtual void
IntroSetPublished();
path::PathSet_ptr
GetSelf() override;
std::weak_ptr<path::PathSet>
GetWeak() override;
void
AsyncProcessAuthMessage(
std::shared_ptr<ProtocolMessage> msg, std::function<void(AuthResult)> hook);

View file

@ -10,6 +10,7 @@
#include <llarp/profiling.hpp>
#include <llarp/util/meta/memfn.hpp>
#include <memory>
#include <random>
#include <algorithm>
@ -200,12 +201,13 @@ namespace llarp
path::Builder::HandlePathBuilt(p);
p->SetDataHandler([self = weak_from_this()](auto path, auto frame) {
if (auto ptr = self.lock())
return ptr->HandleHiddenServiceFrame(path, frame);
return std::static_pointer_cast<OutboundContext>(ptr)->HandleHiddenServiceFrame(
path, frame);
return false;
});
p->SetDropHandler([self = weak_from_this()](auto path, auto id, auto seqno) {
if (auto ptr = self.lock())
return ptr->HandleDataDrop(path, id, seqno);
return std::static_pointer_cast<OutboundContext>(ptr)->HandleDataDrop(path, id, seqno);
return false;
});
if (markedBad)
@ -253,7 +255,8 @@ namespace llarp
currentConvoTag,
t);
ex->hook = [self = shared_from_this(), path](auto frame) {
ex->hook = [self = std::static_pointer_cast<OutboundContext>(shared_from_this()),
path](auto frame) {
if (not self->Send(std::move(frame), path))
return;
self->m_Endpoint->Loop()->call_later(
@ -296,7 +299,9 @@ namespace llarp
{
HiddenServiceAddressLookup* job = new HiddenServiceAddressLookup(
m_Endpoint,
util::memFn(&OutboundContext::OnIntroSetUpdate, shared_from_this()),
util::memFn(
&OutboundContext::OnIntroSetUpdate,
std::static_pointer_cast<OutboundContext>(shared_from_this())),
location,
PubKey{addr.as_array()},
path->Endpoint(),

View file

@ -15,9 +15,7 @@ namespace llarp
struct Endpoint;
/// context needed to initiate an outbound hidden service session
struct OutboundContext : public path::Builder,
public SendContext,
public std::enable_shared_from_this<OutboundContext>
struct OutboundContext : public path::Builder, public SendContext
{
OutboundContext(const IntroSet& introSet, Endpoint* parent);
@ -35,18 +33,6 @@ namespace llarp
bool
ShouldBundleRC() const override;
path::PathSet_ptr
GetSelf() override
{
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
Address
Addr() const;