Browse Source

use shared_ptr for ClientDestination

pull/163/head
orignal 10 years ago
parent
commit
52f806ff94
  1. 16
      BOB.cpp
  2. 12
      BOB.h
  3. 28
      ClientContext.cpp
  4. 16
      ClientContext.h
  5. 2
      I2PService.cpp
  6. 10
      I2PService.h
  7. 4
      I2PTunnel.cpp
  8. 4
      I2PTunnel.h
  9. 4
      SAM.cpp
  10. 4
      SAM.h

16
BOB.cpp

@ -8,7 +8,7 @@ namespace i2p
{ {
namespace client namespace client
{ {
BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, ClientDestination * localDestination): BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination):
BOBI2PTunnel (localDestination), BOBI2PTunnel (localDestination),
m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (localDestination->GetService ()) m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (localDestination->GetService ())
{ {
@ -142,7 +142,7 @@ namespace client
} }
BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port, 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) m_Endpoint (boost::asio::ip::address::from_string (address), port), m_IsQuiet (quiet)
{ {
} }
@ -176,7 +176,7 @@ namespace client
} }
} }
BOBDestination::BOBDestination (ClientDestination& localDestination): BOBDestination::BOBDestination (std::shared_ptr<ClientDestination> localDestination):
m_LocalDestination (localDestination), m_LocalDestination (localDestination),
m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr) m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr)
{ {
@ -186,7 +186,7 @@ namespace client
{ {
delete m_OutboundTunnel; delete m_OutboundTunnel;
delete m_InboundTunnel; delete m_InboundTunnel;
i2p::client::context.DeleteLocalDestination (&m_LocalDestination); i2p::client::context.DeleteLocalDestination (m_LocalDestination);
} }
void BOBDestination::Start () void BOBDestination::Start ()
@ -198,7 +198,7 @@ namespace client
void BOBDestination::Stop () void BOBDestination::Stop ()
{ {
StopTunnels (); StopTunnels ();
m_LocalDestination.Stop (); m_LocalDestination->Stop ();
} }
void BOBDestination::StopTunnels () void BOBDestination::StopTunnels ()
@ -220,13 +220,13 @@ namespace client
void BOBDestination::CreateInboundTunnel (int port) void BOBDestination::CreateInboundTunnel (int port)
{ {
if (!m_InboundTunnel) 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) void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet)
{ {
if (!m_OutboundTunnel) 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): BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner):
@ -384,7 +384,7 @@ namespace client
LogPrint (eLogDebug, "BOB: start ", m_Nickname); LogPrint (eLogDebug, "BOB: start ", m_Nickname);
if (!m_CurrentDestination) 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); m_Owner.AddDestination (m_Nickname, m_CurrentDestination);
} }
if (m_InPort) if (m_InPort)

12
BOB.h

