]> git.sesse.net Git - greproxy/blob - rsencoder.cpp
Add FEC.
[greproxy] / rsencoder.cpp
1 #include <string.h>
2 #include <arpa/inet.h>
3 #include <netinet/in.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/select.h>
7 #include <sys/socket.h>
8 extern "C" {
9 #include <fec.h>
10 }
11
12 #include "reorderer.h"
13 #include "rsencoder.h"
14 #include "rs_parm.h"
15
16 #include <algorithm>
17
18 using namespace std;
19
20 void RSEncoder::send_packet(uint16_t proto, const std::string &data, int incoming_seq)
21 {
22         if (!packet_history.empty() &&
23             incoming_seq <= packet_history.back().seq) {
24                 // Reorderer should have done this for us.
25                 return;
26         }
27         if (!packet_history.empty() &&
28             incoming_seq / RS_PAYLOAD_SIZE !=
29                 packet_history.back().seq / RS_PAYLOAD_SIZE) {
30                 // Received an unfinished group.
31                 packet_history.clear();
32         }
33         sender->send_packet(proto, data, incoming_seq);
34         packet_history.emplace_back(GREPacket{incoming_seq, proto, data});
35         if (packet_history.size() == RS_PAYLOAD_SIZE) {
36                 finish_group();
37         }
38 }
39
40 void RSEncoder::finish_group()
41 {
42         // Our RS packets need to have the same max length as the longest one.
43         int max_length = 0;
44         for (int i = 0; i < packet_history.size(); ++i) {
45                 max_length = max<int>(max_length, packet_history[i].data.size());
46         }
47
48         vector<string> padded_packets;
49         for (int i = 0; i < packet_history.size(); ++i) {
50                 string p;
51                 p.resize(max_length + 4);
52                 memset(&p[0], 0, max_length + 4);
53                 uint16_t proto_be = htons(packet_history[i].proto);
54                 memcpy(&p[0], &proto_be, sizeof(uint16_t));
55                 uint16_t len_be = htons(packet_history[i].data.size());
56                 memcpy(&p[2], &len_be, sizeof(uint16_t));
57                 memcpy(&p[4], packet_history[i].data.data(), packet_history[i].data.size());
58                 padded_packets.push_back(p);
59         }
60
61         // Now construct RS packets.
62         vector<string> rs_packets;
63         for (int i = 0; i < RS_PARITY_SIZE; ++i) {
64                 string p;
65                 p.resize(max_length + 4);
66                 memset(&p[0], 0, max_length + 4);
67                 rs_packets.push_back(p);
68         }
69         string data, parity;
70         data.resize(RS_PAYLOAD_SIZE);
71         parity.resize(RS_PARITY_SIZE);
72         for (int i = 0; i < max_length + 4; ++i) {
73                 for (int j = 0; j < packet_history.size(); ++j) {
74                         data[j] = packet_history[j].data[i];
75                 }
76                 encode_rs_8(reinterpret_cast<unsigned char *>(&data[0]),
77                             reinterpret_cast<unsigned char *>(&parity[0]),
78                             RS_PAD);
79                 for (int j = 0; j < RS_PARITY_SIZE; ++j) {
80                         rs_packets[j][i] = parity[j];
81                 }
82         }
83
84         // Actually send the RS packets.
85         int start_seq = packet_history[0].seq - 1;
86         for (int i = 0; i < RS_PARITY_SIZE; ++i) {
87                 sender->send_packet(0xffff, rs_packets[i], start_seq - i);
88         }
89         
90         packet_history.clear();
91 }
92