]> git.sesse.net Git - greproxy/blob - reorderer.cpp
Add missing commits from previous cleanups.
[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::handle_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                 send_packet(packet_buffer.top().proto, data, silence);
47                 packet_buffer.pop();
48                 last_seq = front_seq;
49                 if (!silence && !packet_buffer.empty()) {
50                         printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
51                         silence = true;
52                 }
53         }
54 }
55
56 void Reorderer::send_packet(uint16_t proto, const string &data, bool silence)
57 {
58         if (data.size() == 1344) {
59                 for (int i = 0; i < 7; ++i) {
60                         const char *pkt = &data[i * 188 + 28];
61                         int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
62                         if (pid == 8191) {
63                                 // stuffing, ignore
64                                 continue;
65                         }
66                         int has_payload = pkt[3] & 0x10;
67                         int cc = pkt[3] & 0xf;
68                         if (has_payload) {
69                                 int last_cc = ccs[pid];
70                                 if (!silence && cc != ((last_cc + 1) & 0xf)) {
71                                         printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);
72                                 }
73                                 ccs[pid] = cc;
74                         }
75                 }
76         }
77         sender->send_packet(proto, data);
78 }
79