Browse Source

shared_ptr for GarlicRouting Session

pull/151/head
orignal 10 years ago
parent
commit
b269bda52b
  1. 15
      Garlic.cpp
  2. 10
      Garlic.h
  3. 4
      Streaming.cpp
  4. 2
      Streaming.h

15
Garlic.cpp

@ -197,7 +197,7 @@ namespace garlic
{ {
(*numCloves)++; (*numCloves)++;
m_UnconfirmedTagsMsgs[msgID] = newTags; m_UnconfirmedTagsMsgs[msgID] = newTags;
m_Owner->DeliveryStatusSent (this, msgID); m_Owner->DeliveryStatusSent (shared_from_this (), msgID);
} }
else else
LogPrint ("DeliveryStatus clove was not created"); LogPrint ("DeliveryStatus clove was not created");
@ -306,9 +306,6 @@ namespace garlic
GarlicDestination::~GarlicDestination () GarlicDestination::~GarlicDestination ()
{ {
for (auto it: m_Sessions)
delete it.second;
m_Sessions.clear ();
} }
void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag) void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag)
@ -503,23 +500,23 @@ namespace garlic
} }
} }
GarlicRoutingSession * GarlicDestination::GetRoutingSession ( std::shared_ptr<GarlicRoutingSession> GarlicDestination::GetRoutingSession (
const i2p::data::RoutingDestination& destination, int numTags) const i2p::data::RoutingDestination& destination, int numTags)
{ {
auto it = m_Sessions.find (destination.GetIdentHash ()); auto it = m_Sessions.find (destination.GetIdentHash ());
GarlicRoutingSession * session = nullptr; std::shared_ptr<GarlicRoutingSession> session;
if (it != m_Sessions.end ()) if (it != m_Sessions.end ())
session = it->second; session = it->second;
if (!session) if (!session)
{ {
session = new GarlicRoutingSession (this, &destination, numTags); session = std::make_shared<GarlicRoutingSession> (this, &destination, numTags);
std::unique_lock<std::mutex> l(m_SessionsMutex); std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[destination.GetIdentHash ()] = session; m_Sessions[destination.GetIdentHash ()] = session;
} }
return session; return session;
} }
void GarlicDestination::DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID) void GarlicDestination::DeliveryStatusSent (std::shared_ptr<GarlicRoutingSession> session, uint32_t msgID)
{ {
m_CreatedSessions[msgID] = session; m_CreatedSessions[msgID] = session;
} }
@ -533,7 +530,7 @@ namespace garlic
{ {
it->second->TagsConfirmed (msgID); it->second->TagsConfirmed (msgID);
m_CreatedSessions.erase (it); m_CreatedSessions.erase (it);
LogPrint ("Garlic message ", msgID, " acknowledged"); LogPrint (eLogInfo, "Garlic message ", msgID, " acknowledged");
} }
} }
DeleteI2NPMessage (msg); DeleteI2NPMessage (msg);

10
Garlic.h

@ -54,7 +54,7 @@ namespace garlic
}; };
class GarlicDestination; class GarlicDestination;
class GarlicRoutingSession class GarlicRoutingSession: public std::enable_shared_from_this<GarlicRoutingSession>
{ {
struct UnconfirmedTags struct UnconfirmedTags
{ {
@ -105,13 +105,13 @@ namespace garlic
GarlicDestination (): m_LastTagsCleanupTime (0) {}; GarlicDestination (): m_LastTagsCleanupTime (0) {};
~GarlicDestination (); ~GarlicDestination ();
GarlicRoutingSession * GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags); std::shared_ptr<GarlicRoutingSession> GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * msg, bool attachLeaseSet = false); I2NPMessage * msg, bool attachLeaseSet = false);
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread
void DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID); void DeliveryStatusSent (std::shared_ptr<GarlicRoutingSession> session, uint32_t msgID);
virtual void ProcessGarlicMessage (I2NPMessage * msg); virtual void ProcessGarlicMessage (I2NPMessage * msg);
virtual void ProcessDeliveryStatusMessage (I2NPMessage * msg); virtual void ProcessDeliveryStatusMessage (I2NPMessage * msg);
@ -135,12 +135,12 @@ namespace garlic
// outgoing sessions // outgoing sessions
std::mutex m_SessionsMutex; std::mutex m_SessionsMutex;
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions; std::map<i2p::data::IdentHash, std::shared_ptr<GarlicRoutingSession> > m_Sessions;
// incoming // incoming
std::map<SessionTag, std::shared_ptr<i2p::crypto::CBCDecryption>> m_Tags; std::map<SessionTag, std::shared_ptr<i2p::crypto::CBCDecryption>> m_Tags;
uint32_t m_LastTagsCleanupTime; uint32_t m_LastTagsCleanupTime;
// DeliveryStatus // DeliveryStatus
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session std::map<uint32_t, std::shared_ptr<GarlicRoutingSession> > m_CreatedSessions; // msgID -> session
}; };
} }
} }

4
Streaming.cpp

@ -15,7 +15,7 @@ namespace stream
const i2p::data::LeaseSet& remote, int port): m_Service (service), m_SendStreamID (0), const i2p::data::LeaseSet& remote, int port): m_Service (service), m_SendStreamID (0),
m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false),
m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local),
m_RemoteLeaseSet (&remote), m_RoutingSession (nullptr), m_CurrentOutboundTunnel (nullptr), m_RemoteLeaseSet (&remote), m_CurrentOutboundTunnel (nullptr),
m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port) m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port)
{ {
@ -26,7 +26,7 @@ namespace stream
Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): Stream::Stream (boost::asio::io_service& service, StreamingDestination& local):
m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1),
m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local),
m_RemoteLeaseSet (nullptr), m_RoutingSession (nullptr), m_CurrentOutboundTunnel (nullptr), m_RemoteLeaseSet (nullptr), m_CurrentOutboundTunnel (nullptr),
m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0) m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0)
{ {

2
Streaming.h

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

Loading…
Cancel
Save