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. 2
      libi2pd/NetDb.hpp
  3. 20
      libi2pd/RouterInfo.cpp
  4. 17
      libi2pd/RouterInfo.h

1
libi2pd/NetDb.cpp

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

2
libi2pd/NetDb.hpp

@ -122,7 +122,6 @@ namespace data @@ -122,7 +122,6 @@ namespace data
void ClearRouterInfos () { m_RouterInfos.clear (); };
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; };
@ -180,7 +179,6 @@ namespace data @@ -180,7 +179,6 @@ namespace data
uint32_t m_PublishReplyToken = 0;
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
};
extern NetDb netdb;

20
libi2pd/RouterInfo.cpp

@ -29,6 +29,12 @@ namespace i2p @@ -29,6 +29,12 @@ namespace i2p
{
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)
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
@ -44,15 +50,14 @@ namespace data @@ -44,15 +50,14 @@ namespace data
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_ReachableTransports (0), m_Caps (0), m_Version (0)
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
if (len <= MAX_RI_BUFFER_SIZE)
{
m_Buffer = netdb.NewRouterInfoBuffer ();
memcpy (m_Buffer->data (), buf, len);
m_Addresses = boost::make_shared<Addresses>(); // create empty list
m_Buffer = buf;
m_BufferLen = len;
ReadFromBuffer (true);
}
@ -64,6 +69,11 @@ namespace data @@ -64,6 +69,11 @@ namespace data
}
}
RouterInfo::RouterInfo (const uint8_t * buf, size_t len):
RouterInfo (std::make_shared<Buffer> (buf, len), len)
{
}
RouterInfo::~RouterInfo ()
{
}
@ -205,7 +215,7 @@ namespace data @@ -205,7 +215,7 @@ namespace data
for (int i = 0; i < numAddresses; i++)
{
uint8_t supportedTransports = 0;
auto address = netdb.NewRouterInfoAddress ();
auto address = std::make_shared<Address> ();
uint8_t cost; // ignore
s.read ((char *)&cost, sizeof (cost));
s.read ((char *)&address->date, sizeof (address->date));

17
libi2pd/RouterInfo.h

@ -1,5 +1,5 @@ @@ -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
*
@ -54,7 +54,7 @@ namespace data @@ -54,7 +54,7 @@ namespace data
const uint8_t COST_SSU_DIRECT = 9;
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
{
public:
@ -158,14 +158,23 @@ namespace data @@ -158,14 +158,23 @@ namespace data
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 ()); };
};
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::array<uint8_t, MAX_RI_BUFFER_SIZE> Buffer;
RouterInfo ();
RouterInfo (const std::string& fullPath);
RouterInfo (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 ();
std::shared_ptr<const IdentityEx> GetRouterIdentity () const { return m_RouterIdentity; };

Loading…
Cancel
Save