Skip to content

Commit

Permalink
just commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu- committed Oct 6, 2017
1 parent 844eac0 commit 3d70fd9
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 151 deletions.
4 changes: 3 additions & 1 deletion connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern int disable_anti_replay;
#include "log.h"
#include "delay_manager.h"
#include "fd_manager.h"
#include "fec_manager.h"


/*
Expand Down Expand Up @@ -67,7 +68,8 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o
//handle multiple clients
{
conv_manager_t conv_manager;
//anti_replay_t anti_replay;
fec_encode_manager_t fec_encode_manager;
fec_decode_manager_t fec_decode_manager;
fd64_t timer_fd;
ip_port_t ip_port;
};//g_conn_info;
Expand Down
147 changes: 125 additions & 22 deletions fec_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "log.h"
#include "common.h"
#include "lib/rs.h"
u32_t seq=0;



blob_encode_t::blob_encode_t()
Expand Down Expand Up @@ -66,7 +64,6 @@ int blob_encode_t::output(int n,char ** &s_arr,int & len)
blob_decode_t::blob_decode_t()
{
clear();

}
int blob_decode_t::clear()
{
Expand Down Expand Up @@ -118,46 +115,50 @@ int blob_decode_t::output(int &n,char ** &s_arr,int *&len_arr)

fec_encode_manager_t::fec_encode_manager_t()
{
re_init();
re_init(4,2,1200);
}
int fec_encode_manager_t::re_init()
int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu)
{
fec_data_num=4;
fec_redundant_num=2;
fec_mtu=1200;
fec_data_num=data_num;
fec_redundant_num=redundant_num;
fec_mtu=mtu;

counter=0;
blob_encode.clear();
ready_for_output=0;
seq=0;
return 0;
}
int fec_encode_manager_t::input(char *s,int len,int &is_first_packet)
int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
{
is_first_packet=0;
if(s==0 ||blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu)
{
char ** blob_output;
int blob_len;
assert(counter!=0);
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;i<fec_data_num+fec_redundant_num;i++)
{
int tmp_idx=0;
write_u32(buf[i]+tmp_idx,seq);
tmp_idx+=sizeof(u32_t);
buf[i][tmp_idx++]=(unsigned char)0;
buf[i][tmp_idx++]=(unsigned char)fec_data_num;
buf[i][tmp_idx++]=(unsigned char)fec_redundant_num;
buf[i][tmp_idx++]=(unsigned char)i;
buf[i][tmp_idx++]=(unsigned char)0;
output_buf[i]=buf[i]+tmp_idx;
if(i<fec_data_num)
{
memcpy(buf[i]+tmp_idx,blob_output[i],blob_len);
tmp_idx+=blob_len;
}
output_buf[i]=buf[i]+sizeof(u32_t)+3*sizeof(char);

}
output_len=blob_len+sizeof(u32_t)+3*sizeof(char);
output_len=blob_len+sizeof(u32_t)+4*sizeof(char);/////remember to change this 4,if modified the protocol

rs_encode2(fec_data_num,fec_data_num+fec_redundant_num,output_buf,blob_len);
for(int i=0;i<fec_data_num+fec_redundant_num;i++)
{
Expand All @@ -169,10 +170,9 @@ int fec_encode_manager_t::input(char *s,int len,int &is_first_packet)
counter=0;
blob_encode.clear();
}

if(s!=0)
{
if(counter==0) is_first_packet=1;
//if(counter==0) is_first_packet=1;
blob_encode.input(s,len);
counter++;
}
Expand All @@ -198,14 +198,117 @@ int fec_encode_manager_t::output(int &n,char ** &s_arr,int &len)
return 0;
}

/*
int fec_decode_manager_t::input(char *s,int l)
fec_decode_manager_t::fec_decode_manager_t()
{
return 0;
re_init();
}

int fec_decode_manager_t::output(int &n,char ** &s_arr,int* &l_arr)
int fec_decode_manager_t::re_init()
{
for(int i=0;i<(int)fec_buff_size;i++)
fec_data[i].used=0;
ready_for_output=0;
return 0;
}*/
}

