/* * fec_manager.cpp * * Created on: Sep 27, 2017 * Author: root */ #include "fec_manager.h" #include "log.h" #include "common.h" #include "lib/rs.h" #include "fd_manager.h" blob_encode_t::blob_encode_t() { clear(); } int blob_encode_t::clear() { counter=0; current_len=(int)sizeof(u32_t); return 0; } int blob_encode_t::get_num() { return counter; } int blob_encode_t::get_shard_len(int n) { return round_up_div(current_len,n); } int blob_encode_t::get_shard_len(int n,int next_packet_len) { return round_up_div(current_len+(int)sizeof(u16_t)+next_packet_len,n); } int blob_encode_t::input(char *s,int len) { assert(current_len+len+sizeof(u16_t) <=max_fec_packet_num*buf_len); assert(len<=65535&&len>=0); counter++; assert(counter<=max_normal_packet_num); write_u16(buf+current_len,len); current_len+=sizeof(u16_t); memcpy(buf+current_len,s,len); current_len+=len; return 0; } int blob_encode_t::output(int n,char ** &s_arr,int & len) { len=round_up_div(current_len,n); write_u32(buf,counter); for(int i=0;icurrent_len) return -1; n=(int)read_u32(buf+parser_pos); if(n>max_normal_packet_num) {mylog(log_info,"failed 1\n");return -1;} s_arr=s_buf; len_arr=len_buf; parser_pos+=sizeof(u32_t); for(int i=0;icurrent_len) {mylog(log_info,"failed2 \n");return -1;} len_arr[i]=(int)read_u16(buf+parser_pos); parser_pos+=(int)sizeof(u16_t); if(parser_pos+len_arr[i]>current_len) {mylog(log_info,"failed 3 %d %d %d\n",parser_pos,len_arr[i],current_len);return -1;} s_arr[i]=buf+parser_pos; parser_pos+=len_arr[i]; } return 0; } fec_encode_manager_t::fec_encode_manager_t() { //int timer_fd; if ((timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0) { mylog(log_fatal,"timer_fd create error"); myexit(1); } timer_fd64=fd_manager.create(timer_fd); re_init(4,2,1200,100,10000); } fec_encode_manager_t::~fec_encode_manager_t() { fd_manager.fd64_close(timer_fd64); } u64_t fec_encode_manager_t::get_timer_fd64() { return timer_fd64; } int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time) { fec_data_num=data_num; fec_redundant_num=redundant_num; fec_mtu=mtu; fec_pending_num=pending_num; fec_pending_time=pending_time; counter=0; blob_encode.clear(); ready_for_output=0; seq=0; itimerspec zero_its; memset(&zero_its, 0, sizeof(zero_its)); timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &zero_its, 0); return 0; } int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/) { if(s==0 ||blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu||counter>=fec_pending_num) { char ** blob_output; int blob_len; mylog(log_debug,"counter=%d\n",counter); if(counter==0) { if(s==0) return 0;//relax this restriction temporarily else mylog(log_warn,"message too long,ignored\n"); } blob_encode.output(fec_data_num,blob_output,blob_len); for(int i=0;imax_fec_packet_num) { return -1; } if(!anti_replay.is_vaild(seq)) { return 0; } if(!mp[seq].empty()) { int first_idx=mp[seq].begin()->second; int ok=1; if(fec_data[first_idx].data_num!=data_num) ok=0; if(fec_data[first_idx].redundant_num!=redundant_num) ok=0; if(fec_data[first_idx].len!=len) ok=0; if(fec_data[first_idx].type!=type) ok=0; if(ok==0) { return 0; } } if(fec_data[index].used!=0) { int tmp_seq=fec_data[index].seq; anti_replay.set_invaild(tmp_seq); if(mp.find(tmp_seq)!=mp.end()) { mp.erase(tmp_seq); } } fec_data[index].used=1; fec_data[index].seq=seq; fec_data[index].type=type; fec_data[index].data_num=data_num; fec_data[index].redundant_num=redundant_num; fec_data[index].idx=inner_index; fec_data[index].len=len; memcpy(fec_data[index].buf,s+tmp_idx,len); mp[seq][inner_index]=index; index++; if(index==int(anti_replay_buff_size)) index=0; map &inner_mp=mp[seq]; assert((int)inner_mp.size()<=data_num); if((int)inner_mp.size()==data_num) { if(type==0) { char *fec_tmp_arr[max_fec_packet_num+5]={0}; for(auto it=inner_mp.begin();it!=inner_mp.end();it++) { fec_tmp_arr[it->first]=fec_data[it->second].buf; } rs_decode2(data_num,data_num+redundant_num,fec_tmp_arr,len); //the input data has been modified in-place blob_decode.clear(); for(int i=0;i