1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-22 04:04:16 +00:00

Make I2PClientTunnel use TCPIPAcceptor

This commit is contained in:
Francisco Blas (klondike) Izquierdo Riera 2015-01-08 03:49:35 +01:00
parent df3e8ce937
commit 56014962d4
4 changed files with 17 additions and 54 deletions

View File

@ -11,7 +11,6 @@ namespace i2p
{ {
namespace proxy namespace proxy
{ {
class HTTPProxyHandler;
class HTTPProxyServer: public i2p::client::TCPIPAcceptor class HTTPProxyServer: public i2p::client::TCPIPAcceptor
{ {
protected: protected:

View File

@ -76,6 +76,7 @@ namespace client
std::atomic<bool> m_Dead; //To avoid cleaning up multiple times std::atomic<bool> 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 //This is a service that listens for connections on the IP network and interacts with I2P
class TCPIPAcceptor: public I2PService class TCPIPAcceptor: public I2PService
{ {

View File

@ -203,29 +203,17 @@ namespace client
} }
I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination): I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination):
I2PService (localDestination), TCPIPAcceptor (port,localDestination), 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)
{
}
I2PClientTunnel::~I2PClientTunnel ()
{
Stop ();
}
void I2PClientTunnel::Start () void I2PClientTunnel::Start ()
{ {
GetIdentHash(); GetIdentHash();
m_Acceptor.listen ();
Accept ();
} }
void I2PClientTunnel::Stop () void I2PClientTunnel::Stop ()
{ {
m_Acceptor.close(); TCPIPAcceptor::Stop();
m_Timer.cancel ();
ClearHandlers ();
auto *originalIdentHash = m_DestinationIdentHash; auto *originalIdentHash = m_DestinationIdentHash;
m_DestinationIdentHash = nullptr; m_DestinationIdentHash = nullptr;
delete originalIdentHash; delete originalIdentHash;
@ -245,38 +233,13 @@ namespace client
return m_DestinationIdentHash; return m_DestinationIdentHash;
} }
std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(boost::asio::ip::tcp::socket * socket)
void I2PClientTunnel::Accept ()
{ {
auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); const i2p::data::IdentHash *identHash = GetIdentHash();
m_Acceptor.async_accept (*newSocket, std::bind (&I2PClientTunnel::HandleAccept, this, if (identHash)
std::placeholders::_1, newSocket)); return std::make_shared<I2PClientTunnelHandler>(this, *identHash, socket);
}
void I2PClientTunnel::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
{
if (!ecode)
{
const i2p::data::IdentHash *identHash = GetIdentHash();
if (identHash)
{
auto connection = std::make_shared<I2PClientTunnelHandler>(this, *identHash, socket);
AddHandler (connection);
connection->Handle ();
}
else
{
LogPrint (eLogError,"Closing socket");
socket->close();
delete socket;
}
Accept ();
}
else else
{ return nullptr;
LogPrint (eLogError,"Closing socket on accept because: ", ecode.message ());
delete socket;
}
} }
I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination): I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination):

View File

@ -55,12 +55,18 @@ namespace client
bool m_IsQuiet; // don't send destination bool m_IsQuiet; // don't send destination
}; };
class I2PClientTunnel: public I2PService class I2PClientTunnel: public TCPIPAcceptor
{ {
protected:
// Implements TCPIPAcceptor
std::shared_ptr<I2PServiceHandler> CreateHandler(boost::asio::ip::tcp::socket * socket);
const char* GetName() { return "I2P Client Tunnel"; }
public: public:
I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr); I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr);
~I2PClientTunnel (); ~I2PClientTunnel () {}
void Start (); void Start ();
void Stop (); void Stop ();
@ -68,13 +74,7 @@ namespace client
private: private:
const i2p::data::IdentHash * GetIdentHash (); 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; std::string m_Destination;
const i2p::data::IdentHash * m_DestinationIdentHash; const i2p::data::IdentHash * m_DestinationIdentHash;
}; };