diff --git a/HTTPProxy.h b/HTTPProxy.h index 4558c88f..a7b81553 100644 --- a/HTTPProxy.h +++ b/HTTPProxy.h @@ -11,7 +11,6 @@ namespace i2p { namespace proxy { - class HTTPProxyHandler; class HTTPProxyServer: public i2p::client::TCPIPAcceptor { protected: diff --git a/I2PService.h b/I2PService.h index 636a9780..a03f16ed 100644 --- a/I2PService.h +++ b/I2PService.h @@ -76,6 +76,7 @@ namespace client std::atomic m_Dead; //To avoid cleaning up multiple times }; + /* TODO: support IPv6 too */ //This is a service that listens for connections on the IP network and interacts with I2P class TCPIPAcceptor: public I2PService { diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 6771abd5..fe59b7d7 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -203,29 +203,17 @@ namespace client } 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)), - m_Timer (GetService ()), m_Destination (destination), m_DestinationIdentHash (nullptr) - { - } + TCPIPAcceptor (port,localDestination), m_Destination (destination), m_DestinationIdentHash (nullptr) + {} - I2PClientTunnel::~I2PClientTunnel () - { - Stop (); - } - void I2PClientTunnel::Start () { GetIdentHash(); - m_Acceptor.listen (); - Accept (); } void I2PClientTunnel::Stop () { - m_Acceptor.close(); - m_Timer.cancel (); - ClearHandlers (); + TCPIPAcceptor::Stop(); auto *originalIdentHash = m_DestinationIdentHash; m_DestinationIdentHash = nullptr; delete originalIdentHash; @@ -245,38 +233,13 @@ namespace client return m_DestinationIdentHash; } - - void I2PClientTunnel::Accept () - { - auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); - m_Acceptor.async_accept (*newSocket, std::bind (&I2PClientTunnel::HandleAccept, this, - std::placeholders::_1, newSocket)); - } - - void I2PClientTunnel::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket) + std::shared_ptr I2PClientTunnel::CreateHandler(boost::asio::ip::tcp::socket * socket) { - if (!ecode) - { - const i2p::data::IdentHash *identHash = GetIdentHash(); - if (identHash) - { - auto connection = std::make_shared(this, *identHash, socket); - AddHandler (connection); - connection->Handle (); - } - else - { - LogPrint (eLogError,"Closing socket"); - socket->close(); - delete socket; - } - Accept (); - } + const i2p::data::IdentHash *identHash = GetIdentHash(); + if (identHash) + return std::make_shared(this, *identHash, socket); else - { - LogPrint (eLogError,"Closing socket on accept because: ", ecode.message ()); - delete socket; - } + return nullptr; } I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination): diff --git a/I2PTunnel.h b/I2PTunnel.h index 374fa107..010f4843 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -55,12 +55,18 @@ namespace client bool m_IsQuiet; // don't send destination }; - class I2PClientTunnel: public I2PService + class I2PClientTunnel: public TCPIPAcceptor { + protected: + + // Implements TCPIPAcceptor + std::shared_ptr CreateHandler(boost::asio::ip::tcp::socket * socket); + const char* GetName() { return "I2P Client Tunnel"; } + public: I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr); - ~I2PClientTunnel (); + ~I2PClientTunnel () {} void Start (); void Stop (); @@ -68,13 +74,7 @@ namespace client private: const i2p::data::IdentHash * GetIdentHash (); - void Accept (); - void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); - - private: - boost::asio::ip::tcp::acceptor m_Acceptor; - boost::asio::deadline_timer m_Timer; std::string m_Destination; const i2p::data::IdentHash * m_DestinationIdentHash; };