3 #include <netinet/in.h>
6 #include <sys/select.h>
7 #include <sys/socket.h>
12 #include "reorderer.h"
13 #include "rsencoder.h"
20 void RSEncoder::send_packet(uint16_t proto, const std::string &data, int incoming_seq)
22 if (!packet_history.empty() &&
23 incoming_seq <= packet_history.back().seq) {
24 // Reorderer should have done this for us.
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();
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) {
40 void RSEncoder::finish_group()
42 // Our RS packets need to have the same max length as the longest one.
44 for (int i = 0; i < packet_history.size(); ++i) {
45 max_length = max<int>(max_length, packet_history[i].data.size());
48 vector<string> padded_packets;
49 for (int i = 0; i < packet_history.size(); ++i) {
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);
61 // Now construct RS packets.
62 vector<string> rs_packets;
63 for (int i = 0; i < RS_PARITY_SIZE; ++i) {
65 p.resize(max_length + 4);
66 memset(&p[0], 0, max_length + 4);
67 rs_packets.push_back(p);
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];
76 encode_rs_8(reinterpret_cast<unsigned char *>(&data[0]),
77 reinterpret_cast<unsigned char *>(&parity[0]),
79 for (int j = 0; j < RS_PARITY_SIZE; ++j) {
80 rs_packets[j][i] = parity[j];
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);
90 packet_history.clear();