mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-11-14 05:20:10 +00:00
calculate RTT for server tunnel session. Common UDPConnection
This commit is contained in:
parent
c2556b9b9d
commit
5b8c5a5eff
2 changed files with 49 additions and 26 deletions
|
|
@ -54,6 +54,8 @@ namespace client
|
|||
nullptr, 0, m_LastSession->LocalPort, m_LastSession->RemotePort, &replyOptions); // Ack only, no payload
|
||||
}
|
||||
}
|
||||
if (options->Get (UDP_SESSION_ACKED, seqn))
|
||||
m_LastSession->Acked (seqn);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -146,6 +148,23 @@ namespace client
|
|||
return s;
|
||||
}
|
||||
|
||||
void UDPConnection::Acked (uint32_t seqn)
|
||||
{
|
||||
if (m_UnackedDatagrams.empty () && seqn < m_UnackedDatagrams.front ().first) return;
|
||||
auto it = m_UnackedDatagrams.begin ();
|
||||
while (it != m_UnackedDatagrams.end ())
|
||||
{
|
||||
if (it->first > seqn) break;
|
||||
if (it->first == seqn)
|
||||
{
|
||||
auto rtt = i2p::util::GetMillisecondsSinceEpoch () - it->second;
|
||||
m_RTT = m_RTT ? (m_RTT + rtt)/2 : rtt;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
m_UnackedDatagrams.erase (m_UnackedDatagrams.begin (), it);
|
||||
}
|
||||
|
||||
UDPSession::UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
|
||||
const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
|
||||
const boost::asio::ip::udp::endpoint& endpoint, const i2p::data::IdentHash& to,
|
||||
|
|
@ -153,7 +172,7 @@ namespace client
|
|||
m_Destination(localDestination->GetDatagramDestination()),
|
||||
IPSocket(localDestination->GetService(), localEndpoint), Identity (to),
|
||||
SendEndpoint(endpoint), LastActivity(i2p::util::GetMillisecondsSinceEpoch()),
|
||||
LocalPort(ourPort), RemotePort(theirPort), m_NextSendPacketNum (1), m_LastReceivedPacketNum (0)
|
||||
LocalPort(ourPort), RemotePort(theirPort)
|
||||
{
|
||||
IPSocket.set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU ));
|
||||
IPSocket.non_blocking (true);
|
||||
|
|
@ -174,14 +193,25 @@ namespace client
|
|||
LogPrint(eLogDebug, "UDPSession: Forward ", len, "B from ", FromEndpoint);
|
||||
auto ts = i2p::util::GetMillisecondsSinceEpoch();
|
||||
auto session = GetDatagramSession ();
|
||||
if (ts > LastActivity + I2P_UDP_REPLIABLE_DATAGRAM_INTERVAL)
|
||||
uint64_t repliableDatagramInterval = I2P_UDP_REPLIABLE_DATAGRAM_INTERVAL;
|
||||
if (m_RTT >= 10) repliableDatagramInterval = m_RTT/10; // 1 ms min
|
||||
if (ts > LastActivity + repliableDatagramInterval)
|
||||
{
|
||||
if (session->GetVersion () == i2p::datagram::eDatagramV3)
|
||||
{
|
||||
uint8_t flags = 0;
|
||||
if (!m_RTT || (!m_UnackedDatagrams.empty () &&
|
||||
ts > m_UnackedDatagrams.back ().second + repliableDatagramInterval)) // last ack request
|
||||
{
|
||||
flags |= UDP_SESSION_FLAG_ACK_REQUESTED;
|
||||
m_UnackedDatagrams.push_back ({ m_NextSendPacketNum, ts });
|
||||
}
|
||||
i2p::util::Mapping options;
|
||||
options.Put (UDP_SESSION_SEQN, m_NextSendPacketNum);
|
||||
if (m_LastReceivedPacketNum > 0)
|
||||
options.Put (UDP_SESSION_ACKED, m_LastReceivedPacketNum);
|
||||
if (flags)
|
||||
options.Put (UDP_SESSION_FLAGS, flags);
|
||||
m_Destination->SendDatagram(session, m_Buffer, len, LocalPort, RemotePort, &options);
|
||||
}
|
||||
else
|
||||
|
|
@ -288,7 +318,6 @@ namespace client
|
|||
m_Name (name), m_RemoteDest (remoteDest), m_LocalDest (localDestination), m_LocalEndpoint (localEndpoint),
|
||||
m_ResolveThread (nullptr), m_LocalSocket (nullptr), RemotePort (remotePort),
|
||||
m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip), m_DatagramVersion (datagramVersion),
|
||||
m_NextSendPacketNum (1), m_LastReceivedPacketNum (0), m_RTT (0),
|
||||
m_AckTimer (localDestination->GetService ()), m_AckTimerSeqn (0)
|
||||
{
|
||||
}
|
||||
|
|
@ -546,19 +575,7 @@ namespace client
|
|||
m_AckTimerSeqn = 0;
|
||||
m_AckTimer.cancel ();
|
||||
}
|
||||
if (m_UnackedDatagrams.empty () && seqn < m_UnackedDatagrams.front ().first) return;
|
||||
auto it = m_UnackedDatagrams.begin ();
|
||||
while (it != m_UnackedDatagrams.end ())
|
||||
{
|
||||
if (it->first > seqn) break;
|
||||
if (it->first == seqn)
|
||||
{
|
||||
auto rtt = i2p::util::GetMillisecondsSinceEpoch () - it->second;
|
||||
m_RTT = m_RTT ? (m_RTT + rtt)/2 : rtt;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
m_UnackedDatagrams.erase (m_UnackedDatagrams.begin (), it);
|
||||
UDPConnection::Acked (seqn);
|
||||
}
|
||||
|
||||
void I2PUDPClientTunnel::ScheduleAckTimer (uint32_t seqn)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,16 @@ namespace client
|
|||
/** max size for i2p udp */
|
||||
const size_t I2P_UDP_MAX_MTU = 64*1024;
|
||||
|
||||
struct UDPSession // for server side
|
||||
struct UDPConnection
|
||||
{
|
||||
uint32_t m_NextSendPacketNum = 1, m_LastReceivedPacketNum = 0;
|
||||
std::list<std::pair<uint32_t, uint64_t> > m_UnackedDatagrams; // list of sent but not acked repliable datagrams(seqn, timestamp) in ascending order
|
||||
uint64_t m_RTT = 0; // milliseconds
|
||||
|
||||
virtual void Acked (uint32_t seqn);
|
||||
};
|
||||
|
||||
struct UDPSession: public UDPConnection // for server side
|
||||
{
|
||||
i2p::datagram::DatagramDestination * m_Destination;
|
||||
std::weak_ptr<i2p::datagram::DatagramSession> m_LastDatagramSession;
|
||||
|
|
@ -50,7 +59,6 @@ namespace client
|
|||
uint16_t RemotePort;
|
||||
|
||||
uint8_t m_Buffer[I2P_UDP_MAX_MTU];
|
||||
uint32_t m_NextSendPacketNum, m_LastReceivedPacketNum;
|
||||
|
||||
UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
|
||||
const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
|
||||
|
|
@ -132,7 +140,7 @@ namespace client
|
|||
bool isUpdated; // transient, used during reload only
|
||||
};
|
||||
|
||||
class I2PUDPClientTunnel
|
||||
class I2PUDPClientTunnel: public UDPConnection
|
||||
{
|
||||
public:
|
||||
|
||||
|
|
@ -168,7 +176,7 @@ namespace client
|
|||
void HandleRecvFromI2PRaw (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||||
void TryResolving ();
|
||||
std::shared_ptr<i2p::datagram::DatagramSession> GetDatagramSession ();
|
||||
void Acked (uint32_t seqn);
|
||||
void Acked (uint32_t seqn) override;
|
||||
void ScheduleAckTimer (uint32_t seqn);
|
||||
|
||||
private:
|
||||
|
|
@ -190,9 +198,7 @@ namespace client
|
|||
i2p::datagram::DatagramVersion m_DatagramVersion;
|
||||
std::shared_ptr<UDPConvo> m_LastSession;
|
||||
std::weak_ptr<i2p::datagram::DatagramSession> m_LastDatagramSession;
|
||||
uint32_t m_NextSendPacketNum, m_LastReceivedPacketNum;
|
||||
std::list<std::pair<uint32_t, uint64_t> > m_UnackedDatagrams; // list of sent but not acked repliable datagrams(seqn, timestamp) in ascending order
|
||||
uint64_t m_RTT; // milliseconds
|
||||
|
||||
boost::asio::deadline_timer m_AckTimer;
|
||||
uint32_t m_AckTimerSeqn;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue