mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2024-11-11 19:10:55 +00:00
pick random tunnel from LeaseSet
This commit is contained in:
parent
38cb57a4c4
commit
74a7f8c869
2
Garlic.h
2
Garlic.h
@ -35,7 +35,7 @@ namespace garlic
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
const int TAGS_EXPIRATION_TIMEOUT = 660; // 15 minutes
|
||||
const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
|
||||
class GarlicRoutingSession
|
||||
{
|
||||
public:
|
||||
|
@ -57,13 +57,13 @@ namespace data
|
||||
LogPrint ("LeaseSet verification failed");
|
||||
}
|
||||
|
||||
std::set<Lease> LeaseSet::GetNonExpiredLeases () const
|
||||
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
|
||||
{
|
||||
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||
std::set<Lease> leases;
|
||||
std::vector<Lease> leases;
|
||||
for (auto& it: m_Leases)
|
||||
if (ts < it.endDate)
|
||||
leases.insert (it);
|
||||
leases.push_back (it);
|
||||
return leases;
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include "Identity.h"
|
||||
|
||||
namespace i2p
|
||||
@ -43,7 +42,7 @@ namespace data
|
||||
const Identity& GetIdentity () const { return m_Identity; };
|
||||
const IdentHash& GetIdentHash () const { return m_IdentHash; };
|
||||
const std::vector<Lease>& GetLeases () const { return m_Leases; };
|
||||
std::set<Lease> GetNonExpiredLeases () const;
|
||||
const std::vector<Lease> GetNonExpiredLeases () const;
|
||||
bool HasExpiredLeases () const;
|
||||
bool HasNonExpiredLeases () const;
|
||||
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
|
||||
|
@ -20,6 +20,7 @@ namespace stream
|
||||
m_RemoteLeaseSet (remote), m_OutboundTunnel (nullptr)
|
||||
{
|
||||
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
||||
UpdateCurrentRemoteLease ();
|
||||
}
|
||||
|
||||
Stream::~Stream ()
|
||||
@ -62,6 +63,7 @@ namespace stream
|
||||
{
|
||||
// we have received duplicate. Most likely our outbound tunnel is dead
|
||||
LogPrint ("Duplicate message ", receivedSeqn, " received");
|
||||
UpdateCurrentRemoteLease (); // pick another lease
|
||||
m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); // pick another tunnel
|
||||
if (m_OutboundTunnel)
|
||||
SendQuickAck (); // resend ack for previous message again
|
||||
@ -276,11 +278,12 @@ namespace stream
|
||||
m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
|
||||
if (m_OutboundTunnel)
|
||||
{
|
||||
auto leases = m_RemoteLeaseSet.GetNonExpiredLeases ();
|
||||
if (!leases.empty ())
|
||||
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||
if (ts >= m_CurrentRemoteLease.endDate)
|
||||
UpdateCurrentRemoteLease ();
|
||||
if (ts < m_CurrentRemoteLease.endDate)
|
||||
{
|
||||
auto& lease = *leases.begin (); // TODO:
|
||||
m_OutboundTunnel->SendTunnelDataMsg (lease.tunnelGateway, lease.tunnelID, msg);
|
||||
m_OutboundTunnel->SendTunnelDataMsg (m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, msg);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@ -297,6 +300,18 @@ namespace stream
|
||||
return false;
|
||||
}
|
||||
|
||||
void Stream::UpdateCurrentRemoteLease ()
|
||||
{
|
||||
auto leases = m_RemoteLeaseSet.GetNonExpiredLeases ();
|
||||
if (!leases.empty ())
|
||||
{
|
||||
uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1);
|
||||
m_CurrentRemoteLease = leases[i];
|
||||
}
|
||||
else
|
||||
m_CurrentRemoteLease.endDate = 0;
|
||||
}
|
||||
|
||||
StreamingDestination * sharedLocalDestination = nullptr;
|
||||
|
||||
StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
|
||||
@ -402,7 +417,7 @@ namespace stream
|
||||
size += 32; // tunnel_gw
|
||||
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ());
|
||||
size += 4; // tunnel_id
|
||||
uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT;
|
||||
uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
|
||||
ts *= 1000; // in milliseconds
|
||||
*(uint64_t *)(buf + size) = htobe64 (ts);
|
||||
size += 8; // end_date
|
||||
|
@ -91,12 +91,15 @@ namespace stream
|
||||
void SavePacket (Packet * packet);
|
||||
void ProcessPacket (Packet * packet);
|
||||
|
||||
void UpdateCurrentRemoteLease ();
|
||||
|
||||
private:
|
||||
|
||||
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber;
|
||||
bool m_IsOpen, m_LeaseSetUpdated;
|
||||
StreamingDestination * m_LocalDestination;
|
||||
const i2p::data::LeaseSet& m_RemoteLeaseSet;
|
||||
i2p::data::Lease m_CurrentRemoteLease;
|
||||
i2p::util::Queue<Packet> m_ReceiveQueue;
|
||||
std::set<Packet *, PacketCmp> m_SavedPackets;
|
||||
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
|
||||
|
Loading…
Reference in New Issue
Block a user