]> git.sesse.net Git - greproxy/blob - reorderer.cpp
Port greproxy to all the new classes and stuff.
[greproxy] / reorderer.cpp
1 #include <arpa/inet.h>
2
3 #include "reorderer.h"
4 #include "protocol.h"
5
6 #define PACKET_BUFFER_SIZE 100
7
8 using namespace std;
9
10 Reorderer::Reorderer(Protocol* sender)
11         : sender(sender), last_seq(-1)
12 {
13 }
14
15 void Reorderer::send_packet(uint16_t proto, const string& data, int seq)
16 {
17         bool silence = false;
18         if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
19                 printf("Gave up waiting for packets [%d,%d>\n",
20                         last_seq + 1, packet_buffer.top().seq);
21                 silence = true;
22                 last_seq = packet_buffer.top().seq - 1;
23         }
24
25         GREPacket packet;
26         packet.seq = seq;
27         packet.proto = proto;
28         packet.data = data;
29         packet_buffer.push(packet);
30
31         while (!packet_buffer.empty() &&
32                (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
33                 int front_seq = packet_buffer.top().seq;
34                 if (front_seq < last_seq + 1) {
35                         printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
36                                 front_seq, last_seq + 1);
37                         packet_buffer.pop();
38                         continue;
39                 }
40                 //if (packet_buffer.size() > 1) {
41                 //      printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
42                 //} else {
43                 //      printf("seq=%d\n", front_seq);
44                 //}
45                 const string &data = packet_buffer.top().data;
46                 check_ts_discontinuity(packet_buffer.top().proto, data, silence);
47                 sender->send_packet(proto, data, seq);
48                 packet_buffer.pop();
49                 last_seq = front_seq;
50                 if (!silence && !packet_buffer.empty()) {
51                         printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
52                         silence = true;
53                 }
54         }
55 }
56
57 void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence)
58 {
59         if (data.size() == 1344) {
60                 for (int i = 0; i < 7; ++i) {
61                         const char *pkt = &data[i * 188 + 28];
62                         int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
63                         if (pid == 8191) {
64                                 // stuffing, ignore
65                                 continue;
66                         }
67                         int has_payload = pkt[3] & 0x10;
68                         int cc = pkt[3] & 0xf;
69                         if (has_payload) {
70                                 int last_cc = ccs[pid];
71                                 if (!silence && cc != ((last_cc + 1) & 0xf)) {
72                                         printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);
73                                 }
74                                 ccs[pid] = cc;
75                         }
76                 }
77         }
78 }
79