Browse Source

shared_ptr for NetdbRequests

pull/1941/merge
orignal 6 months ago
parent
commit
296b721929
  1. 40
      libi2pd/NetDb.cpp
  2. 3
      libi2pd/NetDb.hpp
  3. 6
      libi2pd/NetDbRequests.cpp
  4. 2
      libi2pd/NetDbRequests.h

40
libi2pd/NetDb.cpp

@ -80,12 +80,20 @@ namespace data
i2p::config::GetOption("persist.profiles", m_PersistProfiles); i2p::config::GetOption("persist.profiles", m_PersistProfiles);
if (!m_Requests)
{
m_Requests = std::make_shared<NetDbRequests>();
m_Requests->Start ();
}
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&NetDb::Run, this)); m_Thread = new std::thread (std::bind (&NetDb::Run, this));
} }
void NetDb::Stop () void NetDb::Stop ()
{ {
if (m_Requests)
m_Requests->Stop ();
if (m_IsRunning) if (m_IsRunning)
{ {
if (m_PersistProfiles) if (m_PersistProfiles)
@ -102,8 +110,8 @@ namespace data
m_Thread = 0; m_Thread = 0;
} }
m_LeaseSets.clear(); m_LeaseSets.clear();
m_Requests.Stop ();
} }
m_Requests = nullptr;
} }
void NetDb::Run () void NetDb::Run ()
@ -158,7 +166,7 @@ namespace data
{ {
if (lastManageRequest || i2p::tunnel::tunnels.GetExploratoryPool ()) // expolratory pool is ready? if (lastManageRequest || i2p::tunnel::tunnels.GetExploratoryPool ()) // expolratory pool is ready?
{ {
m_Requests.ManageRequests (); if (m_Requests) m_Requests->ManageRequests ();
lastManageRequest = mts; lastManageRequest = mts;
} }
} }
@ -274,7 +282,7 @@ namespace data
if (!r->Update (buf, len)) if (!r->Update (buf, len))
{ {
updated = false; updated = false;
m_Requests.RequestComplete (ident, r); m_Requests->RequestComplete (ident, r);
return r; return r;
} }
if (r->IsUnreachable () || if (r->IsUnreachable () ||
@ -287,7 +295,7 @@ namespace data
std::lock_guard<std::mutex> l(m_FloodfillsMutex); std::lock_guard<std::mutex> l(m_FloodfillsMutex);
m_Floodfills.Remove (r->GetIdentHash ()); m_Floodfills.Remove (r->GetIdentHash ());
} }
m_Requests.RequestComplete (ident, nullptr); m_Requests->RequestComplete (ident, nullptr);
return nullptr; return nullptr;
} }
} }
@ -359,7 +367,7 @@ namespace data
updated = false; updated = false;
} }
// take care about requested destination // take care about requested destination
m_Requests.RequestComplete (ident, r); m_Requests->RequestComplete (ident, r);
return r; return r;
} }
@ -777,7 +785,7 @@ namespace data
void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct) void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct)
{ {
if (direct && i2p::transport::transports.RoutesRestricted ()) direct = false; // always use tunnels for restricted routes if (direct && i2p::transport::transports.RoutesRestricted ()) direct = false; // always use tunnels for restricted routes
auto dest = m_Requests.CreateRequest (destination, false, direct, requestComplete); // non-exploratory auto dest = m_Requests->CreateRequest (destination, false, direct, requestComplete); // non-exploratory
if (!dest) if (!dest)
{ {
LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already or cached"); LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already or cached");
@ -795,7 +803,7 @@ namespace data
if (CheckLogLevel (eLogDebug)) if (CheckLogLevel (eLogDebug))
LogPrint (eLogDebug, "NetDb: Request ", dest->GetDestination ().ToBase64 (), " at ", floodfill->GetIdentHash ().ToBase64 (), " directly"); LogPrint (eLogDebug, "NetDb: Request ", dest->GetDestination ().ToBase64 (), " at ", floodfill->GetIdentHash ().ToBase64 (), " directly");
auto msg = dest->CreateRequestMessage (floodfill->GetIdentHash ()); auto msg = dest->CreateRequestMessage (floodfill->GetIdentHash ());
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests.SendNextRequest (dest); }; msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests->SendNextRequest (dest); };
transports.SendMessage (floodfill->GetIdentHash (), msg); transports.SendMessage (floodfill->GetIdentHash (), msg);
} }
else else
@ -808,28 +816,28 @@ namespace data
if (CheckLogLevel (eLogDebug)) if (CheckLogLevel (eLogDebug))
LogPrint (eLogDebug, "NetDb: Request ", dest->GetDestination ().ToBase64 (), " at ", floodfill->GetIdentHash ().ToBase64 (), " through tunnels"); LogPrint (eLogDebug, "NetDb: Request ", dest->GetDestination ().ToBase64 (), " at ", floodfill->GetIdentHash ().ToBase64 (), " through tunnels");
auto msg = dest->CreateRequestMessage (floodfill, inbound); auto msg = dest->CreateRequestMessage (floodfill, inbound);
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests.SendNextRequest (dest); }; msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests->SendNextRequest (dest); };
outbound->SendTunnelDataMsgTo (floodfill->GetIdentHash (), 0, outbound->SendTunnelDataMsgTo (floodfill->GetIdentHash (), 0,
i2p::garlic::WrapECIESX25519MessageForRouter (msg, floodfill->GetIdentity ()->GetEncryptionPublicKey ())); i2p::garlic::WrapECIESX25519MessageForRouter (msg, floodfill->GetIdentity ()->GetEncryptionPublicKey ()));
} }
else else
{ {
LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no tunnels found"); LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no tunnels found");
m_Requests.RequestComplete (destination, nullptr); m_Requests->RequestComplete (destination, nullptr);
} }
} }
} }
else else
{ {
LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no floodfills found"); LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no floodfills found");
m_Requests.RequestComplete (destination, nullptr); m_Requests->RequestComplete (destination, nullptr);
} }
} }
void NetDb::RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploratory, RequestedDestination::RequestComplete requestComplete) void NetDb::RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploratory, RequestedDestination::RequestComplete requestComplete)
{ {
auto dest = m_Requests.CreateRequest (destination, exploratory, true, requestComplete); // non-exploratory auto dest = m_Requests->CreateRequest (destination, exploratory, true, requestComplete); // non-exploratory
if (!dest) if (!dest)
{ {
LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already"); LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already");
@ -999,15 +1007,15 @@ namespace data
size_t num = buf[32]; // num size_t num = buf[32]; // num
LogPrint (eLogDebug, "NetDb: DatabaseSearchReply for ", key, " num=", num); LogPrint (eLogDebug, "NetDb: DatabaseSearchReply for ", key, " num=", num);
IdentHash ident (buf); IdentHash ident (buf);
auto dest = m_Requests.FindRequest (ident); auto dest = m_Requests->FindRequest (ident);
if (dest && dest->IsActive ()) if (dest && dest->IsActive ())
{ {
if (!dest->IsExploratory () && (num > 0 || dest->GetNumExcludedPeers () < 3)) // before 3-rd attempt might be just bad luck if (!dest->IsExploratory () && (num > 0 || dest->GetNumExcludedPeers () < 3)) // before 3-rd attempt might be just bad luck
// try to send next requests // try to send next requests
m_Requests.SendNextRequest (dest); m_Requests->SendNextRequest (dest);
else else
// no more requests for destination possible. delete it // no more requests for destination possible. delete it
m_Requests.RequestComplete (ident, nullptr); m_Requests->RequestComplete (ident, nullptr);
} }
else if (!m_FloodfillBootstrap) else if (!m_FloodfillBootstrap)
{ {
@ -1222,7 +1230,7 @@ namespace data
for (int i = 0; i < numDestinations; i++) for (int i = 0; i < numDestinations; i++)
{ {
RAND_bytes (randomHash, 32); RAND_bytes (randomHash, 32);
auto dest = m_Requests.CreateRequest (randomHash, true, !throughTunnels); // exploratory auto dest = m_Requests->CreateRequest (randomHash, true, !throughTunnels); // exploratory
if (!dest) if (!dest)
{ {
LogPrint (eLogWarning, "NetDb: Exploratory destination is requested already"); LogPrint (eLogWarning, "NetDb: Exploratory destination is requested already");
@ -1252,7 +1260,7 @@ namespace data
i2p::transport::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); i2p::transport::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
} }
else else
m_Requests.RequestComplete (randomHash, nullptr); m_Requests->RequestComplete (randomHash, nullptr);
} }
if (throughTunnels && msgs.size () > 0) if (throughTunnels && msgs.size () > 0)
outbound->SendTunnelDataMsgs (msgs); outbound->SendTunnelDataMsgs (msgs);

3
libi2pd/NetDb.hpp

@ -179,8 +179,7 @@ namespace data
Families m_Families; Families m_Families;
i2p::fs::HashedStorage m_Storage; i2p::fs::HashedStorage m_Storage;
friend class NetDbRequests; std::shared_ptr<NetDbRequests> m_Requests;
NetDbRequests m_Requests;
bool m_PersistProfiles; bool m_PersistProfiles;
std::future<void> m_SavingProfiles, m_DeletingProfiles; std::future<void> m_SavingProfiles, m_DeletingProfiles;

6
libi2pd/NetDbRequests.cpp

@ -246,7 +246,8 @@ namespace data
if (CheckLogLevel (eLogDebug)) if (CheckLogLevel (eLogDebug))
LogPrint (eLogDebug, "NetDbReq: Try ", dest->GetDestination ().ToBase64 (), " at ", count, " floodfill ", nextFloodfill->GetIdentHash ().ToBase64 (), " directly"); LogPrint (eLogDebug, "NetDbReq: Try ", dest->GetDestination ().ToBase64 (), " at ", count, " floodfill ", nextFloodfill->GetIdentHash ().ToBase64 (), " directly");
auto msg = dest->CreateRequestMessage (nextFloodfill->GetIdentHash ()); auto msg = dest->CreateRequestMessage (nextFloodfill->GetIdentHash ());
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->SendNextRequest (dest); }; auto s = shared_from_this ();
msg->onDrop = [s, dest]() { if (dest->IsActive ()) s->SendNextRequest (dest); };
i2p::transport::transports.SendMessage (nextFloodfill->GetIdentHash (), msg); i2p::transport::transports.SendMessage (nextFloodfill->GetIdentHash (), msg);
} }
else else
@ -261,7 +262,8 @@ namespace data
if (CheckLogLevel (eLogDebug)) if (CheckLogLevel (eLogDebug))
LogPrint (eLogDebug, "NetDbReq: Try ", dest->GetDestination ().ToBase64 (), " at ", count, " floodfill ", nextFloodfill->GetIdentHash ().ToBase64 (), " through tunnels"); LogPrint (eLogDebug, "NetDbReq: Try ", dest->GetDestination ().ToBase64 (), " at ", count, " floodfill ", nextFloodfill->GetIdentHash ().ToBase64 (), " through tunnels");
auto msg = dest->CreateRequestMessage (nextFloodfill, inbound); auto msg = dest->CreateRequestMessage (nextFloodfill, inbound);
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->SendNextRequest (dest); }; auto s = shared_from_this ();
msg->onDrop = [s, dest]() { if (dest->IsActive ()) s->SendNextRequest (dest); };
outbound->SendTunnelDataMsgTo (nextFloodfill->GetIdentHash (), 0, outbound->SendTunnelDataMsgTo (nextFloodfill->GetIdentHash (), 0,
i2p::garlic::WrapECIESX25519MessageForRouter (msg, nextFloodfill->GetIdentity ()->GetEncryptionPublicKey ())); i2p::garlic::WrapECIESX25519MessageForRouter (msg, nextFloodfill->GetIdentity ()->GetEncryptionPublicKey ()));
} }

2
libi2pd/NetDbRequests.h

@ -67,7 +67,7 @@ namespace data
RequestComplete m_RequestComplete; RequestComplete m_RequestComplete;
}; };
class NetDbRequests class NetDbRequests: public std::enable_shared_from_this<NetDbRequests>
{ {
public: public:

Loading…
Cancel
Save