TunnelBase.cpp
1 /* 2 * Copyright (c) 2024, The PurpleI2P Project 3 * 4 * This file is part of Purple i2pd project and licensed under BSD3 5 * 6 * See full license text in LICENSE file at top of project tree 7 * 8 */ 9 10 #include "Transports.h" 11 #include "TunnelBase.h" 12 13 namespace i2p 14 { 15 namespace tunnel 16 { 17 void TunnelTransportSender::SendMessagesTo (const i2p::data::IdentHash& to, 18 std::list<std::shared_ptr<I2NPMessage> >&& msgs) 19 { 20 if (msgs.empty ()) return; 21 auto currentTransport = m_CurrentTransport.lock (); 22 if (!currentTransport) 23 { 24 // try to obtain transport from pending request or send thought transport is not complete 25 if (m_PendingTransport.valid ()) // pending request? 26 { 27 if (m_PendingTransport.wait_for(std::chrono::seconds(0)) == std::future_status::ready) 28 { 29 // pending request complete 30 currentTransport = m_PendingTransport.get (); // take transports used in pending request 31 if (currentTransport) 32 { 33 if (currentTransport->IsEstablished ()) 34 m_CurrentTransport = currentTransport; 35 else 36 currentTransport = nullptr; 37 } 38 } 39 else // still pending 40 { 41 // send through transports, but don't update pending transport 42 i2p::transport::transports.SendMessages (to, std::move (msgs)); 43 return; 44 } 45 } 46 } 47 if (currentTransport) // session is good 48 // send to session directly 49 currentTransport->SendI2NPMessages (msgs); 50 else // no session yet 51 // send through transports 52 m_PendingTransport = i2p::transport::transports.SendMessages (to, std::move (msgs)); 53 54 } 55 56 void TunnelTransportSender::SendMessagesTo (const i2p::data::IdentHash& to, 57 std::list<std::shared_ptr<I2NPMessage> >& msgs) 58 { 59 std::list<std::shared_ptr<i2p::I2NPMessage> > msgs1; 60 msgs.swap (msgs1); 61 SendMessagesTo (to, std::move (msgs1)); 62 } 63 64 void TunnelTransportSender::Reset () 65 { 66 m_CurrentTransport.reset (); 67 if (m_PendingTransport.valid ()) 68 m_PendingTransport = std::future<std::shared_ptr<i2p::transport::TransportSession> >(); 69 } 70 } 71 }