Skip to content

Commit

Permalink
don't send Ack with NACK immediately but after 2 milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 27, 2024
1 parent 75c2cb7 commit 530c353
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
37 changes: 16 additions & 21 deletions libi2pd/Streaming.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023, The PurpleI2P Project
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -179,13 +179,9 @@ namespace stream
{
if (!m_IsAckSendScheduled)
{
m_IsAckSendScheduled = true;
auto ackTimeout = m_RTT/10;
if (ackTimeout > m_AckDelay) ackTimeout = m_AckDelay;
else if (ackTimeout < MIN_SEND_ACK_TIMEOUT) ackTimeout = MIN_SEND_ACK_TIMEOUT;
m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(ackTimeout));
m_AckSendTimer.async_wait (std::bind (&Stream::HandleAckSendTimer,
shared_from_this (), std::placeholders::_1));
ScheduleAck (ackTimeout);
}
}
else if (packet->IsSYN ())
Expand All @@ -207,23 +203,11 @@ namespace stream
// save message and wait for missing message again
SavePacket (packet);
if (m_LastReceivedSequenceNumber >= 0)
{
// send NACKs for missing messages ASAP
if (m_IsAckSendScheduled)
{
m_IsAckSendScheduled = false;
m_AckSendTimer.cancel ();
}
SendQuickAck ();
}
// send NACKs for missing messages with minimal timeout
ScheduleAck (MIN_SEND_ACK_TIMEOUT);
else
{
// wait for SYN
m_IsAckSendScheduled = true;
m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(SYN_TIMEOUT));
m_AckSendTimer.async_wait (std::bind (&Stream::HandleAckSendTimer,
shared_from_this (), std::placeholders::_1));
}
ScheduleAck (SYN_TIMEOUT);
}
}
}
Expand Down Expand Up @@ -1029,6 +1013,17 @@ namespace stream
}
}

void Stream::ScheduleAck (int timeout)
{
if (m_IsAckSendScheduled)
m_AckSendTimer.cancel ();
m_IsAckSendScheduled = true;
if (timeout < MIN_SEND_ACK_TIMEOUT) timeout = MIN_SEND_ACK_TIMEOUT;
m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(timeout));
m_AckSendTimer.async_wait (std::bind (&Stream::HandleAckSendTimer,
shared_from_this (), std::placeholders::_1));
}

void Stream::HandleAckSendTimer (const boost::system::error_code& ecode)
{
if (m_IsAckSendScheduled)
Expand Down
3 changes: 2 additions & 1 deletion libi2pd/Streaming.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023, The PurpleI2P Project
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -227,6 +227,7 @@ namespace stream

void ScheduleResend ();
void HandleResendTimer (const boost::system::error_code& ecode);
void ScheduleAck (int timeout);
void HandleAckSendTimer (const boost::system::error_code& ecode);

private:
Expand Down

0 comments on commit 530c353

Please sign in to comment.