@ -46,7 +46,7 @@ namespace client
{ {
public: public:
BOBI2PTunnel (ClientDestination * localDestination): BOBI2PTunnel (std::shared_ptr<ClientDestination> localDestination):
I2PService (localDestination) {}; I2PService (localDestination) {};
virtual void Start () {}; virtual void Start () {};
@ -67,7 +67,7 @@ namespace client
public: public:
BOBI2PInboundTunnel (int port, ClientDestination * localDestination); BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination);
~BOBI2PInboundTunnel (); ~BOBI2PInboundTunnel ();
void Start (); void Start ();
@ -96,7 +96,7 @@ namespace client
{ {
public: 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 Start ();
void Stop (); void Stop ();
@ -119,7 +119,7 @@ namespace client
{ {
public: public:
BOBDestination (ClientDestination& localDestination); BOBDestination (std::shared_ptr<ClientDestination> localDestination);
~BOBDestination (); ~BOBDestination ();
void Start (); void Start ();
@ -127,11 +127,11 @@ namespace client
void StopTunnels (); void StopTunnels ();
void CreateInboundTunnel (int port); void CreateInboundTunnel (int port);
void CreateOutboundTunnel (const std::string& address, int port, bool quiet); 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: private:
ClientDestination& m_LocalDestination; std::shared_ptr<ClientDestination> m_LocalDestination;
BOBI2POutboundTunnel * m_OutboundTunnel; BOBI2POutboundTunnel * m_OutboundTunnel;
BOBI2PInboundTunnel * m_InboundTunnel; BOBI2PInboundTunnel * m_InboundTunnel;
}; };

28
ClientContext.cpp

@ -49,7 +49,7 @@ namespace client
std::string ircDestination = i2p::util::config::GetArg("-ircdest", ""); std::string ircDestination = i2p::util::config::GetArg("-ircdest", "");
if (ircDestination.length () > 0) // ircdest is presented if (ircDestination.length () > 0) // ircdest is presented
{ {
ClientDestination * localDestination = nullptr; std::shared_ptr<ClientDestination> localDestination = nullptr;
std::string ircKeys = i2p::util::config::GetArg("-irckeys", ""); std::string ircKeys = i2p::util::config::GetArg("-irckeys", "");
if (ircKeys.length () > 0) if (ircKeys.length () > 0)
localDestination = LoadLocalDestination (ircKeys, false); localDestination = LoadLocalDestination (ircKeys, false);
@ -146,15 +146,12 @@ namespace client
} }
for (auto it: m_Destinations) for (auto it: m_Destinations)
{
it.second->Stop (); it.second->Stop ();
delete it.second;
}
m_Destinations.clear (); 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; i2p::data::PrivateKeys keys;
std::string fullPath = i2p::util::filesystem::GetFullPath (filename); std::string fullPath = i2p::util::filesystem::GetFullPath (filename);
@ -184,7 +181,7 @@ namespace client
LogPrint ("New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ().GetIdentHash ()), " created"); 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); std::unique_lock<std::mutex> l(m_DestinationsMutex);
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
if (it != m_Destinations.end ()) if (it != m_Destinations.end ())
@ -194,25 +191,25 @@ namespace client
} }
else else
{ {
localDestination = new ClientDestination (keys, isPublic); localDestination = std::make_shared<ClientDestination> (keys, isPublic);
m_Destinations[localDestination->GetIdentHash ()] = localDestination; m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start (); localDestination->Start ();
} }
return localDestination; 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) const std::map<std::string, std::string> * params)
{ {
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); 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); std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination; m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start (); localDestination->Start ();
return localDestination; return localDestination;
} }
void ClientContext::DeleteLocalDestination (ClientDestination * destination) void ClientContext::DeleteLocalDestination (std::shared_ptr<ClientDestination> destination)
{ {
if (!destination) return; if (!destination) return;
auto it = m_Destinations.find (destination->GetIdentHash ()); auto it = m_Destinations.find (destination->GetIdentHash ());
@ -224,11 +221,10 @@ namespace client
m_Destinations.erase (it); m_Destinations.erase (it);
} }
d->Stop (); 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) const std::map<std::string, std::string> * params)
{ {
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
@ -242,14 +238,14 @@ namespace client
} }
return nullptr; 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); std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination; m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination;
localDestination->Start (); localDestination->Start ();
return localDestination; 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); auto it = m_Destinations.find (destination);
if (it != m_Destinations.end ()) if (it != m_Destinations.end ())
@ -299,7 +295,7 @@ namespace client
for (int i = 0; i < numClientTunnels; i++) for (int i = 0; i < numClientTunnels; i++)
{ {
ClientDestination * localDestination = nullptr; std::shared_ptr<ClientDestination> localDestination = nullptr;
if (keys[i].length () > 0) if (keys[i].length () > 0)
localDestination = LoadLocalDestination (keys[i], false); localDestination = LoadLocalDestination (keys[i], false);
auto clientTunnel = new I2PClientTunnel (destinations[i], ports[i], localDestination); auto clientTunnel = new I2PClientTunnel (destinations[i], ports[i], localDestination);

16
ClientContext.h

@ -37,14 +37,14 @@ namespace client
void Start (); void Start ();
void Stop (); void Stop ();
ClientDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; std::shared_ptr<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> 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 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); const std::map<std::string, std::string> * params = nullptr);
void DeleteLocalDestination (ClientDestination * destination); void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
ClientDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic); std::shared_ptr<ClientDestination> LoadLocalDestination (const std::string& filename, bool isPublic);
AddressBook& GetAddressBook () { return m_AddressBook; }; AddressBook& GetAddressBook () { return m_AddressBook; };
const SAMBridge * GetSAMBridge () const { return m_SamBridge; }; const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
@ -56,8 +56,8 @@ namespace client
private: private:
std::mutex m_DestinationsMutex; std::mutex m_DestinationsMutex;
std::map<i2p::data::IdentHash, ClientDestination *> m_Destinations; std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
ClientDestination * m_SharedLocalDestination; std::shared_ptr<ClientDestination> m_SharedLocalDestination;
AddressBook m_AddressBook; AddressBook m_AddressBook;

