Browse Source

common ServiceAcceptor for all stream protocols

pull/2024/head
orignal 10 months ago
parent
commit
695dc96a83
  1. 49
      libi2pd_client/I2PService.cpp
  2. 87
      libi2pd_client/I2PService.h

49
libi2pd_client/I2PService.cpp

@ -147,54 +147,5 @@ namespace client
m_LocalDestination->CreateStream (streamRequestComplete, address->blindedPublicKey, port); m_LocalDestination->CreateStream (streamRequestComplete, address->blindedPublicKey, port);
} }
} }
void TCPIPAcceptor::Start ()
{
m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint));
// update the local end point in case port has been set zero and got updated now
m_LocalEndpoint = m_Acceptor->local_endpoint();
m_Acceptor->listen ();
Accept ();
}
void TCPIPAcceptor::Stop ()
{
if (m_Acceptor)
{
m_Acceptor->close();
m_Acceptor.reset (nullptr);
}
m_Timer.cancel ();
ClearHandlers();
}
void TCPIPAcceptor::Accept ()
{
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
m_Acceptor->async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this,
std::placeholders::_1, newSocket));
}
void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{
if (!ecode)
{
LogPrint(eLogDebug, "I2PService: ", GetName(), " accepted");
auto handler = CreateHandler(socket);
if (handler)
{
AddHandler(handler);
handler->Handle();
}
else
socket->close();
Accept();
}
else
{
if (ecode != boost::asio::error::operation_aborted)
LogPrint (eLogError, "I2PService: ", GetName(), " closing socket on accept because: ", ecode.message ());
}
}
} }
} }

87
libi2pd_client/I2PService.h

@ -210,41 +210,80 @@ namespace client
return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream); return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream);
} }
/* TODO: support IPv6 too */ //This is a service that listens for connections on the IP network or local socket and interacts with I2P
//This is a service that listens for connections on the IP network and interacts with I2P template<typename Protocol>
class TCPIPAcceptor: public I2PService class ServiceAcceptor: public I2PService
{ {
public: public:
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) : ServiceAcceptor (const typename Protocol::endpoint& localEndpoint, std::shared_ptr<ClientDestination> localDestination = nullptr) :
I2PService(localDestination), I2PService(localDestination), m_LocalEndpoint (localEndpoint) {}
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port),
m_Timer (GetService ()) {}
TCPIPAcceptor (const std::string& address, uint16_t port, i2p::data::SigningKeyType kt) :
I2PService(kt),
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port),
m_Timer (GetService ()) {}
virtual ~TCPIPAcceptor () { TCPIPAcceptor::Stop(); }
//If you override this make sure you call it from the children
void Start ();
//If you override this make sure you call it from the children
void Stop ();
const boost::asio::ip::tcp::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; }; virtual ~ServiceAcceptor () { Stop(); }
void Start () override
{
m_Acceptor.reset (new typename Protocol::acceptor (GetService (), m_LocalEndpoint));
// update the local end point in case port has been set zero and got updated now
m_LocalEndpoint = m_Acceptor->local_endpoint();
m_Acceptor->listen ();
Accept ();
}
void Stop () override
{
if (m_Acceptor)
{
m_Acceptor->close();
m_Acceptor.reset (nullptr);
}
ClearHandlers();
}
const typename Protocol::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; };
virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; } const char* GetName() override { return "Generic TCP/IP accepting daemon"; }
protected: protected:
virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0; virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<typename Protocol::socket> socket) = 0;
private:
void Accept()
{
auto newSocket = std::make_shared<typename Protocol::socket> (GetService ());
m_Acceptor->async_accept (*newSocket,
[newSocket, this](const boost::system::error_code& ecode)
{
if (ecode == boost::asio::error::operation_aborted) return;
if (!ecode)
{
LogPrint(eLogDebug, "ServiceAcceptor: ", GetName(), " accepted");
auto handler = CreateHandler(newSocket);
if (handler)
{
AddHandler(handler);
handler->Handle();
}
else
newSocket->close();
Accept();
}
else
LogPrint (eLogError, "ServiceAcceptor: ", GetName(), " closing socket on accept because: ", ecode.message ());
});
}
private: private:
void Accept(); typename Protocol::endpoint m_LocalEndpoint;
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket); std::unique_ptr<typename Protocol::acceptor> m_Acceptor;
boost::asio::ip::tcp::endpoint m_LocalEndpoint; };
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_Acceptor;
boost::asio::deadline_timer m_Timer; class TCPIPAcceptor: public ServiceAcceptor<boost::asio::ip::tcp>
{
public:
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
ServiceAcceptor (boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port), localDestination) {}
}; };
} }
} }

Loading…
Cancel
Save