File: TunnelBase.cpp

package info (click to toggle)
i2pd 2.58.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: cpp: 59,663; makefile: 224; sh: 138
file content (71 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*
*/

#include "Transports.h"
#include "TunnelBase.h"

namespace i2p
{
namespace tunnel
{
	void TunnelTransportSender::SendMessagesTo (const i2p::data::IdentHash& to,
		std::list<std::shared_ptr<I2NPMessage> >&& msgs)
	{
		if (msgs.empty ()) return;
		auto currentTransport = m_CurrentTransport.lock ();
		if (!currentTransport)
		{
			// try to obtain transport from pending request or send thought transport is not complete
			if (m_PendingTransport.valid ()) // pending request?
			{
				if (m_PendingTransport.wait_for(std::chrono::seconds(0)) == std::future_status::ready) 
				{	
					// pending request complete
					currentTransport = m_PendingTransport.get (); // take transports used in pending request
					if (currentTransport)
					{	
						if (currentTransport->IsEstablished ()) 
							m_CurrentTransport = currentTransport;
						else
							currentTransport = nullptr;
					}
				}	
				else // still pending
				{	
					// send through transports, but don't update pending transport
					i2p::transport::transports.SendMessages (to, std::move (msgs));
					return;
				}	
			}
		}
		if (currentTransport) // session is good
			// send to session directly
			currentTransport->SendI2NPMessages (msgs);
		else // no session yet
			// send through transports
			m_PendingTransport = i2p::transport::transports.SendMessages (to, std::move (msgs));

	}

	void TunnelTransportSender::SendMessagesTo (const i2p::data::IdentHash& to,
		std::list<std::shared_ptr<I2NPMessage> >& msgs)
	{
		std::list<std::shared_ptr<i2p::I2NPMessage> > msgs1;
		msgs.swap (msgs1);
		SendMessagesTo (to, std::move (msgs1));
	}	

	void TunnelTransportSender::Reset ()
	{
		m_CurrentTransport.reset ();
		if (m_PendingTransport.valid ())
			m_PendingTransport = std::future<std::shared_ptr<i2p::transport::TransportSession> >();
	}	
}
}