From 7d9c0b76fcbadf2f86f5e30f58aa044902a1867c Mon Sep 17 00:00:00 2001 From: "Francisco Blas (klondike) Izquierdo Riera" Date: Thu, 8 Jan 2015 01:45:49 +0100 Subject: [PATCH] Make SOCKS use TCPIPAcceptor --- SOCKS.cpp | 36 ++---------------------------------- SOCKS.h | 26 +++++++------------------- 2 files changed, 9 insertions(+), 53 deletions(-) diff --git a/SOCKS.cpp b/SOCKS.cpp index 1cae4c83..ac30b6bd 100644 --- a/SOCKS.cpp +++ b/SOCKS.cpp @@ -514,41 +514,9 @@ namespace proxy } } - void SOCKSServer::Start () + std::shared_ptr SOCKSServer::CreateHandler(boost::asio::ip::tcp::socket * socket) { - m_Acceptor.listen (); - Accept (); - } - - void SOCKSServer::Stop () - { - m_Acceptor.close(); - m_Timer.cancel (); - ClearHandlers(); - } - - void SOCKSServer::Accept () - { - auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); - m_Acceptor.async_accept (*newSocket, std::bind (&SOCKSServer::HandleAccept, this, - std::placeholders::_1, newSocket)); - } - - void SOCKSServer::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket) - { - if (!ecode) - { - LogPrint(eLogDebug,"--- SOCKS accepted"); - auto handle = std::make_shared (this, socket); - AddHandler(handle); - handle->Handle(); - Accept(); - } - else - { - LogPrint (eLogError,"--- SOCKS Closing socket on accept because: ", ecode.message ()); - delete socket; - } + return std::make_shared (this, socket); } } diff --git a/SOCKS.h b/SOCKS.h index dc97bc0e..3d107f1c 100644 --- a/SOCKS.h +++ b/SOCKS.h @@ -11,28 +11,16 @@ namespace i2p { namespace proxy { - class SOCKSHandler; - class SOCKSServer: public i2p::client::I2PService + class SOCKSServer: public i2p::client::TCPIPAcceptor { - private: - std::set > m_Handlers; - boost::asio::ip::tcp::acceptor m_Acceptor; - boost::asio::deadline_timer m_Timer; - std::mutex m_HandlersMutex; - - private: - - void Accept(); - void HandleAccept(const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); + protected: + // Implements TCPIPAcceptor + std::shared_ptr CreateHandler(boost::asio::ip::tcp::socket * socket); + const char* GetName() { return "SOCKS"; } public: - SOCKSServer(int port) : I2PService(nullptr), - m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), - m_Timer (GetService ()) {}; - ~SOCKSServer() { Stop(); } - - void Start (); - void Stop (); + SOCKSServer(int port) : TCPIPAcceptor(port) {} + ~SOCKSServer() {} }; typedef SOCKSServer SOCKSProxy;