Browse Source

shared_ptr for Lease

pull/373/head
orignal 9 years ago
parent
commit
7d927b0e28
  1. 2
      Datagram.cpp
  2. 4
      I2NPProtocol.cpp
  3. 12
      LeaseSet.cpp
  4. 20
      LeaseSet.h
  5. 17
      Streaming.cpp
  6. 2
      Streaming.h

2
Datagram.cpp

@ -66,7 +66,7 @@ namespace datagram
msgs.push_back (i2p::tunnel::TunnelMessageBlock msgs.push_back (i2p::tunnel::TunnelMessageBlock
{ {
i2p::tunnel::eDeliveryTypeTunnel, i2p::tunnel::eDeliveryTypeTunnel,
leases[i].tunnelGateway, leases[i].tunnelID, leases[i]->tunnelGateway, leases[i]->tunnelID,
garlic garlic
}); });
outboundTunnel->SendTunnelDataMsg (msgs); outboundTunnel->SendTunnelDataMsg (msgs);

4
I2NPProtocol.cpp

@ -265,9 +265,9 @@ namespace i2p
auto leases = leaseSet->GetNonExpiredLeases (); auto leases = leaseSet->GetNonExpiredLeases ();
if (leases.size () > 0) if (leases.size () > 0)
{ {
htobe32buf (payload + size, leases[0].tunnelID); htobe32buf (payload + size, leases[0]->tunnelID);
size += 4; // reply tunnelID size += 4; // reply tunnelID
memcpy (payload + size, leases[0].tunnelGateway, 32); memcpy (payload + size, leases[0]->tunnelGateway, 32);
size += 32; // reply tunnel gateway size += 32; // reply tunnel gateway
} }
else else

12
LeaseSet.cpp

@ -128,7 +128,9 @@ namespace data
m_ExpirationTime = lease.endDate; m_ExpirationTime = lease.endDate;
if (m_StoreLeases) if (m_StoreLeases)
{ {
m_Leases.push_back (lease); auto l = std::make_shared<Lease>();
*l = lease;
m_Leases.push_back (l);
// check if lease's gateway is in our netDb // check if lease's gateway is in our netDb
if (!netdb.FindRouter (lease.tunnelGateway)) if (!netdb.FindRouter (lease.tunnelGateway))
{ {
@ -147,13 +149,13 @@ namespace data
} }
} }
const std::vector<Lease> LeaseSet::GetNonExpiredLeases (bool withThreshold) const const std::vector<std::shared_ptr<Lease> > LeaseSet::GetNonExpiredLeases (bool withThreshold) const
{ {
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
std::vector<Lease> leases; std::vector<std::shared_ptr<Lease> > leases;
for (auto& it: m_Leases) for (auto& it: m_Leases)
{ {
auto endDate = it.endDate; auto endDate = it->endDate;
if (!withThreshold) if (!withThreshold)
endDate -= i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000; endDate -= i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000;
if (ts < endDate) if (ts < endDate)
@ -166,7 +168,7 @@ namespace data
{ {
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto& it: m_Leases) for (auto& it: m_Leases)
if (ts >= it.endDate) return true; if (ts >= it->endDate) return true;
return false; return false;
} }

20
LeaseSet.h

@ -4,6 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <vector> #include <vector>
#include <memory>
#include "Identity.h" #include "Identity.h"
namespace i2p namespace i2p
@ -21,14 +22,6 @@ namespace data
IdentHash tunnelGateway; IdentHash tunnelGateway;
uint32_t tunnelID; uint32_t tunnelID;
uint64_t endDate; uint64_t endDate;
bool operator< (const Lease& other) const
{
if (endDate != other.endDate)
return endDate > other.endDate;
else
return tunnelID < other.tunnelID;
}
}; };
const int MAX_LS_BUFFER_SIZE = 3072; const int MAX_LS_BUFFER_SIZE = 3072;
@ -48,13 +41,14 @@ namespace data
size_t GetBufferLen () const { return m_BufferLen; }; size_t GetBufferLen () const { return m_BufferLen; };
bool IsValid () const { return m_IsValid; }; bool IsValid () const { return m_IsValid; };
// implements RoutingDestination const std::vector<std::shared_ptr<Lease> >& GetLeases () const { return m_Leases; };
const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); }; const std::vector<std::shared_ptr<Lease> > GetNonExpiredLeases (bool withThreshold = true) const;
const std::vector<Lease>& GetLeases () const { return m_Leases; };
const std::vector<Lease> GetNonExpiredLeases (bool withThreshold = true) const;
bool HasExpiredLeases () const; bool HasExpiredLeases () const;
bool IsExpired () const; bool IsExpired () const;
uint64_t GetExpirationTime () const { return m_ExpirationTime; }; uint64_t GetExpirationTime () const { return m_ExpirationTime; };
// implements RoutingDestination
const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
bool IsDestination () const { return true; }; bool IsDestination () const { return true; };
@ -65,7 +59,7 @@ namespace data
private: private:
bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill
std::vector<Lease> m_Leases; std::vector<std::shared_ptr<Lease> > m_Leases;
uint64_t m_ExpirationTime; // in milliseconds uint64_t m_ExpirationTime; // in milliseconds
std::shared_ptr<const IdentityEx> m_Identity; std::shared_ptr<const IdentityEx> m_Identity;
uint8_t m_EncryptionKey[256]; uint8_t m_EncryptionKey[256];

