里德所罗门算法组包还是偏复杂,使用范德蒙矩阵的FEC包生成方式,则简单的多。

验证:
void test_2() {
#define DATA_SIZE 1400
#define K_1 10 // 原始包数量
#define N_1 13 // 总包数量(原始 + 冗余)
init_fec();
void *code = fec_new(K_1, N_1);
if (!code) {
fprintf(stderr, "Failed to create FEC code\n");
return;
}
// 生成原始数据包
char *src[K_1];
for (int i = 0; i < K_1; i++) {
src[i] = malloc(DATA_SIZE);
for (int j = 0; j < DATA_SIZE; j++) {
src[i][j] = rand() % DATA_SIZE;
}
}
// 生成冗余包
char *dst[N_1 - K_1];
for (int i = 0; i < N_1 - K_1; i++) {
dst[i] = malloc(DATA_SIZE);
memset(dst[i], 0x00, DATA_SIZE);
fec_encode(code, (void **)src, dst[i], K_1 + i, DATA_SIZE);
}
printf("************---dst[K_1]:");
printf_hex(dst[K_1], DATA_SIZE);
printf("src[0]:");
printf_hex(src[0], DATA_SIZE);
// 模拟网络传输:假设丢失原始包,但收到其他所有包(包括冗余包)
// 收到的包:原始包1、2,冗余包3、4
void *recv_pkts[N_1];
for (int i=0; i<K_1; i++){
recv_pkts[i] = src[i];
}
for (int i=K_1; i<N_1; i++){
recv_pkts[i] = dst[i - K_1];
}
recv_pkts[0]= NULL;//
recv_pkts[2]= NULL;//
recv_pkts[4]= NULL;//
printf("OLD[4]:");
printf_hex(src[4], DATA_SIZE);
printf("OLD[10]:");
printf_hex(src[10], DATA_SIZE);
#if 0
int recv_indexes[N_1] = {0, 1, 2, 3, 4}; // 包含所有索引
// 调用解码函数:需传递有效包数量和正确索引
int result = fec_decode(
code, // FEC编解码器
recv_pkts, // 接收到的包数组
recv_indexes, // 对应的索引数组
N_1 - 1 // 实际接收到的包数量(sz参数)
);
#else
int result = rs_decode(code, recv_pkts,DATA_SIZE);
#endif
if (result < 0) {
printf("Decoding failed\n");
} else {
printf("Decoding successful\n");
//printf("recv_pkts[0]:");
//printf_hex(recv_pkts[0], DATA_SIZE);
//printf("recv_pkts[2]:");
//printf_hex(recv_pkts[2], DATA_SIZE);
printf("recv_pkts[4]:");
printf_hex(recv_pkts[4], DATA_SIZE);
printf("recv_pkts[2]:");
printf_hex(recv_pkts[2], DATA_SIZE);
// 检查包0是否恢复成功
if (memcmp(src[0], recv_pkts[0], DATA_SIZE) == 0) {
printf("Packet 0 recovered correctly\n");
} else {
printf("Packet 0 recovery failed\n");
}
if (memcmp(src[2], recv_pkts[2], DATA_SIZE) == 0) {
printf("Packet 0 recovered correctly\n");
} else {
printf("Packet 0 recovery failed\n");
}
}
// 释放内存
for (int i = 0; i < K_1; i++) free(src[i]);
for (int i = 0; i < N_1 - K_1; i++) free(dst[i]);
fec_free(code);
}-------------------广告线---------------
项目、合作,欢迎勾搭,邮箱:promall@qq.com
本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com