2
I2PService.cpp

@ -10,7 +10,7 @@ namespace client
{ {
static const i2p::data::SigningKeyType I2P_SERVICE_DEFAULT_KEY_TYPE = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256; 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 : m_LocalDestination (localDestination ? localDestination :
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)) i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE))
{ {

10
I2PService.h

@ -17,7 +17,7 @@ namespace client
class I2PService class I2PService
{ {
public: public:
I2PService (ClientDestination * localDestination = nullptr); I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr);
I2PService (i2p::data::SigningKeyType kt); I2PService (i2p::data::SigningKeyType kt);
virtual ~I2PService () { ClearHandlers (); } virtual ~I2PService () { ClearHandlers (); }
@ -37,8 +37,8 @@ namespace client
m_Handlers.clear(); m_Handlers.clear();
} }
inline ClientDestination * GetLocalDestination () { return m_LocalDestination; } inline std::shared_ptr<ClientDestination> GetLocalDestination () { return m_LocalDestination; }
inline void SetLocalDestination (ClientDestination * dest) { m_LocalDestination = dest; } inline void SetLocalDestination (std::shared_ptr<ClientDestination> dest) { m_LocalDestination = dest; }
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0); void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0);
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); } inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
@ -49,7 +49,7 @@ namespace client
virtual const char* GetName() { return "Generic I2P Service"; } virtual const char* GetName() { return "Generic I2P Service"; }
private: private:
ClientDestination * m_LocalDestination; std::shared_ptr<ClientDestination> m_LocalDestination;
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers; std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
std::mutex m_HandlersMutex; std::mutex m_HandlersMutex;
}; };
@ -81,7 +81,7 @@ namespace client
class TCPIPAcceptor: public I2PService class TCPIPAcceptor: public I2PService
{ {
public: public:
TCPIPAcceptor (int port, ClientDestination * localDestination = nullptr) : TCPIPAcceptor (int port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
I2PService(localDestination), I2PService(localDestination),
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_Timer (GetService ()) {} m_Timer (GetService ()) {}

4
I2PTunnel.cpp

@ -202,7 +202,7 @@ namespace client
Done(shared_from_this()); 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) TCPIPAcceptor (port,localDestination), m_Destination (destination), m_DestinationIdentHash (nullptr)
{} {}
@ -243,7 +243,7 @@ namespace client
return nullptr; 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) I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port)
{ {
} }

4
I2PTunnel.h

@ -65,7 +65,7 @@ namespace client
public: 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 () {} ~I2PClientTunnel () {}
void Start (); void Start ();
@ -83,7 +83,7 @@ namespace client
{ {
public: 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 Start ();
void Stop (); void Stop ();

4
SAM.cpp

@ -595,7 +595,7 @@ namespace client
LogPrint (eLogWarning, "Datagram size ", len," exceeds buffer"); LogPrint (eLogWarning, "Datagram size ", len," exceeds buffer");
} }
SAMSession::SAMSession (ClientDestination * dest): SAMSession::SAMSession (std::shared_ptr<ClientDestination> dest):
localDestination (dest) localDestination (dest)
{ {
} }
@ -700,7 +700,7 @@ namespace client
SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination, SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination,
const std::map<std::string, std::string> * params) const std::map<std::string, std::string> * params)
{ {
ClientDestination * localDestination = nullptr; std::shared_ptr<ClientDestination> localDestination = nullptr;
if (destination != "") if (destination != "")
{ {
i2p::data::PrivateKeys keys; i2p::data::PrivateKeys keys;

4
SAM.h

@ -128,10 +128,10 @@ namespace client
struct SAMSession struct SAMSession
{ {
ClientDestination * localDestination; std::shared_ptr<ClientDestination> localDestination;
std::list<std::shared_ptr<SAMSocket> > sockets; std::list<std::shared_ptr<SAMSocket> > sockets;
SAMSession (ClientDestination * localDestination); SAMSession (std::shared_ptr<ClientDestination> dest);
~SAMSession (); ~SAMSession ();
void CloseStreams (); void CloseStreams ();

Loading…
Cancel
Save