wangyu-UDPspeeder/fec_manager.h

185 lines
3.6 KiB
C
Raw Normal View History

2017-09-28 05:21:53 -05:00
/*
* fec_manager.h
*
* Created on: Sep 27, 2017
* Author: root
*/
#ifndef FEC_MANAGER_H_
#define FEC_MANAGER_H_
2017-10-05 12:21:06 -05:00
#include "common.h"
#include "log.h"
#include "lib/rs.h"
2017-09-28 05:21:53 -05:00
2017-10-17 12:35:03 -05:00
const int max_blob_packet_num=20000;//how many packet can be contain in a blob_t ,can be set very large
const u32_t anti_replay_buff_size=20000;//can be set very large
const int max_fec_packet_num=255;// this is the limitation of the rs lib
const u32_t fec_buff_num=2000;// how many packet can fec_decode_manager hold. shouldnt be very large,or it will cost huge memory
2017-09-28 05:21:53 -05:00
2017-10-05 12:21:06 -05:00
struct anti_replay_t
2017-09-28 05:21:53 -05:00
{
2017-10-05 12:21:06 -05:00
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));
2017-10-15 12:03:10 -05:00
st.rehash(anti_replay_buff_size*3);
2017-10-05 12:21:06 -05:00
index=0;
}
void set_invaild(u32_t seq)
{
if(st.find(seq)!=st.end() )
{
2017-10-08 11:09:36 -05:00
mylog(log_trace,"seq %u exist\n",seq);
2017-10-05 12:21:06 -05:00
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();
}
};
2017-09-28 05:21:53 -05:00
struct blob_encode_t
{
char input_buf[(max_fec_packet_num+5)*buf_len];
2017-10-05 12:21:06 -05:00
int current_len;
int counter;
char *output_buf[max_fec_packet_num+100];
2017-10-06 13:42:53 -05:00
2017-10-05 12:21:06 -05:00
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);
2017-09-28 05:21:53 -05:00
};
struct blob_decode_t
{
char input_buf[(max_fec_packet_num+5)*buf_len];
2017-10-05 12:21:06 -05:00
int current_len;
int last_len;
int counter;
2017-10-17 12:35:03 -05:00
char *output_buf[max_blob_packet_num+100];
int output_len[max_blob_packet_num+100];
2017-10-06 13:42:53 -05:00
2017-10-05 12:21:06 -05:00
blob_decode_t();
int clear();
int input(char *input,int len);
int output(int &n,char ** &output,int *&len_arr);
2017-09-28 05:21:53 -05:00
};
2017-10-05 12:21:06 -05:00
2017-10-06 09:23:54 -05:00
class fec_encode_manager_t
2017-10-05 12:21:06 -05:00
{
2017-10-15 12:03:10 -05:00
private:
u32_t seq;
int type;
2017-10-05 12:21:06 -05:00
int fec_data_num,fec_redundant_num;
int fec_mtu;
2017-10-06 13:42:53 -05:00
int fec_pending_num;
int fec_pending_time;
2017-10-15 12:03:10 -05:00
2017-10-17 05:20:30 -05:00
my_time_t first_packet_time;
my_time_t first_packet_time_for_output;
2017-10-16 07:50:28 -05:00
blob_encode_t blob_encode;
2017-10-15 12:03:10 -05:00
char input_buf[max_fec_packet_num+5][buf_len];
2017-10-16 07:50:28 -05:00
int input_len[max_fec_packet_num+100];
char *output_buf[max_fec_packet_num+100];
int output_len[max_fec_packet_num+100];
2017-10-15 12:03:10 -05:00
2017-10-05 12:21:06 -05:00
int counter;
2017-10-06 13:42:53 -05:00
int timer_fd;
u64_t timer_fd64;
2017-10-05 12:21:06 -05:00
2017-10-15 12:03:10 -05:00
int ready_for_output;
2017-10-09 12:50:15 -05:00
u32_t output_n;
2017-10-16 07:50:28 -05:00
2017-10-09 12:50:15 -05:00
int append(char *s,int len);
2017-10-15 12:03:10 -05:00
2017-10-06 09:23:54 -05:00
public:
2017-10-05 12:21:06 -05:00
fec_encode_manager_t();
2017-10-06 14:19:02 -05:00
~fec_encode_manager_t();
2017-10-06 13:42:53 -05:00
2017-10-17 05:20:30 -05:00
int get_first_packet_time()
{
return first_packet_time_for_output;
}
2017-10-06 13:42:53 -05:00
u64_t get_timer_fd64();
2017-10-09 12:50:15 -05:00
int re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type);
2017-10-06 09:23:54 -05:00
int input(char *s,int len/*,int &is_first_packet*/);
2017-10-08 11:09:36 -05:00
int output(int &n,char ** &s_arr,int *&len);
2017-10-05 12:21:06 -05:00
};
struct fec_data_t
{
int used;
u32_t seq;
2017-10-06 09:23:54 -05:00
int type;
2017-10-05 12:21:06 -05:00
int data_num;
int redundant_num;
int idx;
char buf[buf_len];
int len;
};
2017-10-10 12:09:33 -05:00
struct fec_group_t
{
int type=-1;
int data_num=-1;
int redundant_num=-1;
int len=-1;
2017-10-16 07:50:28 -05:00
//int data_counter=0;
2017-10-10 12:09:33 -05:00
map<int,int> group_mp;
};
2017-10-06 09:23:54 -05:00
class fec_decode_manager_t
2017-10-05 12:21:06 -05:00
{
anti_replay_t anti_replay;
fec_data_t fec_data[fec_buff_num+10];
2017-10-05 12:21:06 -05:00
int index;
2017-10-10 12:09:33 -05:00
unordered_map<u32_t, fec_group_t> mp;
2017-10-05 12:21:06 -05:00
blob_decode_t blob_decode;
2017-10-06 13:42:53 -05:00
2017-10-05 12:21:06 -05:00
int output_n;
char ** output_s_arr;
int * output_len_arr;
2017-10-16 07:50:28 -05:00
char *output_s_arr_buf[max_fec_packet_num+100];
int output_len_arr_buf[max_fec_packet_num+100];
2017-10-05 12:21:06 -05:00
int ready_for_output;
2017-10-06 09:23:54 -05:00
public:
fec_decode_manager_t();
int re_init();
int input(char *s,int len);
int output(int &n,char ** &s_arr,int* &len_arr);
2017-10-05 12:21:06 -05:00
};
2017-09-28 05:21:53 -05:00
#endif /* FEC_MANAGER_H_ */