From 1bacf4bb3d9dbd9f07e33b5676f5a1be43cd83a5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 7 Feb 2015 16:30:30 +0100 Subject: [PATCH] Timeout packets in the reordering buffer after 100 ms. Helps in situations with sequence number restarts, and also when there is no steady stream of packets. --- reorderer.cpp | 21 ++++++++++++++++++++- reorderer.h | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/reorderer.cpp b/reorderer.cpp index e18b63f..7b19d1d 100644 --- a/reorderer.cpp +++ b/reorderer.cpp @@ -4,9 +4,16 @@ #include "protocol.h" #define PACKET_BUFFER_SIZE 100 +#define TIMEOUT_SEC 0.100 using namespace std; +double tdiff(const timeval& a, const timeval& b) +{ + return b.tv_sec - a.tv_sec + + 1e-6 * (b.tv_usec - a.tv_usec); +} + Reorderer::Reorderer(Sender* sender) : sender(sender), last_seq(-1) { @@ -14,18 +21,30 @@ Reorderer::Reorderer(Sender* sender) void Reorderer::send_packet(uint16_t proto, const string& data, int seq) { + timeval now; + gettimeofday(&now, NULL); + bool silence = false; if (packet_buffer.size() >= PACKET_BUFFER_SIZE) { - printf("Gave up waiting for packets [%d,%d>\n", + printf("Gave up waiting for packets [%d,%d> (buffer full)\n", + last_seq + 1, packet_buffer.top().seq); + silence = true; + last_seq = packet_buffer.top().seq - 1; + } else if (!packet_buffer.empty() && + tdiff(packet_buffer.top().ts, now) > TIMEOUT_SEC) { + printf("Gave up waiting for packets [%d,%d> (timeout)\n", last_seq + 1, packet_buffer.top().seq); silence = true; last_seq = packet_buffer.top().seq - 1; + // TODO: Rerun immediately after we've cleared out, + // in case there are more timeouts. } GREPacket packet; packet.seq = seq; packet.proto = proto; packet.data = data; + packet.ts = now; packet_buffer.push(packet); while (!packet_buffer.empty() && diff --git a/reorderer.h b/reorderer.h index cdde814..f58a932 100644 --- a/reorderer.h +++ b/reorderer.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -16,6 +17,7 @@ struct GREPacket { int seq; uint16_t proto; std::string data; + timeval ts; bool operator> (const GREPacket &other) const { return seq > other.seq; -- 2.39.2