]> git.sesse.net Git - greproxy/commitdiff
Move Reorderer into its own file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Feb 2015 00:16:34 +0000 (01:16 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Feb 2015 00:16:34 +0000 (01:16 +0100)
reorderer.cpp [new file with mode: 0644]

diff --git a/reorderer.cpp b/reorderer.cpp
new file mode 100644 (file)
index 0000000..fe3b7b6
--- /dev/null
@@ -0,0 +1,79 @@
+#include <arpa/inet.h>
+
+#include "reorderer.h"
+#include "protocol.h"
+
+#define PACKET_BUFFER_SIZE 100
+
+using namespace std;
+
+Reorderer::Reorderer(Protocol* sender)
+       : sender(sender), last_seq(-1)
+{
+}
+
+void Reorderer::handle_packet(uint16_t proto, const string& data, int seq)
+{
+       bool silence = false;
+       if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
+               printf("Gave up waiting for packets [%d,%d>\n",
+                       last_seq + 1, packet_buffer.top().seq);
+               silence = true;
+               last_seq = packet_buffer.top().seq - 1;
+       }
+
+       GREPacket packet;
+       packet.seq = seq;
+       packet.proto = proto;
+       packet.data = data;
+       packet_buffer.push(packet);
+
+       while (!packet_buffer.empty() &&
+              (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
+               int front_seq = packet_buffer.top().seq;
+               if (front_seq < last_seq + 1) {
+                       printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
+                               front_seq, last_seq + 1);
+                       packet_buffer.pop();
+                       continue;
+               }
+               //if (packet_buffer.size() > 1) {
+               //      printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
+               //} else {
+               //      printf("seq=%d\n", front_seq);
+               //}
+               const string &data = packet_buffer.top().data;
+               send_packet(packet_buffer.top().proto, data, silence);
+               packet_buffer.pop();
+               last_seq = front_seq;
+               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;
+               }
+       }
+}
+
+void Reorderer::send_packet(uint16_t proto, const string &data, bool silence)
+{
+       if (data.size() == 1344) {
+               for (int i = 0; i < 7; ++i) {
+                       const char *pkt = &data[i * 188 + 28];
+                       int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
+                       if (pid == 8191) {
+                               // stuffing, ignore
+                               continue;
+                       }
+                       int has_payload = pkt[3] & 0x10;
+                       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);
+                               }
+                               ccs[pid] = cc;
+                       }
+               }
+       }
+       sender->send_packet(proto, data);
+}
+