17
Streaming.cpp

@ -22,7 +22,6 @@ namespace stream
{ {
RAND_bytes ((uint8_t *)&m_RecvStreamID, 4); RAND_bytes ((uint8_t *)&m_RecvStreamID, 4);
m_RemoteIdentity = remote->GetIdentity (); m_RemoteIdentity = remote->GetIdentity ();
m_CurrentRemoteLease.endDate = 0;
} }
Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): Stream::Stream (boost::asio::io_service& service, StreamingDestination& local):
@ -599,9 +598,9 @@ namespace stream
} }
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
if (!m_CurrentRemoteLease.endDate || ts >= m_CurrentRemoteLease.endDate - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000) if (m_CurrentRemoteLease || ts >= m_CurrentRemoteLease->endDate - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000)
UpdateCurrentRemoteLease (true); UpdateCurrentRemoteLease (true);
if (ts < m_CurrentRemoteLease.endDate) if (m_CurrentRemoteLease && ts < m_CurrentRemoteLease->endDate)
{ {
std::vector<i2p::tunnel::TunnelMessageBlock> msgs; std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
for (auto it: packets) for (auto it: packets)
@ -610,7 +609,7 @@ namespace stream
msgs.push_back (i2p::tunnel::TunnelMessageBlock msgs.push_back (i2p::tunnel::TunnelMessageBlock
{ {
i2p::tunnel::eDeliveryTypeTunnel, i2p::tunnel::eDeliveryTypeTunnel,
m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, m_CurrentRemoteLease->tunnelGateway, m_CurrentRemoteLease->tunnelID,
msg msg
}); });
m_NumSentBytes += it->GetLength (); m_NumSentBytes += it->GetLength ();
@ -725,10 +724,10 @@ namespace stream
if (!leases.empty ()) if (!leases.empty ())
{ {
bool updated = false; bool updated = false;
if (expired) if (expired && m_CurrentRemoteLease)
{ {
for (auto it: leases) for (auto it: leases)
if ((it.tunnelGateway == m_CurrentRemoteLease.tunnelGateway) && (it.tunnelID != m_CurrentRemoteLease.tunnelID)) if ((it->tunnelGateway == m_CurrentRemoteLease->tunnelGateway) && (it->tunnelID != m_CurrentRemoteLease->tunnelID))
{ {
m_CurrentRemoteLease = it; m_CurrentRemoteLease = it;
updated = true; updated = true;
@ -738,7 +737,7 @@ namespace stream
if (!updated) if (!updated)
{ {
uint32_t i = rand () % leases.size (); uint32_t i = rand () % leases.size ();
if (m_CurrentRemoteLease.endDate && leases[i].tunnelID == m_CurrentRemoteLease.tunnelID) if (m_CurrentRemoteLease && leases[i]->tunnelID == m_CurrentRemoteLease->tunnelID)
// make sure we don't select previous // make sure we don't select previous
i = (i + 1) % leases.size (); // if so, pick next i = (i + 1) % leases.size (); // if so, pick next
m_CurrentRemoteLease = leases[i]; m_CurrentRemoteLease = leases[i];
@ -747,12 +746,12 @@ namespace stream
else else
{ {
m_RemoteLeaseSet = nullptr; m_RemoteLeaseSet = nullptr;
m_CurrentRemoteLease.endDate = 0; m_CurrentRemoteLease = nullptr;
// re-request expired // re-request expired
} }
} }
else else
m_CurrentRemoteLease.endDate = 0; m_CurrentRemoteLease = nullptr;
} }
std::shared_ptr<I2NPMessage> Stream::CreateDataMessage (const uint8_t * payload, size_t len) std::shared_ptr<I2NPMessage> Stream::CreateDataMessage (const uint8_t * payload, size_t len)

2
Streaming.h

@ -172,7 +172,7 @@ namespace stream
std::shared_ptr<const i2p::data::IdentityEx> m_RemoteIdentity; std::shared_ptr<const i2p::data::IdentityEx> m_RemoteIdentity;
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet; std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession; std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
i2p::data::Lease m_CurrentRemoteLease; std::shared_ptr<i2p::data::Lease> m_CurrentRemoteLease;
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel; std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
std::queue<Packet *> m_ReceiveQueue; std::queue<Packet *> m_ReceiveQueue;
std::set<Packet *, PacketCmp> m_SavedPackets; std::set<Packet *, PacketCmp> m_SavedPackets;

Loading…
Cancel
Save