wangyu-UDPspeeder/fec_manager.h
2017-10-09 12:50:15 -05:00

161 lines
3.1 KiB
C++

/*
* fec_manager.h
*
* Created on: Sep 27, 2017
* Author: root
*/
#ifndef FEC_MANAGER_H_
#define FEC_MANAGER_H_
#include "common.h"
#include "log.h"
#include "lib/rs.h"
const int max_normal_packet_num=1000;
const int max_fec_packet_num=255;
const u32_t anti_replay_buff_size=30000;
const u32_t fec_buff_size=3000;
struct anti_replay_t
{
u64_t replay_buffer[anti_replay_buff_size];
unordered_set<u32_t> st;
int index;
anti_replay_t()
{
memset(replay_buffer,-1,sizeof(replay_buffer));
st.rehash(anti_replay_buff_size*10);
index=0;
}
void set_invaild(u32_t seq)
{
if(st.find(seq)!=st.end() )
{
mylog(log_trace,"seq %u exist\n",seq);
return;
//return 0;
}
if(replay_buffer[index]!=u64_t(i64_t(-1)))
{
assert(st.find(replay_buffer[index])!=st.end());
st.erase(replay_buffer[index]);
}
replay_buffer[index]=seq;
st.insert(seq);
index++;
if(index==int(anti_replay_buff_size)) index=0;
//return 1; //for complier check
}
int is_vaild(u32_t seq)
{
return st.find(seq)==st.end();
}
};
struct blob_encode_t
{
char buf[(max_fec_packet_num+5)*buf_len];
int current_len;
int counter;
char *output_arr[max_fec_packet_num+100];
blob_encode_t();
int clear();
int get_num();
int get_shard_len(int n);
int get_shard_len(int n,int next_packet_len);
int input(char *s,int len); //len=use len=0 for second and following packet
int output(int n,char ** &s_arr,int & len);
};
struct blob_decode_t
{
char buf[(max_fec_packet_num+5)*buf_len];
int current_len;
int last_len;
int counter;
char *s_buf[max_normal_packet_num+100];
int len_buf[max_normal_packet_num+100];
blob_decode_t();
int clear();
int input(char *input,int len);
int output(int &n,char ** &output,int *&len_arr);
};
class fec_encode_manager_t
{
int fec_data_num,fec_redundant_num;
int fec_mtu;
int fec_pending_num;
int fec_pending_time;
char buf[max_fec_packet_num+5][buf_len+100];
char buf_s_len[max_fec_packet_num+5];
char *output_buf[max_fec_packet_num+5];
int output_len[max_fec_packet_num+5];
int ready_for_output;
u32_t seq;
int counter;
int timer_fd;
u64_t timer_fd64;
u32_t output_n;
int type;
blob_encode_t blob_encode;
int append(char *s,int len);
public:
fec_encode_manager_t();
~fec_encode_manager_t();
u64_t get_timer_fd64();
int re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type);
int input(char *s,int len/*,int &is_first_packet*/);
int output(int &n,char ** &s_arr,int *&len);
};
struct fec_data_t
{
int used;
u32_t seq;
int type;
int data_num;
int redundant_num;
int idx;
char buf[buf_len];
int len;
};
class fec_decode_manager_t
{
anti_replay_t anti_replay;
fec_data_t fec_data[fec_buff_size];
int index;
unordered_map<u32_t, map<int,int> > mp;
blob_decode_t blob_decode;
int output_n;
char ** output_s_arr;
int * output_len_arr;
char *output_s_arr_buf[max_normal_packet_num+100];
int output_len_arr_buf[max_normal_packet_num+100];
int ready_for_output;
public:
fec_decode_manager_t();
int re_init();
int input(char *s,int len);
int output(int &n,char ** &s_arr,int* &len_arr);
};
#endif /* FEC_MANAGER_H_ */