int fec_decode_manager_t::input(char *s,int len)
{
assert(s!=0);
int tmp_idx=0;
u32_t seq=read_u32(s+tmp_idx);
tmp_idx+=sizeof(u32_t);
int type=(unsigned char)s[tmp_idx++];
int data_num=(unsigned char)s[tmp_idx++];
int redundant_num=(unsigned char)s[tmp_idx++];
int inner_index=(unsigned char)s[tmp_idx++];
len=len-tmp_idx;
if(len<0)
{
return -1;
}
if(data_num+redundant_num>255)
{
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(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<int,int> &inner_mp=mp[seq];
assert((int)inner_mp.size()<=data_num);
if((int)inner_mp.size()==data_num)
{

char *fec_tmp_arr[256+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<data_num;i++)
{
blob_decode.input(fec_tmp_arr[i],len);
}
blob_decode.output(output_n,output_s_arr,output_len_arr);
ready_for_output=1;
anti_replay.set_invaild(seq);
}

return 0;
}
int fec_decode_manager_t::output(int &n,char ** &s_arr,int* &len_arr)
{
if(!ready_for_output)
{
n=-1;
s_arr=0;
len_arr=0;
}
else
{
ready_for_output=0;
n=output_n;
s_arr=output_s_arr;
len_arr=output_len_arr;
}
return 0;
}
122 changes: 12 additions & 110 deletions fec_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,150 +87,52 @@ struct blob_decode_t
int output(int &n,char ** &output,int *&len_arr);
};

struct fec_encode_manager_t
class fec_encode_manager_t
{
int fec_data_num,fec_redundant_num;
int fec_mtu;
char buf[256+5][buf_len+100];
char *output_buf[256+5];
int output_len;
int ready_for_output;

u32_t seq;
int counter;

blob_encode_t blob_encode;
public:
fec_encode_manager_t();
int re_init();
int input(char *s,int len,int &is_first_packet);
int re_init(int data_num,int redundant_num,int mtu);
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;
int type;
char buf[buf_len];
int len;
};
struct fec_decode_manager_t
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;

fec_decode_manager_t()
{
for(int i=0;i<(int)fec_buff_size;i++)
fec_data[i].used=0;
ready_for_output=0;
}

int output_n;
char ** output_s_arr;
int * output_len_arr;

int ready_for_output;
int input(char *s,int len)
{
char *ori_s=s;
u32_t seq=read_u32(s);
s+=sizeof(u32_t);
int data_num=(unsigned char)*(s++);
int redundant_num=(unsigned char)*(s++);
int innder_index=(unsigned char)*(s++);
int type=(unsigned char)*(s++);
len=len-int(s-ori_s);
if(len<0)
{
return -1;
}

if(!anti_replay.is_vaild(seq))
{
return 0;
}
if(!mp[seq].empty())
{
int tmp_idx=mp[seq].begin()->second;
int ok=1;
if(data_num+redundant_num>255)
ok=0;
if(fec_data[tmp_idx].data_num!=data_num||fec_data[tmp_idx].redundant_num!=redundant_num||fec_data[tmp_idx].len!=len)
{
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].data_num=data_num;
fec_data[index].redundant_num=redundant_num;
fec_data[index].idx=innder_index;
fec_data[index].type=type;
fec_data[index].len=len;
mp[seq][innder_index]=index;


map<int,int> &inner_mp=mp[seq];
if((int)inner_mp.size()>=data_num)
{
anti_replay.set_invaild(seq);
char *fec_tmp_arr[256+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,redundant_num,fec_tmp_arr,len);
blob_decode.clear();
for(int i=0;i<data_num;i++)
{
blob_decode.input(fec_tmp_arr[i],len);
}
blob_decode.output(output_n,output_s_arr,output_len_arr);
ready_for_output=1;
}

index++;
if(index==int(anti_replay_buff_size)) index=0;


return 0;
}
int output(int &n,char ** &s_arr,int* &len_arr)
{
if(!ready_for_output)
{
n=-1;
s_arr=0;
len_arr=0;
}
else
{
ready_for_output=0;
n=output_n;
s_arr=output_s_arr;
len_arr=output_len_arr;
}
return 0;
}
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_ */
Loading

0 comments on commit 3d70fd9

Please sign in to comment.