forked from catid/siamese
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.cpp
More file actions
59 lines (45 loc) · 1.54 KB
/
Copy pathEncoder.cpp
File metadata and controls
59 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Encoder.h"
#include "GF256Tables.h"
#include "gf256.h"
#include <vector>
#include <cstring>
namespace siamese {
Encoder::Encoder() {
gf256_init(); // Ensure GF256 tables are initialized
packet_count = 0;
}
bool Encoder::Add(SiameseOriginalPacket& packet) {
if (packet.Data == nullptr || packet.DataBytes == 0)
return false;
OriginalPacket op;
op.Data.assign(packet.Data, packet.Data + packet.DataBytes);
op.PacketNum = packet.PacketNum;
op.DataBytes = packet.DataBytes;
originals.push_back(std::move(op));
++packet_count;
return true;
}
bool Encoder::Retransmit(SiameseOriginalPacket& packet) {
// For now, treat retransmit like a re-add
return Add(packet);
}
bool Encoder::GetRecoveryPacket(unsigned int id, unsigned char* buffer, unsigned int bufferSize, unsigned int& packetSize) {
if (originals.empty() || bufferSize == 0)
return false;
unsigned int size = originals[0].DataBytes;
if (bufferSize < size)
return false;
memset(buffer, 0, size);
for (size_t i = 0; i < originals.size(); ++i) {
uint8_t coefficient = gf256_exp((id + i) % 255); // Simple coefficient variation
gf256_muladd_mem(buffer, coefficient, originals[i].Data.data(), size);
}
packetSize = size;
return true;
}
bool Encoder::Acknowledge(const unsigned char* data, unsigned int size, unsigned int& acknowledgedCount) {
// Simulate ACK logic (optional depending on protocol)
acknowledgedCount = packet_count;
return true;
}
} // namespace siamese