Skip to content

Commit

Permalink
shared_ptr for RoutingDestination
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 29, 2015
1 parent 938fa00 commit 974a7ff
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Datagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace datagram
{
}

void DatagramDestination::SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::LeaseSet& remote)
void DatagramDestination::SendDatagramTo (const uint8_t * payload, size_t len, std::shared_ptr<const i2p::data::LeaseSet> remote)
{
uint8_t buf[MAX_DATAGRAM_SIZE];
auto identityLen = m_Owner.GetIdentity ().ToBuffer (buf, MAX_DATAGRAM_SIZE);
Expand All @@ -40,10 +40,10 @@ namespace datagram
CreateDataMessage (buf, len + headerLen), remote));
}

void DatagramDestination::SendMsg (I2NPMessage * msg, const i2p::data::LeaseSet& remote)
void DatagramDestination::SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote)
{
auto outboundTunnel = m_Owner.GetTunnelPool ()->GetNextOutboundTunnel ();
auto leases = remote.GetNonExpiredLeases ();
auto leases = remote->GetNonExpiredLeases ();
if (!leases.empty () && outboundTunnel)
{
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
Expand Down
5 changes: 3 additions & 2 deletions Datagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DATAGRAM_H__

#include <inttypes.h>
#include <memory>
#include <functional>
#include "Identity.h"
#include "LeaseSet.h"
Expand All @@ -25,7 +26,7 @@ namespace datagram
DatagramDestination (i2p::client::ClientDestination& owner);
~DatagramDestination () {};

void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::LeaseSet& remote);
void SendDatagramTo (const uint8_t * payload, size_t len, std::shared_ptr<const i2p::data::LeaseSet> remote);
void HandleDataMessagePayload (const uint8_t * buf, size_t len);

void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
Expand All @@ -34,7 +35,7 @@ namespace datagram
private:

I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len);
void SendMsg (I2NPMessage * msg, const i2p::data::LeaseSet& remote);
void SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote);
void HandleDatagram (const uint8_t * buf, size_t len);

private:
Expand Down
4 changes: 2 additions & 2 deletions Destination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ namespace client
m_ExcludedFloodfills.insert (floodfill->GetIdentHash ());
LogPrint (eLogDebug, "Publish LeaseSet of ", GetIdentHash ().ToBase32 ());
m_PublishReplyToken = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
auto msg = WrapMessage (*floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken));
auto msg = WrapMessage (floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken));
m_PublishConfirmationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT));
m_PublishConfirmationTimer.async_wait (std::bind (&ClientDestination::HandlePublishConfirmationTimer,
this, std::placeholders::_1));
Expand Down Expand Up @@ -508,7 +508,7 @@ namespace client
rnd.GenerateBlock (replyTag, 32); // random session tag
AddSessionKey (replyKey, replyTag);

I2NPMessage * msg = WrapMessage (*nextFloodfill,
I2NPMessage * msg = WrapMessage (nextFloodfill,
CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
replyTunnel.get (), replyKey, replyTag));
outboundTunnel->SendTunnelDataMsg (
Expand Down
14 changes: 7 additions & 7 deletions Garlic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace i2p
namespace garlic
{
GarlicRoutingSession::GarlicRoutingSession (GarlicDestination * owner,
const i2p::data::RoutingDestination * destination, int numTags):
std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags):
m_Owner (owner), m_Destination (destination), m_NumTags (numTags),
m_LeaseSetUpdated (numTags > 0)
{
Expand Down Expand Up @@ -501,7 +501,7 @@ namespace garlic
}
}

I2NPMessage * GarlicDestination::WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * GarlicDestination::WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
I2NPMessage * msg, bool attachLeaseSet)
{
if (attachLeaseSet) // we should maintain this session
Expand All @@ -511,23 +511,23 @@ namespace garlic
}
else // one time session
{
GarlicRoutingSession session (this, &destination, 0); // don't use tag if no LeaseSet
GarlicRoutingSession session (this, destination, 0); // don't use tag if no LeaseSet
return session.WrapSingleMessage (msg);
}
}

std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
const i2p::data::RoutingDestination& destination, int numTags)
std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags)
{
auto it = m_Sessions.find (destination.GetIdentHash ());
auto it = m_Sessions.find (destination->GetIdentHash ());
std::shared_ptr<GarlicRoutingSession> session;
if (it != m_Sessions.end ())
session = it->second;
if (!session)
{
session = std::make_shared<GarlicRoutingSession> (this, &destination, numTags);
session = std::make_shared<GarlicRoutingSession> (this, destination, numTags);
std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[destination.GetIdentHash ()] = session;
m_Sessions[destination->GetIdentHash ()] = session;
}
return session;
}
Expand Down
8 changes: 4 additions & 4 deletions Garlic.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace garlic

public:

GarlicRoutingSession (GarlicDestination * owner, const i2p::data::RoutingDestination * destination, int numTags);
GarlicRoutingSession (GarlicDestination * owner, std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags);
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg);
Expand All @@ -88,7 +88,7 @@ namespace garlic
private:

GarlicDestination * m_Owner;
const i2p::data::RoutingDestination * m_Destination;
std::shared_ptr<const i2p::data::RoutingDestination> m_Destination;
i2p::crypto::AESKey m_SessionKey;
std::list<SessionTag> m_SessionTags;
int m_NumTags;
Expand All @@ -106,10 +106,10 @@ namespace garlic
GarlicDestination (): m_LastTagsCleanupTime (0) {};
~GarlicDestination ();

std::shared_ptr<GarlicRoutingSession> GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags);
void CleanupRoutingSessions ();
void RemoveCreatedSession (uint32_t msgID);
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
I2NPMessage * msg, bool attachLeaseSet = false);

void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
Expand Down
2 changes: 1 addition & 1 deletion SAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ namespace client
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ());
if (leaseSet)
session->localDestination->GetDatagramDestination ()->
SendDatagramTo ((uint8_t *)eol, payloadLen, *leaseSet);
SendDatagramTo ((uint8_t *)eol, payloadLen, leaseSet);
else
{
LogPrint ("SAM datagram destination not found");
Expand Down
2 changes: 1 addition & 1 deletion Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ namespace stream
if (m_RemoteLeaseSet)
{
if (!m_RoutingSession)
m_RoutingSession = m_LocalDestination.GetOwner ().GetRoutingSession (*m_RemoteLeaseSet, 32);
m_RoutingSession = m_LocalDestination.GetOwner ().GetRoutingSession (m_RemoteLeaseSet, 32);
auto leases = m_RemoteLeaseSet->GetNonExpiredLeases ();
if (!leases.empty ())
{
Expand Down

0 comments on commit 974a7ff

Please sign in to comment.