Skip to content

Commit 247a1eb

Browse files
sapiersapier
sapier
authored and
sapier
committedAug 22, 2014
Improve timeout calculation
gf
1 parent 944ffe9 commit 247a1eb

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed
 

‎src/connection.cpp

+25-16
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,7 @@ void ConnectionSendThread::runTimeouts(float dtime)
14101410
(m_max_data_packets_per_iteration/numpeers));
14111411

14121412
channel->UpdatePacketLossCounter(timed_outs.size());
1413+
g_profiler->graphAdd("packets_lost", timed_outs.size());
14131414

14141415
m_iteration_packets_avaialble -= timed_outs.size();
14151416

@@ -1421,6 +1422,7 @@ void ConnectionSendThread::runTimeouts(float dtime)
14211422
u16 seqnum = readU16(&(k->data[BASE_HEADER_SIZE+1]));
14221423

14231424
channel->UpdateBytesLost(k->data.getSize());
1425+
k->resend_count++;
14241426

14251427
LOG(derr_con<<m_connection->getDesc()
14261428
<<"RE-SENDING timed-out RELIABLE to "
@@ -2349,24 +2351,30 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
23492351
try{
23502352
BufferedPacket p =
23512353
channel->outgoing_reliables_sent.popSeqnum(seqnum);
2352-
// Get round trip time
2353-
unsigned int current_time = porting::getTimeMs();
23542354

2355-
if (current_time > p.absolute_send_time)
2356-
{
2357-
float rtt = (current_time - p.absolute_send_time) / 1000.0;
2355+
// only calculate rtt from straight sent packets
2356+
if (p.resend_count == 0) {
2357+
// Get round trip time
2358+
unsigned int current_time = porting::getTimeMs();
23582359

2359-
// Let peer calculate stuff according to it
2360-
// (avg_rtt and resend_timeout)
2361-
dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2362-
}
2363-
else if (p.totaltime > 0)
2364-
{
2365-
float rtt = p.totaltime;
2360+
// a overflow is quite unlikely but as it'd result in major
2361+
// rtt miscalculation we handle it here
2362+
if (current_time > p.absolute_send_time)
2363+
{
2364+
float rtt = (current_time - p.absolute_send_time) / 1000.0;
23662365

2367-
// Let peer calculate stuff according to it
2368-
// (avg_rtt and resend_timeout)
2369-
dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2366+
// Let peer calculate stuff according to it
2367+
// (avg_rtt and resend_timeout)
2368+
dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2369+
}
2370+
else if (p.totaltime > 0)
2371+
{
2372+
float rtt = p.totaltime;
2373+
2374+
// Let peer calculate stuff according to it
2375+
// (avg_rtt and resend_timeout)
2376+
dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2377+
}
23702378
}
23712379
//put bytes for max bandwidth calculation
23722380
channel->UpdateBytesSent(p.data.getSize(),1);
@@ -2542,7 +2550,8 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
25422550

25432551
// we already have this packet so this one was on wire at least
25442552
// the current timeout
2545-
dynamic_cast<UDPPeer*>(&peer)->reportRTT(dynamic_cast<UDPPeer*>(&peer)->getResendTimeout());
2553+
// we don't know how long this packet was on wire don't do silly guessing
2554+
// dynamic_cast<UDPPeer*>(&peer)->reportRTT(dynamic_cast<UDPPeer*>(&peer)->getResendTimeout());
25462555

25472556
throw ProcessedSilentlyException("Retransmitting ack for old packet");
25482557
}

‎src/connection.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,19 @@ inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
162162
struct BufferedPacket
163163
{
164164
BufferedPacket(u8 *a_data, u32 a_size):
165-
data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1)
165+
data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
166+
resend_count(0)
166167
{}
167168
BufferedPacket(u32 a_size):
168-
data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1)
169+
data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
170+
resend_count(0)
169171
{}
170172
SharedBuffer<u8> data; // Data of the packet, including headers
171173
float time; // Seconds from buffering the packet or re-sending
172174
float totaltime; // Seconds from buffering the packet
173175
unsigned int absolute_send_time;
174176
Address address; // Sender or destination
177+
unsigned int resend_count;
175178
};
176179

177180
// This adds the base headers to the data and makes a packet out of it

‎src/constants.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4343

4444
#define CONNECTION_TIMEOUT 30
4545

46-
#define RESEND_TIMEOUT_MIN 0.333
46+
#define RESEND_TIMEOUT_MIN 0.1
4747
#define RESEND_TIMEOUT_MAX 3.0
4848
// resend_timeout = avg_rtt * this
4949
#define RESEND_TIMEOUT_FACTOR 4

0 commit comments

Comments
 (0)
Please sign in to comment.