From df3e8ce93708a26a5772014cf50450b899e3a76c Mon Sep 17 00:00:00 2001 From: "Francisco Blas (klondike) Izquierdo Riera" Date: Thu, 8 Jan 2015 03:28:54 +0100 Subject: [PATCH] Move Stream creation to its own handler for cleanliness, it will hand over to a tunnel connection when done --- I2PTunnel.cpp | 76 ++++++++++++++++++++++++++++++++++++++------------- I2PTunnel.h | 1 - 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 2f96b859..6771abd5 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -151,6 +151,57 @@ namespace client } } + /* This handler tries to stablish a connection with the desired server and dies if it fails to do so */ + class I2PClientTunnelHandler: public I2PServiceHandler, public std::enable_shared_from_this + { + public: + I2PClientTunnelHandler (I2PClientTunnel * parent, i2p::data::IdentHash destination, + boost::asio::ip::tcp::socket * socket): + I2PServiceHandler(parent), m_DestinationIdentHash(destination), m_Socket(socket) {} + void Handle(); + void Terminate(); + private: + void HandleStreamRequestComplete (std::shared_ptr stream); + i2p::data::IdentHash m_DestinationIdentHash; + boost::asio::ip::tcp::socket * m_Socket; + }; + + void I2PClientTunnelHandler::Handle() + { + GetOwner()->GetLocalDestination ()->CreateStream (std::bind (&I2PClientTunnelHandler::HandleStreamRequestComplete, + shared_from_this(), std::placeholders::_1), m_DestinationIdentHash); + } + + void I2PClientTunnelHandler::HandleStreamRequestComplete (std::shared_ptr stream) + { + if (stream) + { + if (Kill()) return; + LogPrint (eLogInfo,"New I2PTunnel connection"); + auto connection = std::make_shared(GetOwner(), m_Socket, stream); + GetOwner()->AddHandler (connection); + connection->I2PConnect (); + Done(shared_from_this()); + } + else + { + LogPrint (eLogError,"I2P Client Tunnel Issue when creating the stream, check the previous warnings for more info."); + Terminate(); + } + } + + void I2PClientTunnelHandler::Terminate() + { + if (Kill()) return; + if (m_Socket) + { + m_Socket->close(); + delete m_Socket; + m_Socket = nullptr; + } + Done(shared_from_this()); + } + I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination): I2PService (localDestination), m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), @@ -208,12 +259,15 @@ namespace client { const i2p::data::IdentHash *identHash = GetIdentHash(); if (identHash) - GetLocalDestination ()->CreateStream ( - std::bind (&I2PClientTunnel::HandleStreamRequestComplete, - this, std::placeholders::_1, socket), *identHash); + { + auto connection = std::make_shared(this, *identHash, socket); + AddHandler (connection); + connection->Handle (); + } else { LogPrint (eLogError,"Closing socket"); + socket->close(); delete socket; } Accept (); @@ -225,22 +279,6 @@ namespace client } } - void I2PClientTunnel::HandleStreamRequestComplete (std::shared_ptr stream, boost::asio::ip::tcp::socket * socket) - { - if (stream) - { - LogPrint (eLogInfo,"New I2PTunnel connection"); - auto connection = std::make_shared(this, socket, stream); - AddHandler (connection); - connection->I2PConnect (); - } - else - { - LogPrint (eLogError,"Issue when creating the stream, check the previous warnings for more info."); - delete socket; - } - } - I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination): I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port) { diff --git a/I2PTunnel.h b/I2PTunnel.h index f19f128c..374fa107 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -70,7 +70,6 @@ namespace client const i2p::data::IdentHash * GetIdentHash (); void Accept (); void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); - void HandleStreamRequestComplete (std::shared_ptr stream, boost::asio::ip::tcp::socket * socket); private: