X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=reorderer.cpp;h=c2823e5254c94a85c7201d61efbb613d27d88073;hb=bbcd8293ef911c2bfcae0327dc15266b6d6b0bee;hp=051280287c7754d066c71906438923de610131a6;hpb=02120e9414a6e613b17d6284891593271659b5d9;p=greproxy diff --git a/reorderer.cpp b/reorderer.cpp index 0512802..c2823e5 100644 --- a/reorderer.cpp +++ b/reorderer.cpp @@ -2,24 +2,34 @@ #include "reorderer.h" #include "protocol.h" +#include "timeutil.h" #define PACKET_BUFFER_SIZE 1000 #define TIMEOUT_SEC 1.000 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) { gettimeofday(&last_sent_packet, NULL); } +void Reorderer::possibly_adjust_tv(timeval *tv) +{ + if (packet_buffer.empty()) { + return; + } + + timeval now; + gettimeofday(&now, NULL); + timeval tdiff = subtract_timeval_saturate( + offset_timeval_seconds(packet_buffer.top().ts, TIMEOUT_SEC), now); + if (less_than(tdiff, *tv)) { + *tv = tdiff; + } +} + void Reorderer::send_packet(uint16_t proto, const string& data, int seq) { timeval now; @@ -30,15 +40,15 @@ void Reorderer::send_packet(uint16_t proto, const string& data, int seq) printf("Gave up waiting for packets [%d,%d> (buffer full)\n", last_seq + 1, packet_buffer.top().seq); silence = true; + num_lost_packets += packet_buffer.top().seq - (last_seq + 1); 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; + num_lost_packets += packet_buffer.top().seq - (last_seq + 1); last_seq = packet_buffer.top().seq - 1; - // TODO: Rerun immediately after we've cleared out, - // in case there are more timeouts. } // In case of restarts. @@ -56,6 +66,8 @@ void Reorderer::send_packet(uint16_t proto, const string& data, int seq) packet.ts = now; packet_buffer.push(packet); + bool did_reorder = false; + while (!packet_buffer.empty() && (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) { int front_seq = packet_buffer.top().seq; @@ -72,15 +84,22 @@ void Reorderer::send_packet(uint16_t proto, const string& data, int seq) //} const string &data = packet_buffer.top().data; check_ts_discontinuity(packet_buffer.top().proto, data, silence); - sender->send_packet(proto, data, seq); + sender->send_packet(packet_buffer.top().proto, data, packet_buffer.top().seq); packet_buffer.pop(); last_seq = front_seq; last_sent_packet = now; - if (!silence && !packet_buffer.empty()) { - printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq); - silence = true; + if (!packet_buffer.empty()) { + did_reorder = true; + if (!silence) { + printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq); + silence = true; + } } } + + if (did_reorder) { + ++num_reorders; + } } void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence) @@ -97,8 +116,11 @@ void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool int cc = pkt[3] & 0xf; if (has_payload) { int last_cc = ccs[pid]; - if (!silence && cc != ((last_cc + 1) & 0xf)) { - printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc); + if (cc != ((last_cc + 1) & 0xf)) { + if (!silence) { + printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc); + } + ++num_ts_discontinuities; } ccs[pid] = cc; }