]> git.sesse.net Git - greproxy/blob - reorderer.cpp
Make Reorderer handle multi-timeouts.
[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::possibly_adjust_tv(timeval *tv)
19 {
20         if (packet_buffer.empty()) {
21                 return;
22         }
23
24         timeval now;
25         gettimeofday(&now, NULL);
26         timeval tdiff = subtract_timeval_saturate(
27                 offset_timeval_seconds(packet_buffer.top().ts, TIMEOUT_SEC), now);
28         if (less_than(tdiff, *tv)) {
29                 *tv = tdiff;
30         }
31 }
32
33 void Reorderer::send_packet(uint16_t proto, const string& data, int seq)
34 {
35         timeval now;
36         gettimeofday(&now, NULL);
37
38         bool silence = false;
39         if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
40                 printf("Gave up waiting for packets [%d,%d> (buffer full)\n",
41                         last_seq + 1, packet_buffer.top().seq);
42                 silence = true;
43                 last_seq = packet_buffer.top().seq - 1;
44         } else if (!packet_buffer.empty() &&
45                    tdiff(packet_buffer.top().ts, now) > TIMEOUT_SEC) {
46                 printf("Gave up waiting for packets [%d,%d> (timeout)\n",
47                         last_seq + 1, packet_buffer.top().seq);
48                 silence = true;
49                 last_seq = packet_buffer.top().seq - 1;
50         }
51
52         // In case of restarts.
53         if (packet_buffer.empty() &&
54             seq < last_seq &&
55             tdiff(last_sent_packet, now) > 5.0) {
56                 printf("No good data for five seconds, resetting sequence to %d\n", seq);
57                 last_seq = seq - 1;
58         }
59
60         GREPacket packet;
61         packet.seq = seq;
62         packet.proto = proto;
63         packet.data = data;
64         packet.ts = now;
65         packet_buffer.push(packet);
66
67         while (!packet_buffer.empty() &&
68                (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
69                 int front_seq = packet_buffer.top().seq;
70                 if (front_seq < last_seq + 1) {
71                         printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
72                                 front_seq, last_seq + 1);
73                         packet_buffer.pop();
74                         continue;
75                 }
76                 //if (packet_buffer.size() > 1) {
77                 //      printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
78                 //} else {
79                 //      printf("seq=%d\n", front_seq);
80                 //}
81                 const string &data = packet_buffer.top().data;
82                 check_ts_discontinuity(packet_buffer.top().proto, data, silence);
83                 sender->send_packet(proto, data, seq);
84                 packet_buffer.pop();
85                 last_seq = front_seq;
86                 last_sent_packet = now;
87                 if (!silence && !packet_buffer.empty()) {
88                         printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
89                         silence = true;
90                 }
91         }
92 }
93
94 void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence)
95 {
96         if (data.size() == 1344) {
97                 for (int i = 0; i < 7; ++i) {
98                         const char *pkt = &data[i * 188 + 28];
99                         int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
100                         if (pid == 8191) {
101                                 // stuffing, ignore
102                                 continue;
103                         }
104                         int has_payload = pkt[3] & 0x10;
105                         int cc = pkt[3] & 0xf;
106                         if (has_payload) {
107                                 int last_cc = ccs[pid];
108                                 if (!silence && cc != ((last_cc + 1) & 0xf)) {
109                                         printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);
110                                 }
111                                 ccs[pid] = cc;
112                         }
113                 }
114         }
115 }
116