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