From 4d640dac2aabf9b1201585947c4c244759ab2d4f Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 29 Dec 2014 14:29:55 -0500 Subject: [PATCH] always use destination's thread to avoid race conditions --- BOB.cpp | 22 ++++++++++------------ BOB.h | 12 +++++------- ClientContext.cpp | 8 +++----- I2PTunnel.cpp | 14 ++++++-------- I2PTunnel.h | 13 +++++-------- 5 files changed, 29 insertions(+), 40 deletions(-) diff --git a/BOB.cpp b/BOB.cpp index d4c9eba3..ea902866 100644 --- a/BOB.cpp +++ b/BOB.cpp @@ -8,10 +8,9 @@ namespace i2p { namespace client { - BOBI2PInboundTunnel::BOBI2PInboundTunnel (boost::asio::io_service& service, int port, ClientDestination * localDestination): - BOBI2PTunnel (service, localDestination), - m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), - m_Timer (service) + BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, ClientDestination * localDestination): + BOBI2PTunnel (localDestination), + m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (localDestination->GetService ()) { } @@ -142,8 +141,8 @@ namespace client delete receiver; } - BOBI2POutboundTunnel::BOBI2POutboundTunnel (boost::asio::io_service& service, const std::string& address, int port, - ClientDestination * localDestination, bool quiet): BOBI2PTunnel (service, localDestination), + BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port, + ClientDestination * localDestination, bool quiet): BOBI2PTunnel (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port), m_IsQuiet (quiet) { } @@ -177,8 +176,8 @@ namespace client } } - BOBDestination::BOBDestination (boost::asio::io_service& service, ClientDestination& localDestination): - m_Service (service), m_LocalDestination (localDestination), + BOBDestination::BOBDestination (ClientDestination& localDestination): + m_LocalDestination (localDestination), m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr) { } @@ -220,13 +219,13 @@ namespace client void BOBDestination::CreateInboundTunnel (int port) { if (!m_InboundTunnel) - m_InboundTunnel = new BOBI2PInboundTunnel (m_Service, 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 (m_Service, address, port, &m_LocalDestination, quiet); + m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, &m_LocalDestination, quiet); } BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner): @@ -384,8 +383,7 @@ namespace client LogPrint (eLogDebug, "BOB: start ", m_Nickname); if (!m_CurrentDestination) { - m_CurrentDestination = new BOBDestination (m_Owner.GetService (), - *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) diff --git a/BOB.h b/BOB.h index f8921665..fc961137 100644 --- a/BOB.h +++ b/BOB.h @@ -45,8 +45,8 @@ namespace client { public: - BOBI2PTunnel (boost::asio::io_service& service, ClientDestination * localDestination): - I2PTunnel (service, localDestination) {}; + BOBI2PTunnel (ClientDestination * localDestination): + I2PTunnel (localDestination) {}; virtual void Start () {}; virtual void Stop () {}; @@ -66,7 +66,7 @@ namespace client public: - BOBI2PInboundTunnel (boost::asio::io_service& service, int port, ClientDestination * localDestination); + BOBI2PInboundTunnel (int port, ClientDestination * localDestination); ~BOBI2PInboundTunnel (); void Start (); @@ -95,8 +95,7 @@ namespace client { public: - BOBI2POutboundTunnel (boost::asio::io_service& service, const std::string& address, int port, - ClientDestination * localDestination, bool quiet); + BOBI2POutboundTunnel (const std::string& address, int port, ClientDestination * localDestination, bool quiet); void Start (); void Stop (); @@ -119,7 +118,7 @@ namespace client { public: - BOBDestination (boost::asio::io_service& service, ClientDestination& localDestination); + BOBDestination (ClientDestination& localDestination); ~BOBDestination (); void Start (); @@ -131,7 +130,6 @@ namespace client private: - boost::asio::io_service& m_Service; ClientDestination& m_LocalDestination; BOBI2POutboundTunnel * m_OutboundTunnel; BOBI2PInboundTunnel * m_InboundTunnel; diff --git a/ClientContext.cpp b/ClientContext.cpp index c4700e0e..46bace37 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -48,8 +48,7 @@ namespace client std::string ircKeys = i2p::util::config::GetArg("-irckeys", ""); if (ircKeys.length () > 0) localDestination = LoadLocalDestination (ircKeys, false); - m_IrcTunnel = new I2PClientTunnel (m_SocksProxy->GetService (), ircDestination, - i2p::util::config::GetArg("-ircport", 6668), localDestination); + m_IrcTunnel = new I2PClientTunnel (ircDestination, i2p::util::config::GetArg("-ircport", 6668), localDestination); m_IrcTunnel->Start (); LogPrint("IRC tunnel started"); } @@ -57,9 +56,8 @@ namespace client if (eepKeys.length () > 0) // eepkeys file is presented { auto localDestination = LoadLocalDestination (eepKeys, true); - m_ServerTunnel = new I2PServerTunnel (m_SocksProxy->GetService (), - i2p::util::config::GetArg("-eephost", "127.0.0.1"), i2p::util::config::GetArg("-eepport", 80), - localDestination); + m_ServerTunnel = new I2PServerTunnel (i2p::util::config::GetArg("-eephost", "127.0.0.1"), + i2p::util::config::GetArg("-eepport", 80), localDestination); m_ServerTunnel->Start (); LogPrint("Server tunnel started"); } diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 46e1b4bb..ac372112 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -154,12 +154,11 @@ namespace client m_Connections.clear (); } - I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, - int port, ClientDestination * localDestination): - I2PTunnel (service, localDestination ? localDestination : + I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination): + I2PTunnel (localDestination ? localDestination : i2p::client::context.CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256)), - m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), - m_Timer (service), m_Destination (destination), m_DestinationIdentHash (nullptr), + m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), + m_Timer (GetService ()), m_Destination (destination), m_DestinationIdentHash (nullptr), m_RemoteLeaseSet (nullptr) { } @@ -260,9 +259,8 @@ namespace client } } - I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, - ClientDestination * localDestination): I2PTunnel (service, localDestination), - m_Endpoint (boost::asio::ip::address::from_string (address), port) + I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination): + I2PTunnel (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port) { } diff --git a/I2PTunnel.h b/I2PTunnel.h index b63b4a9f..5df769b6 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -58,8 +58,8 @@ namespace client { public: - I2PTunnel (boost::asio::io_service& service, ClientDestination * localDestination): - m_Service (service), m_LocalDestination (localDestination) {}; + I2PTunnel (ClientDestination * localDestination): + m_LocalDestination (localDestination) {}; virtual ~I2PTunnel () { ClearConnections (); }; void AddConnection (std::shared_ptr conn); @@ -68,11 +68,10 @@ namespace client ClientDestination * GetLocalDestination () { return m_LocalDestination; }; void SetLocalDestination (ClientDestination * dest) { m_LocalDestination = dest; }; - boost::asio::io_service& GetService () { return m_Service; }; + boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }; private: - boost::asio::io_service& m_Service; ClientDestination * m_LocalDestination; std::set > m_Connections; }; @@ -81,8 +80,7 @@ namespace client { public: - I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, int port, - ClientDestination * localDestination = nullptr); + I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr); ~I2PClientTunnel (); void Start (); @@ -108,8 +106,7 @@ namespace client { public: - I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, - ClientDestination * localDestination); + I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination); void Start (); void Stop ();