Browse Source

create RouterInfo from shared pointer to Buffer

pull/1727/head
orignal 3 years ago
parent
commit
e4ab0acc92
  1. 1
      libi2pd/NetDb.cpp
  2. 4
      libi2pd/NetDb.hpp
  3. 22
      libi2pd/RouterInfo.cpp
  4. 17
      libi2pd/RouterInfo.h

1
libi2pd/NetDb.cpp

@ -630,7 +630,6 @@ namespace data
} // m_RouterInfos iteration } // m_RouterInfos iteration
m_RouterInfoBuffersPool.CleanUpMt (); m_RouterInfoBuffersPool.CleanUpMt ();
m_RouterInfoAddressesPool.CleanUpMt ();
if (updatedCount > 0) if (updatedCount > 0)
LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers"); LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers");

4
libi2pd/NetDb.hpp

@ -122,8 +122,7 @@ namespace data
void ClearRouterInfos () { m_RouterInfos.clear (); }; void ClearRouterInfos () { m_RouterInfos.clear (); };
std::shared_ptr<RouterInfo::Buffer> NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); }; std::shared_ptr<RouterInfo::Buffer> NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); };
std::shared_ptr<RouterInfo::Address> NewRouterInfoAddress () { return m_RouterInfoAddressesPool.AcquireSharedMt (); };
uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; }; uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; };
private: private:
@ -180,7 +179,6 @@ namespace data
uint32_t m_PublishReplyToken = 0; uint32_t m_PublishReplyToken = 0;
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool; i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
}; };
extern NetDb netdb; extern NetDb netdb;

22
libi2pd/RouterInfo.cpp

@ -29,6 +29,12 @@ namespace i2p
{ {
namespace data namespace data
{ {
RouterInfo::Buffer::Buffer (const uint8_t * buf, size_t len)
{
if (len > size ()) len = size ();
memcpy (data (), buf, len);
}
RouterInfo::RouterInfo (): m_Buffer (nullptr) RouterInfo::RouterInfo (): m_Buffer (nullptr)
{ {
m_Addresses = boost::make_shared<Addresses>(); // create empty list m_Addresses = boost::make_shared<Addresses>(); // create empty list
@ -44,15 +50,14 @@ namespace data
ReadFromFile (fullPath); ReadFromFile (fullPath);
} }
RouterInfo::RouterInfo (const uint8_t * buf, int len): RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len):
m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0),
m_ReachableTransports (0), m_Caps (0), m_Version (0) m_ReachableTransports (0), m_Caps (0), m_Version (0)
{ {
m_Addresses = boost::make_shared<Addresses>(); // create empty list
if (len <= MAX_RI_BUFFER_SIZE) if (len <= MAX_RI_BUFFER_SIZE)
{ {
m_Buffer = netdb.NewRouterInfoBuffer (); m_Addresses = boost::make_shared<Addresses>(); // create empty list
memcpy (m_Buffer->data (), buf, len); m_Buffer = buf;
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (true); ReadFromBuffer (true);
} }
@ -62,7 +67,12 @@ namespace data
m_Buffer = nullptr; m_Buffer = nullptr;
m_IsUnreachable = true; m_IsUnreachable = true;
} }
} }
RouterInfo::RouterInfo (const uint8_t * buf, size_t len):
RouterInfo (std::make_shared<Buffer> (buf, len), len)
{
}
RouterInfo::~RouterInfo () RouterInfo::~RouterInfo ()
{ {
@ -205,7 +215,7 @@ namespace data
for (int i = 0; i < numAddresses; i++) for (int i = 0; i < numAddresses; i++)
{ {
uint8_t supportedTransports = 0; uint8_t supportedTransports = 0;
auto address = netdb.NewRouterInfoAddress (); auto address = std::make_shared<Address> ();
uint8_t cost; // ignore uint8_t cost; // ignore
s.read ((char *)&cost, sizeof (cost)); s.read ((char *)&cost, sizeof (cost));
s.read ((char *)&address->date, sizeof (address->date)); s.read ((char *)&address->date, sizeof (address->date));

17
libi2pd/RouterInfo.h

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2021, The PurpleI2P Project * Copyright (c) 2013-2022, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -54,7 +54,7 @@ namespace data
const uint8_t COST_SSU_DIRECT = 9; const uint8_t COST_SSU_DIRECT = 9;
const uint8_t COST_SSU_THROUGH_INTRODUCERS = 11; const uint8_t COST_SSU_THROUGH_INTRODUCERS = 11;
const int MAX_RI_BUFFER_SIZE = 2048; // if RouterInfo exceeds 2048 we consider it as malformed, might be changed later const size_t MAX_RI_BUFFER_SIZE = 2048; // if RouterInfo exceeds 2048 we consider it as malformed, might be changed later
class RouterInfo: public RoutingDestination class RouterInfo: public RoutingDestination
{ {
public: public:
@ -158,14 +158,23 @@ namespace data
bool IsV4 () const { return (caps & AddressCaps::eV4) || (host.is_v4 () && !host.is_unspecified ()); }; bool IsV4 () const { return (caps & AddressCaps::eV4) || (host.is_v4 () && !host.is_unspecified ()); };
bool IsV6 () const { return (caps & AddressCaps::eV6) || (host.is_v6 () && !host.is_unspecified ()); }; bool IsV6 () const { return (caps & AddressCaps::eV6) || (host.is_v6 () && !host.is_unspecified ()); };
}; };
class Buffer: public std::array<uint8_t, MAX_RI_BUFFER_SIZE>
{
public:
Buffer () = default;
Buffer (const uint8_t * buf, size_t len);
};
typedef std::vector<std::shared_ptr<Address> > Addresses; typedef std::vector<std::shared_ptr<Address> > Addresses;
typedef std::array<uint8_t, MAX_RI_BUFFER_SIZE> Buffer;
RouterInfo (); RouterInfo ();
RouterInfo (const std::string& fullPath); RouterInfo (const std::string& fullPath);
RouterInfo (const RouterInfo& ) = default; RouterInfo (const RouterInfo& ) = default;
RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len); RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len);
RouterInfo (const uint8_t * buf, size_t len);
~RouterInfo (); ~RouterInfo ();
std::shared_ptr<const IdentityEx> GetRouterIdentity () const { return m_RouterIdentity; }; std::shared_ptr<const IdentityEx> GetRouterIdentity () const { return m_RouterIdentity; };

Loading…
Cancel
Save