Skip to content
/ i2pd Public
forked from PurpleI2P/i2pd

Commit

Permalink
use shared_ptr for ClientDestination
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Feb 24, 2015
1 parent 58ebd8c commit 52f806f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 52 deletions.
16 changes: 8 additions & 8 deletions BOB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace i2p
{
namespace client
{
BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, ClientDestination * localDestination):
BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination):
BOBI2PTunnel (localDestination),
m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (localDestination->GetService ())
{
Expand Down Expand Up @@ -142,7 +142,7 @@ namespace client
}

BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port,
ClientDestination * localDestination, bool quiet): BOBI2PTunnel (localDestination),
std::shared_ptr<ClientDestination> localDestination, bool quiet): BOBI2PTunnel (localDestination),
m_Endpoint (boost::asio::ip::address::from_string (address), port), m_IsQuiet (quiet)
{
}
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace client
}
}

BOBDestination::BOBDestination (ClientDestination& localDestination):
BOBDestination::BOBDestination (std::shared_ptr<ClientDestination> localDestination):
m_LocalDestination (localDestination),
m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr)
{
Expand All @@ -186,7 +186,7 @@ namespace client
{
delete m_OutboundTunnel;
delete m_InboundTunnel;
i2p::client::context.DeleteLocalDestination (&m_LocalDestination);
i2p::client::context.DeleteLocalDestination (m_LocalDestination);
}

void BOBDestination::Start ()
Expand All @@ -198,7 +198,7 @@ namespace client
void BOBDestination::Stop ()
{
StopTunnels ();
m_LocalDestination.Stop ();
m_LocalDestination->Stop ();
}

void BOBDestination::StopTunnels ()
Expand All @@ -220,13 +220,13 @@ namespace client
void BOBDestination::CreateInboundTunnel (int port)
{
if (!m_InboundTunnel)
m_InboundTunnel = new BOBI2PInboundTunnel (port, &m_LocalDestination);
m_InboundTunnel = new BOBI2PInboundTunnel (port, m_LocalDestination);
}

void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet)
{
if (!m_OutboundTunnel)
m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, &m_LocalDestination, quiet);
m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, m_LocalDestination, quiet);
}

BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner):
Expand Down Expand Up @@ -384,7 +384,7 @@ namespace client
LogPrint (eLogDebug, "BOB: start ", m_Nickname);
if (!m_CurrentDestination)
{
m_CurrentDestination = new BOBDestination (*i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options));
m_CurrentDestination = new BOBDestination (i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options));
m_Owner.AddDestination (m_Nickname, m_CurrentDestination);
}
if (m_InPort)
Expand Down
12 changes: 6 additions & 6 deletions BOB.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace client
{
public:

BOBI2PTunnel (ClientDestination * localDestination):
BOBI2PTunnel (std::shared_ptr<ClientDestination> localDestination):
I2PService (localDestination) {};

virtual void Start () {};
Expand All @@ -67,7 +67,7 @@ namespace client

public:

BOBI2PInboundTunnel (int port, ClientDestination * localDestination);
BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination);
~BOBI2PInboundTunnel ();

void Start ();
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace client
{
public:

BOBI2POutboundTunnel (const std::string& address, int port, ClientDestination * localDestination, bool quiet);
BOBI2POutboundTunnel (const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, bool quiet);

void Start ();
void Stop ();
Expand All @@ -119,19 +119,19 @@ namespace client
{
public:

BOBDestination (ClientDestination& localDestination);
BOBDestination (std::shared_ptr<ClientDestination> localDestination);
~BOBDestination ();

void Start ();
void Stop ();
void StopTunnels ();
void CreateInboundTunnel (int port);
void CreateOutboundTunnel (const std::string& address, int port, bool quiet);
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); };
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination->GetPrivateKeys (); };

private:

