#include "protocol.h"
#define PACKET_BUFFER_SIZE 100
+#define TIMEOUT_SEC 0.100
using namespace std;
-Reorderer::Reorderer(Protocol* sender)
+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)
{
}
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() &&