]> git.sesse.net Git - greproxy/blob - reorderer.cpp
Timeout packets in the reordering buffer after 100 ms. Helps in situations with seque...
[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 #define TIMEOUT_SEC 0.100
8
9 using namespace std;
10
11 double tdiff(const timeval& a, const timeval& b)
12 {
13         return b.tv_sec - a.tv_sec +
14                 1e-6 * (b.tv_usec - a.tv_usec);
15 }
16
17 Reorderer::Reorderer(Sender* sender)
18         : sender(sender), last_seq(-1)
19 {
20 }
21
22 void Reorderer::send_packet(uint16_t proto, const string& data, int seq)
23 {
24         timeval now;
25         gettimeofday(&now, NULL);
26
27         bool silence = false;
28         if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
29                 printf("Gave up waiting for packets [%d,%d> (buffer full)\n",
30                         last_seq + 1, packet_buffer.top().seq);
31                 silence = true;
32                 last_seq = packet_buffer.top().seq - 1;
33         } else if (!packet_buffer.empty() &&
34                    tdiff(packet_buffer.top().ts, now) > TIMEOUT_SEC) {
35                 printf("Gave up waiting for packets [%d,%d> (timeout)\n",
36                         last_seq + 1, packet_buffer.top().seq);
37                 silence = true;
38                 last_seq = packet_buffer.top().seq - 1;
39                 // TODO: Rerun immediately after we've cleared out,
40                 // in case there are more timeouts.
41         }
42
43         GREPacket packet;
44         packet.seq = seq;
45         packet.proto = proto;
46         packet.data = data;
47         packet.ts = now;
48         packet_buffer.push(packet);
49
50         while (!packet_buffer.empty() &&
51                (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
52                 int front_seq = packet_buffer.top().seq;
53                 if (front_seq < last_seq + 1) {
54                         printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
55                                 front_seq, last_seq + 1);
56                         packet_buffer.pop();
57                         continue;
58                 }
59                 //if (packet_buffer.size() > 1) {
60                 //      printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
61                 //} else {
62                 //      printf("seq=%d\n", front_seq);
63                 //}
64                 const string &data = packet_buffer.top().data;
65                 check_ts_discontinuity(packet_buffer.top().proto, data, silence);
66                 sender->send_packet(proto, data, seq);
67                 packet_buffer.pop();
68                 last_seq = front_seq;
69                 if (!silence && !packet_buffer.empty()) {
70                         printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
71                         silence = true;
72                 }
73         }
74 }
75
76 void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence)
77 {
78         if (data.size() == 1344) {
79                 for (int i = 0; i < 7; ++i) {
80                         const char *pkt = &data[i * 188 + 28];
81                         int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
82                         if (pid == 8191) {
83                                 // stuffing, ignore
84                                 continue;
85                         }
86                         int has_payload = pkt[3] & 0x10;
87                         int cc = pkt[3] & 0xf;
88                         if (has_payload) {
89                                 int last_cc = ccs[pid];
90                                 if (!silence && cc != ((last_cc + 1) & 0xf)) {
91                                         printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);
92                                 }
93                                 ccs[pid] = cc;
94                         }
95                 }
96         }
97 }
98