mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-31 04:44:13 +00:00
NTCP2 acceptors
This commit is contained in:
parent
1a38e925bf
commit
998653ea9d
@ -776,6 +776,48 @@ namespace transport
|
|||||||
{
|
{
|
||||||
m_IsRunning = true;
|
m_IsRunning = true;
|
||||||
m_Thread = new std::thread (std::bind (&NTCP2Server::Run, this));
|
m_Thread = new std::thread (std::bind (&NTCP2Server::Run, this));
|
||||||
|
auto& addresses = context.GetRouterInfo ().GetAddresses ();
|
||||||
|
for (const auto& address: addresses)
|
||||||
|
{
|
||||||
|
if (!address) continue;
|
||||||
|
if (address->IsPublishedNTCP2 ())
|
||||||
|
{
|
||||||
|
if (address->host.is_v4())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_NTCP2Acceptor.reset (new boost::asio::ip::tcp::acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port)));
|
||||||
|
}
|
||||||
|
catch ( std::exception & ex )
|
||||||
|
{
|
||||||
|
LogPrint(eLogError, "NTCP2: Failed to bind to ip4 port ",address->port, ex.what());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrint (eLogInfo, "NTC2P: Start listening TCP port ", address->port);
|
||||||
|
auto conn = std::make_shared<NTCP2Session>(*this);
|
||||||
|
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this, conn, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
else if (address->host.is_v6() && context.SupportsV6 ())
|
||||||
|
{
|
||||||
|
m_NTCP2V6Acceptor.reset (new boost::asio::ip::tcp::acceptor (m_Service));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_NTCP2V6Acceptor->open (boost::asio::ip::tcp::v6());
|
||||||
|
m_NTCP2V6Acceptor->set_option (boost::asio::ip::v6_only (true));
|
||||||
|
m_NTCP2V6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port));
|
||||||
|
m_NTCP2V6Acceptor->listen ();
|
||||||
|
|
||||||
|
LogPrint (eLogInfo, "NTCP2: Start listening V6 TCP port ", address->port);
|
||||||
|
auto conn = std::make_shared<NTCP2Session> (*this);
|
||||||
|
m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this, conn, std::placeholders::_1));
|
||||||
|
} catch ( std::exception & ex ) {
|
||||||
|
LogPrint(eLogError, "NTCP: failed to bind to ip6 port ", address->port);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -786,6 +828,8 @@ namespace transport
|
|||||||
auto ntcpSessions = m_NTCP2Sessions;
|
auto ntcpSessions = m_NTCP2Sessions;
|
||||||
for (auto& it: ntcpSessions)
|
for (auto& it: ntcpSessions)
|
||||||
it.second->Terminate ();
|
it.second->Terminate ();
|
||||||
|
for (auto& it: m_PendingIncomingSessions)
|
||||||
|
it->Terminate ();
|
||||||
}
|
}
|
||||||
m_NTCP2Sessions.clear ();
|
m_NTCP2Sessions.clear ();
|
||||||
|
|
||||||
@ -871,6 +915,60 @@ namespace transport
|
|||||||
conn->ClientLogin ();
|
conn->ClientLogin ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NTCP2Server::HandleAccept (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error)
|
||||||
|
{
|
||||||
|
if (!error)
|
||||||
|
{
|
||||||
|
boost::system::error_code ec;
|
||||||
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
||||||
|
if (!ec)
|
||||||
|
{
|
||||||
|
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
|
||||||
|
if (conn)
|
||||||
|
{
|
||||||
|
conn->ServerLogin ();
|
||||||
|
// m_PendingIncomingSessions.push_back (conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LogPrint (eLogError, "NTCP2: Connected from error ", ec.message ());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error != boost::asio::error::operation_aborted)
|
||||||
|
{
|
||||||
|
conn = std::make_shared<NTCP2Session> (*this);
|
||||||
|
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this,
|
||||||
|
conn, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NTCP2Server::HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error)
|
||||||
|
{
|
||||||
|
if (!error)
|
||||||
|
{
|
||||||
|
boost::system::error_code ec;
|
||||||
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
||||||
|
if (!ec)
|
||||||
|
{
|
||||||
|
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
|
||||||
|
if (conn)
|
||||||
|
{
|
||||||
|
conn->ServerLogin ();
|
||||||
|
// m_PendingIncomingSessions.push_back (conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LogPrint (eLogError, "NTCP2: Connected from error ", ec.message ());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error != boost::asio::error::operation_aborted)
|
||||||
|
{
|
||||||
|
conn = std::make_shared<NTCP2Session> (*this);
|
||||||
|
m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this,
|
||||||
|
conn, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,6 +155,9 @@ namespace transport
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
|
void HandleAccept (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error);
|
||||||
|
void HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error);
|
||||||
|
|
||||||
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn);
|
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -163,7 +166,9 @@ namespace transport
|
|||||||
std::thread * m_Thread;
|
std::thread * m_Thread;
|
||||||
boost::asio::io_service m_Service;
|
boost::asio::io_service m_Service;
|
||||||
boost::asio::io_service::work m_Work;
|
boost::asio::io_service::work m_Work;
|
||||||
|
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_NTCP2Acceptor, m_NTCP2V6Acceptor;
|
||||||
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
|
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
|
||||||
|
std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user