diff --git a/SSU.cpp b/SSU.cpp index 3ee9c69f..17aa0b2d 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -284,34 +284,39 @@ namespace transport auto address = router->GetSSUAddress (!context.SupportsV6 ()); if (address) { - std::shared_ptr session; boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port); - auto it = m_Sessions.find (remoteEndpoint); - if (it != m_Sessions.end ()) - { - session = it->second; - if (peerTest && session->GetState () == eSessionStateEstablished) - session->SendPeerTest (); - } - else - { - // otherwise create new session - session = std::make_shared (*this, remoteEndpoint, router, peerTest); - { - std::unique_lock l(m_SessionsMutex); - m_Sessions[remoteEndpoint] = session; - } - // connect - LogPrint ("Creating new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ", - remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ()); - session->Connect (); - } + auto& s = remoteEndpoint.address ().is_v6 () ? m_ServiceV6 : m_Service; + s.post (std::bind (&SSUServer::CreateDirectSession, this, router, remoteEndpoint, peerTest)); } else LogPrint (eLogWarning, "Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address"); } } + void SSUServer::CreateDirectSession (std::shared_ptr router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest) + { + auto it = m_Sessions.find (remoteEndpoint); + if (it != m_Sessions.end ()) + { + auto session = it->second; + if (peerTest && session->GetState () == eSessionStateEstablished) + session->SendPeerTest (); + } + else + { + // otherwise create new session + auto session = std::make_shared (*this, remoteEndpoint, router, peerTest); + { + std::unique_lock l(m_SessionsMutex); + m_Sessions[remoteEndpoint] = session; + } + // connect + LogPrint ("Creating new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ", + remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ()); + session->Connect (); + } + } + void SSUServer::CreateSessionThroughIntroducer (std::shared_ptr router, bool peerTest) { if (router && router->UsesIntroducer ()) diff --git a/SSU.h b/SSU.h index 96b9199d..08f54642 100644 --- a/SSU.h +++ b/SSU.h @@ -41,7 +41,6 @@ namespace transport void Start (); void Stop (); void CreateSession (std::shared_ptr router, bool peerTest = false); - void CreateSessionThroughIntroducer (std::shared_ptr router, bool peerTest = false); std::shared_ptr FindSession (std::shared_ptr router) const; std::shared_ptr FindSession (const boost::asio::ip::udp::endpoint& e) const; std::shared_ptr GetRandomEstablishedSession (std::shared_ptr excluded); @@ -72,6 +71,8 @@ namespace transport void HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet); void HandleReceivedPackets (std::vector packets); + void CreateSessionThroughIntroducer (std::shared_ptr router, bool peerTest = false); + void CreateDirectSession (std::shared_ptr router, boost::asio::ip::udp::endpoint remoteEndpoint, bool peerTest); template std::shared_ptr GetRandomSession (Filter filter);