Browse Source

Move Stream creation to its own handler for cleanliness, it will hand over to a tunnel connection when done

pull/146/head
Francisco Blas (klondike) Izquierdo Riera 10 years ago
parent
commit
df3e8ce937
  1. 76
      I2PTunnel.cpp
  2. 1
      I2PTunnel.h

76
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<I2PClientTunnelHandler>
{
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<i2p::stream::Stream> 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<i2p::stream::Stream> stream)
{
if (stream)
{
if (Kill()) return;
LogPrint (eLogInfo,"New I2PTunnel connection");
auto connection = std::make_shared<I2PTunnelConnection>(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): I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination):
I2PService (localDestination), I2PService (localDestination),
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), 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(); const i2p::data::IdentHash *identHash = GetIdentHash();
if (identHash) if (identHash)
GetLocalDestination ()->CreateStream ( {
std::bind (&I2PClientTunnel::HandleStreamRequestComplete, auto connection = std::make_shared<I2PClientTunnelHandler>(this, *identHash, socket);
this, std::placeholders::_1, socket), *identHash); AddHandler (connection);
connection->Handle ();
}
else else
{ {
LogPrint (eLogError,"Closing socket"); LogPrint (eLogError,"Closing socket");
socket->close();
delete socket; delete socket;
} }
Accept (); Accept ();
@ -225,22 +279,6 @@ namespace client
} }
} }
void I2PClientTunnel::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream, boost::asio::ip::tcp::socket * socket)
{
if (stream)
{
LogPrint (eLogInfo,"New I2PTunnel connection");
auto connection = std::make_shared<I2PTunnelConnection>(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): I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination):
I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port) I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port)
{ {

1
I2PTunnel.h

@ -70,7 +70,6 @@ namespace client
const i2p::data::IdentHash * GetIdentHash (); const i2p::data::IdentHash * GetIdentHash ();
void Accept (); void Accept ();
void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket);
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream, boost::asio::ip::tcp::socket * socket);
private: private:

Loading…
Cancel
Save