ClientDestination& m_LocalDestination;
std::shared_ptr<ClientDestination> m_LocalDestination;
BOBI2POutboundTunnel * m_OutboundTunnel;
BOBI2PInboundTunnel * m_InboundTunnel;
};
Expand Down
28 changes: 12 additions & 16 deletions ClientContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace client
std::string ircDestination = i2p::util::config::GetArg("-ircdest", "");
if (ircDestination.length () > 0) // ircdest is presented
{
ClientDestination * localDestination = nullptr;
std::shared_ptr<ClientDestination> localDestination = nullptr;
std::string ircKeys = i2p::util::config::GetArg("-irckeys", "");
if (ircKeys.length () > 0)
localDestination = LoadLocalDestination (ircKeys, false);
Expand Down Expand Up @@ -146,15 +146,12 @@ namespace client
}

for (auto it: m_Destinations)
{
it.second->Stop ();
delete it.second;
}
m_Destinations.clear ();
m_SharedLocalDestination = 0; // deleted through m_Destination
m_SharedLocalDestination = nullptr;
}

ClientDestination * ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic)
std::shared_ptr<ClientDestination> ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic)
{
i2p::data::PrivateKeys keys;
std::string fullPath = i2p::util::filesystem::GetFullPath (filename);
Expand Down Expand Up @@ -184,7 +181,7 @@ namespace client
LogPrint ("New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ().GetIdentHash ()), " created");
}

ClientDestination * localDestination = nullptr;
std::shared_ptr<ClientDestination> localDestination = nullptr;
std::unique_lock<std::mutex> l(m_DestinationsMutex);
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
if (it != m_Destinations.end ())
Expand All @@ -194,25 +191,25 @@ namespace client
}
else
{
localDestination = new ClientDestination (keys, isPublic);
localDestination = std::make_shared<ClientDestination> (keys, isPublic);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
}
return localDestination;
}

ClientDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
const std::map<std::string, std::string> * params)
{
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
auto localDestination = new ClientDestination (keys, isPublic, params);
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}

void ClientContext::DeleteLocalDestination (ClientDestination * destination)
void ClientContext::DeleteLocalDestination (std::shared_ptr<ClientDestination> destination)
{
if (!destination) return;
auto it = m_Destinations.find (destination->GetIdentHash ());
Expand All @@ -224,11 +221,10 @@ namespace client
m_Destinations.erase (it);
}
d->Stop ();
delete d;
}
}

ClientDestination * ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
const std::map<std::string, std::string> * params)
{
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
Expand All @@ -242,14 +238,14 @@ namespace client
}
return nullptr;
}
auto localDestination = new ClientDestination (keys, isPublic, params);
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}

ClientDestination * ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const
std::shared_ptr<ClientDestination> ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const
{
auto it = m_Destinations.find (destination);
if (it != m_Destinations.end ())
Expand Down Expand Up @@ -299,7 +295,7 @@ namespace client

for (int i = 0; i < numClientTunnels; i++)
{
ClientDestination * localDestination = nullptr;
std::shared_ptr<ClientDestination> localDestination = nullptr;
if (keys[i].length () > 0)
localDestination = LoadLocalDestination (keys[i], false);
auto clientTunnel = new I2PClientTunnel (destinations[i], ports[i], localDestination);
Expand Down
16 changes: 8 additions & 8 deletions ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ namespace client
void Start ();
void Stop ();

ClientDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
ClientDestination * CreateNewLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1,
std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1,
const std::map<std::string, std::string> * params = nullptr); // transient
ClientDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr);
void DeleteLocalDestination (ClientDestination * destination);
ClientDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
std::shared_ptr<ClientDestination> LoadLocalDestination (const std::string& filename, bool isPublic);

AddressBook& GetAddressBook () { return m_AddressBook; };
const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
Expand All @@ -56,8 +56,8 @@ namespace client
private:

std::mutex m_DestinationsMutex;
std::map<i2p::data::IdentHash, ClientDestination *> m_Destinations;
ClientDestination * m_SharedLocalDestination;
std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
std::shared_ptr<ClientDestination> m_SharedLocalDestination;

AddressBook m_AddressBook;

Expand Down
2 changes: 1 addition & 1 deletion I2PService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace client
{
static const i2p::data::SigningKeyType I2P_SERVICE_DEFAULT_KEY_TYPE = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256;

I2PService::I2PService (ClientDestination * localDestination):
I2PService::I2PService (std::shared_ptr<ClientDestination> localDestination):
m_LocalDestination (localDestination ? localDestination :
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE))
{
Expand Down
10 changes: 5 additions & 5 deletions I2PService.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace client
class I2PService
{
public:
I2PService (ClientDestination * localDestination = nullptr);
I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr);
I2PService (i2p::data::SigningKeyType kt);
virtual ~I2PService () { ClearHandlers (); }

Expand All @@ -37,8 +37,8 @@ namespace client
m_Handlers.clear();
}

inline ClientDestination * GetLocalDestination () { return m_LocalDestination; }
inline void SetLocalDestination (ClientDestination * dest) { m_LocalDestination = dest; }
inline std::shared_ptr<ClientDestination> GetLocalDestination () { return m_LocalDestination; }
inline void SetLocalDestination (std::shared_ptr<ClientDestination> dest) { m_LocalDestination = dest; }
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0);

inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
Expand All @@ -49,7 +49,7 @@ namespace client
virtual const char* GetName() { return "Generic I2P Service"; }
private:

ClientDestination * m_LocalDestination;
std::shared_ptr<ClientDestination> m_LocalDestination;
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
std::mutex m_HandlersMutex;
};
Expand Down Expand Up @@ -81,7 +81,7 @@ namespace client
class TCPIPAcceptor: public I2PService
{
public:
TCPIPAcceptor (int port, ClientDestination * localDestination = nullptr) :
TCPIPAcceptor (int port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
I2PService(localDestination),
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_Timer (GetService ()) {}
Expand Down
4 changes: 2 additions & 2 deletions I2PTunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace client
Done(shared_from_this());
}

I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination):
I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, std::shared_ptr<ClientDestination> localDestination):
TCPIPAcceptor (port,localDestination), m_Destination (destination), m_DestinationIdentHash (nullptr)
{}

Expand Down Expand Up @@ -243,7 +243,7 @@ namespace client
return nullptr;
}

I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination):
I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination):
I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port)
{
}
Expand Down
4 changes: 2 additions & 2 deletions I2PTunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace client

public:

I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr);
I2PClientTunnel (const std::string& destination, int port, std::shared_ptr<ClientDestination> localDestination = nullptr);
~I2PClientTunnel () {}

void Start ();
Expand All @@ -83,7 +83,7 @@ namespace client
{
public:

I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination);
I2PServerTunnel (const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination);

void Start ();
void Stop ();
Expand Down
4 changes: 2 additions & 2 deletions SAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ namespace client
LogPrint (eLogWarning, "Datagram size ", len," exceeds buffer");
}

SAMSession::SAMSession (ClientDestination * dest):
SAMSession::SAMSession (std::shared_ptr<ClientDestination> dest):
localDestination (dest)
{
}
Expand Down Expand Up @@ -700,7 +700,7 @@ namespace client
SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination,
const std::map<std::string, std::string> * params)
{
ClientDestination * localDestination = nullptr;
std::shared_ptr<ClientDestination> localDestination = nullptr;
if (destination != "")
{
i2p::data::PrivateKeys keys;
Expand Down
4 changes: 2 additions & 2 deletions SAM.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ namespace client

struct SAMSession
{
ClientDestination * localDestination;
std::shared_ptr<ClientDestination> localDestination;
std::list<std::shared_ptr<SAMSocket> > sockets;

SAMSession (ClientDestination * localDestination);
SAMSession (std::shared_ptr<ClientDestination> dest);
~SAMSession ();

void CloseStreams ();
Expand Down

0 comments on commit 52f806f

Please sign in to comment.