fec_manager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * fec_manager.cpp
  3. *
  4. * Created on: Sep 27, 2017
  5. * Author: root
  6. */
  7. #include "fec_manager.h"
  8. #include "log.h"
  9. #include "common.h"
  10. #include "lib/rs.h"
  11. #include "fd_manager.h"
  12. int g_fec_data_num=20;
  13. int g_fec_redundant_num=10;
  14. int g_fec_mtu=1250;
  15. int g_fec_queue_len=200;
  16. int g_fec_timeout=8*1000; //8ms
  17. int g_fec_mode=1;
  18. int dynamic_update_fec=1;
  19. const int encode_fast_send=1;
  20. const int decode_fast_send=1;
  21. int short_packet_optimize=1;
  22. int header_overhead=40;
  23. u32_t fec_buff_num=2000;// how many packet can fec_decode_manager hold. shouldnt be very large,or it will cost huge memory
  24. blob_encode_t::blob_encode_t()
  25. {
  26. clear();
  27. }
  28. int blob_encode_t::clear()
  29. {
  30. counter=0;
  31. current_len=(int)sizeof(u32_t);
  32. return 0;
  33. }
  34. int blob_encode_t::get_num()
  35. {
  36. return counter;
  37. }
  38. int blob_encode_t::get_shard_len(int n)
  39. {
  40. return round_up_div(current_len,n);
  41. }
  42. int blob_encode_t::get_shard_len(int n,int next_packet_len)
  43. {
  44. return round_up_div(current_len+(int)sizeof(u16_t)+next_packet_len,n);
  45. }
  46. int blob_encode_t::input(char *s,int len)
  47. {
  48. assert(current_len+len+sizeof(u16_t) +100<sizeof(input_buf));
  49. assert(len<=65535&&len>=0);
  50. counter++;
  51. assert(counter<=max_blob_packet_num);
  52. write_u16(input_buf+current_len,len);
  53. current_len+=sizeof(u16_t);
  54. memcpy(input_buf+current_len,s,len);
  55. current_len+=len;
  56. return 0;
  57. }
  58. int blob_encode_t::output(int n,char ** &s_arr,int & len)
  59. {
  60. len=round_up_div(current_len,n);
  61. write_u32(input_buf,counter);
  62. for(int i=0;i<n;i++)
  63. {
  64. output_buf[i]=input_buf+len*i;
  65. }
  66. s_arr=output_buf;
  67. return 0;
  68. }
  69. blob_decode_t::blob_decode_t()
  70. {
  71. clear();
  72. }
  73. int blob_decode_t::clear()
  74. {
  75. current_len=0;
  76. last_len=-1;
  77. counter=0;
  78. return 0;
  79. }
  80. int blob_decode_t::input(char *s,int len)
  81. {
  82. if(last_len!=-1)
  83. {
  84. assert(last_len==len);
  85. }
  86. counter++;
  87. assert(counter<=max_fec_packet_num);
  88. last_len=len;
  89. assert(current_len+len+100<(int)sizeof(input_buf));//avoid overflow
  90. memcpy(input_buf+current_len,s,len);
  91. current_len+=len;
  92. return 0;
  93. }
  94. int blob_decode_t::output(int &n,char ** &s_arr,int *&len_arr)
  95. {
  96. int parser_pos=0;
  97. if(parser_pos+(int)sizeof(u32_t)>current_len) {mylog(log_info,"failed 0\n");return -1;}
  98. n=(int)read_u32(input_buf+parser_pos);
  99. if(n>max_blob_packet_num) {mylog(log_info,"failed 1\n");return -1;}
  100. s_arr=output_buf;
  101. len_arr=output_len;
  102. parser_pos+=sizeof(u32_t);
  103. for(int i=0;i<n;i++)
  104. {
  105. if(parser_pos+(int)sizeof(u16_t)>current_len) {mylog(log_info,"failed2 \n");return -1;}
  106. len_arr[i]=(int)read_u16(input_buf+parser_pos);
  107. parser_pos+=(int)sizeof(u16_t);
  108. 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;}
  109. s_arr[i]=input_buf+parser_pos;
  110. parser_pos+=len_arr[i];
  111. }
  112. return 0;
  113. }
  114. fec_encode_manager_t::~fec_encode_manager_t()
  115. {
  116. fd_manager.fd64_close(timer_fd64);
  117. }
  118. u64_t fec_encode_manager_t::get_timer_fd64()
  119. {
  120. return timer_fd64;
  121. }
  122. fec_encode_manager_t::fec_encode_manager_t()
  123. {
  124. //int timer_fd;
  125. if ((timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
  126. {
  127. mylog(log_fatal,"timer_fd create error");
  128. myexit(1);
  129. }
  130. timer_fd64=fd_manager.create(timer_fd);
  131. reset_fec_parameter(g_fec_data_num,g_fec_redundant_num,g_fec_mtu,g_fec_queue_len,g_fec_timeout,g_fec_mode);
  132. }
  133. int fec_encode_manager_t::reset_fec_parameter(int data_num,int redundant_num,int mtu,int queue_len,int timeout,int mode)
  134. {
  135. fec_data_num=data_num;
  136. fec_redundant_num=redundant_num;
  137. fec_mtu=mtu;
  138. fec_queue_len=queue_len;
  139. fec_timeout=timeout;
  140. fec_mode=mode;
  141. assert(data_num+redundant_num<max_fec_packet_num);
  142. clear();
  143. return 0;
  144. }
  145. int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/)
  146. {
  147. if(counter==0)
  148. {
  149. itimerspec its;
  150. memset(&its.it_interval,0,sizeof(its.it_interval));
  151. first_packet_time=get_current_time_us();
  152. my_time_t tmp_time=fec_timeout+first_packet_time;
  153. its.it_value.tv_sec=tmp_time/1000000llu;
  154. its.it_value.tv_nsec=(tmp_time%1000000llu)*1000llu;
  155. timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
  156. }
  157. if(fec_mode==0)//for type 0 use blob
  158. {
  159. assert(blob_encode.input(s,len)==0);
  160. }
  161. else if(fec_mode==1)//for tpe 1 use input_buf and counter
  162. {
  163. mylog(log_trace,"counter=%d\n",counter);
  164. assert(len<=65535&&len>=0);
  165. assert(len<=fec_mtu);
  166. char * p=input_buf[counter]+sizeof(u32_t)+4*sizeof(char);//copy directly to final position,avoid unnecessary copy.
  167. //remember to change this,if protocol is modified
  168. write_u16(p,(u16_t)((u32_t)len)); //TODO omit this u16 for data packet while sending
  169. p+=sizeof(u16_t);
  170. memcpy(p,s,len);
  171. input_len[counter]=len+sizeof(u16_t);
  172. }
  173. else
  174. {
  175. assert(0==1);
  176. }
  177. counter++;
  178. return 0;
  179. }
  180. int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
  181. {
  182. if(counter==0&&dynamic_update_fec)
  183. {
  184. fec_data_num=g_fec_data_num;
  185. fec_redundant_num=g_fec_redundant_num;
  186. fec_mtu=g_fec_mtu;
  187. fec_queue_len=g_fec_queue_len;
  188. fec_timeout=g_fec_timeout;
  189. fec_mode=g_fec_mode;
  190. }
  191. int about_to_fec=0;
  192. int delayed_append=0;
  193. //int counter_back=counter;
  194. assert(fec_mode==0||fec_mode==1);
  195. if(fec_mode==0&& s!=0 &&counter==0)
  196. {
  197. if(fec_data_num==1)
  198. {
  199. if(len>fec_mtu)
  200. {
  201. mylog(log_warn,"message too long for fec_data_num=1, len=%d fec_mtu=%d,ignored\n",len,fec_mtu);
  202. return -1;
  203. }
  204. }
  205. else
  206. {
  207. int out_len=blob_encode.get_shard_len(fec_data_num,len);
  208. if(out_len>fec_mtu)
  209. {
  210. mylog(log_warn,"message too long ori_len=%d out_len=%d fec_mtu=%d,ignored\n",len,out_len,fec_mtu);
  211. return -1;
  212. }
  213. }
  214. }
  215. if(fec_mode==1&&s!=0&&len>fec_mtu)
  216. {
  217. mylog(log_warn,"message too long len=%d fec_mtu=%d,ignored\n",len,fec_mtu);
  218. return -1;
  219. }
  220. if(s==0&&counter==0)
  221. {
  222. mylog(log_warn,"unexpected s==0&&counter==0\n");
  223. return -1;
  224. }
  225. if(s==0) about_to_fec=1;//now
  226. if(fec_mode==0&& blob_encode.get_shard_len(fec_data_num,len)>fec_mtu) {about_to_fec=1; delayed_append=1;}//fec then add packet
  227. if(fec_mode==0) assert(counter<fec_queue_len);//counter will never equal fec_pending_num,if that happens fec should already been done.
  228. if(fec_mode==1) assert(counter<fec_data_num);
  229. if(s!=0&&!delayed_append)
  230. {
  231. append(s,len);
  232. }
  233. if(fec_mode==0&& counter==fec_queue_len) about_to_fec=1;
  234. if(fec_mode==1&& counter==fec_data_num) about_to_fec=1;
  235. if(about_to_fec)
  236. {
  237. char ** blob_output=0;
  238. int fec_len=-1;
  239. mylog(log_trace,"counter=%d\n",counter);
  240. if(counter==0)
  241. {
  242. mylog(log_warn,"unexpected counter==0 here\n");
  243. return -1;
  244. }
  245. int actual_data_num;
  246. int actual_redundant_num;
  247. if(fec_mode==0)
  248. {
  249. actual_data_num=fec_data_num;
  250. actual_redundant_num=fec_redundant_num;
  251. if(short_packet_optimize)
  252. {
  253. u32_t best_len=(blob_encode.get_shard_len(fec_data_num,0)+header_overhead)*(fec_data_num+fec_redundant_num);
  254. int best_data_num=fec_data_num;
  255. for(int i=1;i<actual_data_num;i++)
  256. {
  257. u32_t new_len=(blob_encode.get_shard_len(i,0)+header_overhead)*(i+fec_redundant_num);
  258. if(new_len<best_len)
  259. {
  260. best_len=new_len;
  261. best_data_num=i;
  262. }
  263. }
  264. actual_data_num=best_data_num;
  265. actual_redundant_num=fec_redundant_num;
  266. mylog(log_trace,"actual_data_num=%d actual_redundant_num=%d\n",best_data_num,fec_redundant_num);
  267. }
  268. assert(blob_encode.output(actual_data_num,blob_output,fec_len)==0);
  269. }
  270. else
  271. {
  272. actual_data_num=counter;
  273. actual_redundant_num=fec_redundant_num;
  274. for(int i=0;i<counter;i++)
  275. {
  276. assert(input_len[i]>=0);
  277. if(input_len[i]>fec_len) fec_len=input_len[i];
  278. }
  279. }
  280. mylog(log_trace,"%d %d %d\n",actual_data_num,actual_redundant_num,fec_len);
  281. char *tmp_output_buf[max_fec_packet_num+5]={0};
  282. for(int i=0;i<actual_data_num+actual_redundant_num;i++)
  283. {
  284. int tmp_idx=0;
  285. write_u32(input_buf[i] + tmp_idx, seq);
  286. tmp_idx += sizeof(u32_t);
  287. input_buf[i][tmp_idx++] = (unsigned char) fec_mode;
  288. if (fec_mode == 1 && i < actual_data_num)
  289. {
  290. input_buf[i][tmp_idx++] = (unsigned char) 0;
  291. input_buf[i][tmp_idx++] = (unsigned char) 0;
  292. } else
  293. {
  294. input_buf[i][tmp_idx++] = (unsigned char) actual_data_num;
  295. input_buf[i][tmp_idx++] = (unsigned char) actual_redundant_num;
  296. }
  297. input_buf[i][tmp_idx++] = (unsigned char) i;
  298. tmp_output_buf[i]=input_buf[i]+tmp_idx; //////caution ,trick here.
  299. if(fec_mode==0)
  300. {
  301. output_len[i]=tmp_idx+fec_len;
  302. if(i<actual_data_num)
  303. {
  304. memcpy(input_buf[i]+tmp_idx,blob_output[i],fec_len);
  305. }
  306. }
  307. else
  308. {
  309. if(i<actual_data_num)
  310. {
  311. output_len[i]=tmp_idx+input_len[i];
  312. memset(tmp_output_buf[i]+input_len[i],0,fec_len-input_len[i]);
  313. }
  314. else
  315. output_len[i]=tmp_idx+fec_len;
  316. }
  317. output_buf[i]=input_buf[i];//output_buf points to same block of memory with different offset
  318. }
  319. if(0)
  320. {
  321. printf("seq=%u,fec_len=%d,%d %d,before fec\n",seq,fec_len,actual_data_num,actual_redundant_num);
  322. for(int i=0;i<actual_data_num;i++)
  323. {
  324. printf("{");
  325. for(int j=0;j<8+fec_len;j++)
  326. {
  327. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)input_buf[i][j]);
  328. }
  329. printf("},\n");
  330. //log_bare(log_warn,"")
  331. }
  332. }
  333. //output_len=blob_len+sizeof(u32_t)+4*sizeof(char);/////remember to change this 4,if modified the protocol
  334. rs_encode2(actual_data_num,actual_data_num+actual_redundant_num,tmp_output_buf,fec_len);
  335. if(0)
  336. {
  337. printf("seq=%u,fec_len=%d,%d %d,after fec\n",seq,fec_len,actual_data_num,actual_redundant_num);
  338. for(int i=0;i<actual_data_num+actual_redundant_num;i++)
  339. {
  340. printf("{");
  341. for(int j=0;j<8+fec_len;j++)
  342. {
  343. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)output_buf[i][j]);
  344. }
  345. printf("},\n");
  346. //log_bare(log_warn,"")
  347. }
  348. }
  349. //mylog(log_trace,"!!! s= %d\n");
  350. assert(ready_for_output==0);
  351. ready_for_output=1;
  352. first_packet_time_for_output=first_packet_time;
  353. first_packet_time=0;
  354. seq++;
  355. counter=0;
  356. output_n=actual_data_num+actual_redundant_num;
  357. blob_encode.clear();
  358. itimerspec its;
  359. memset(&its,0,sizeof(its));
  360. timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
  361. if(encode_fast_send&&fec_mode==1)
  362. {
  363. int packet_to_send[max_fec_packet_num+5]={0};
  364. int packet_to_send_counter=0;
  365. //assert(counter!=0);
  366. if(s!=0)
  367. packet_to_send[packet_to_send_counter++]=actual_data_num-1;
  368. for(int i=actual_data_num;i<actual_data_num+actual_redundant_num;i++)
  369. {
  370. packet_to_send[packet_to_send_counter++]=i;
  371. }
  372. output_n=packet_to_send_counter;//re write
  373. for(int i=0;i<packet_to_send_counter;i++)
  374. {
  375. output_buf[i]=output_buf[packet_to_send[i]];
  376. output_len[i]=output_len[packet_to_send[i]];
  377. }
  378. }
  379. }
  380. else
  381. {
  382. if(encode_fast_send&&s!=0&&fec_mode==1)
  383. {
  384. assert(counter>=1);
  385. assert(counter<=255);
  386. int input_buf_idx=counter-1;
  387. assert(ready_for_output==0);
  388. ready_for_output=1;
  389. first_packet_time_for_output=0;
  390. output_n=1;
  391. int tmp_idx=0;
  392. write_u32(input_buf[input_buf_idx]+tmp_idx,seq);
  393. tmp_idx+=sizeof(u32_t);
  394. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)fec_mode;
  395. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
  396. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
  397. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)((u32_t)input_buf_idx);
  398. output_len[0]=input_len[input_buf_idx]+tmp_idx;
  399. output_buf[0]=input_buf[input_buf_idx];
  400. if(0)
  401. {
  402. printf("seq=%u,buf_idx=%d\n",seq,input_buf_idx);
  403. for(int j=0;j<output_len[0];j++)
  404. {
  405. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)output_buf[0][j]);
  406. }
  407. printf("\n");
  408. }
  409. }
  410. }
  411. if(s!=0&&delayed_append)
  412. {
  413. assert(fec_mode!=1);
  414. append(s,len);
  415. }
  416. return 0;
  417. }
  418. int fec_encode_manager_t::output(int &n,char ** &s_arr,int *&len)
  419. {
  420. if(!ready_for_output)
  421. {
  422. n=-1;
  423. len=0;
  424. s_arr=0;
  425. }
  426. else
  427. {
  428. n=output_n;
  429. len=output_len;
  430. s_arr=output_buf;
  431. ready_for_output=0;
  432. }
  433. return 0;
  434. }
  435. /*
  436. int fec_decode_manager_t::re_init()
  437. {
  438. clear();
  439. return 0;
  440. }*/
  441. int fec_decode_manager_t::input(char *s,int len)
  442. {
  443. assert(s!=0);
  444. assert(len+100<buf_len);//guarenteed by upper level
  445. int tmp_idx=0;
  446. int tmp_header_len=sizeof(u32_t)+sizeof(char)*4;
  447. if(len<tmp_header_len)
  448. {
  449. mylog(log_warn,"len =%d\n",len);
  450. return -1;
  451. }
  452. u32_t seq=read_u32(s+tmp_idx);
  453. tmp_idx+=sizeof(u32_t);
  454. int type=(unsigned char)s[tmp_idx++];
  455. int data_num=(unsigned char)s[tmp_idx++];
  456. int redundant_num=(unsigned char)s[tmp_idx++];
  457. int inner_index=(unsigned char)s[tmp_idx++];
  458. len=len-tmp_idx;
  459. //mylog(log_trace,"input\n");
  460. if(len<0)
  461. {
  462. mylog(log_warn,"len<0\n");
  463. return -1;
  464. }
  465. if(type==1)
  466. {
  467. if(len<(int)sizeof(u16_t))
  468. {
  469. mylog(log_warn,"type==1&&len<2\n");
  470. return -1;
  471. }
  472. if(data_num==0&&(int)( read_u16(s+tmp_idx)+sizeof(u16_t))!=len)
  473. {
  474. mylog(log_warn,"inner_index<data_num&&read_u16(s+tmp_idx)+sizeof(u16_t)!=len %d %d\n",(int)( read_u16(s+tmp_idx)+sizeof(u16_t)),len);
  475. return -1;
  476. }
  477. }
  478. if(type==0&&data_num==0)
  479. {
  480. mylog(log_warn,"unexpected type==0&&data_num==0\n");
  481. return -1;
  482. }
  483. if(data_num+redundant_num>=max_fec_packet_num)
  484. {
  485. mylog(log_warn,"data_num+redundant_num>=max_fec_packet_num\n");
  486. return -1;
  487. }
  488. if(!anti_replay.is_vaild(seq))
  489. {
  490. mylog(log_trace,"!anti_replay.is_vaild(seq) ,seq =%u\n",seq);
  491. return 0;
  492. }
  493. if(mp[seq].group_mp.find(inner_index)!=mp[seq].group_mp.end() )
  494. {
  495. mylog(log_debug,"dup fec index\n");//duplicate can happen on a normal network, so its just log_debug
  496. return -1;
  497. }
  498. if(mp[seq].type==-1)
  499. mp[seq].type=type;
  500. else
  501. {
  502. if(mp[seq].type!=type)
  503. {
  504. mylog(log_warn,"type mismatch\n");
  505. return -1;
  506. }
  507. }
  508. if(data_num!=0)
  509. {
  510. //mp[seq].data_counter++;
  511. if(mp[seq].data_num==-1)
  512. {
  513. mp[seq].data_num=data_num;
  514. mp[seq].redundant_num=redundant_num;
  515. mp[seq].len=len;
  516. }
  517. else
  518. {
  519. if(mp[seq].data_num!=data_num||mp[seq].redundant_num!=redundant_num||mp[seq].len!=len)
  520. {
  521. mylog(log_warn,"unexpected mp[seq].data_num!=data_num||mp[seq].redundant_num!=redundant_num||mp[seq].len!=len\n");
  522. return -1;
  523. }
  524. }
  525. }
  526. //mylog(log_info,"mp.size()=%d index=%d\n",mp.size(),index);
  527. if(fec_data[index].used!=0)
  528. {
  529. u32_t tmp_seq=fec_data[index].seq;
  530. anti_replay.set_invaild(tmp_seq);
  531. if(mp.find(tmp_seq)!=mp.end())
  532. {
  533. mp.erase(tmp_seq);
  534. }
  535. if(tmp_seq==seq)
  536. {
  537. mylog(log_warn,"unexpected tmp_seq==seq ,seq=%d\n",seq);
  538. return -1;
  539. }
  540. }
  541. fec_data[index].used=1;
  542. fec_data[index].seq=seq;
  543. fec_data[index].type=type;
  544. fec_data[index].data_num=data_num;
  545. fec_data[index].redundant_num=redundant_num;
  546. fec_data[index].idx=inner_index;
  547. fec_data[index].len=len;
  548. assert(0<=index&&index<(int)fec_buff_num);
  549. assert(len+100<buf_len);
  550. memcpy(fec_data[index].buf,s+tmp_idx,len);
  551. mp[seq].group_mp[inner_index]=index;
  552. //index++ at end of function
  553. map<int,int> &inner_mp=mp[seq].group_mp;
  554. int about_to_fec=0;
  555. if(type==0)
  556. {
  557. //assert((int)inner_mp.size()<=data_num);
  558. if((int)inner_mp.size()>data_num)
  559. {
  560. mylog(log_warn,"inner_mp.size()>data_num\n");
  561. anti_replay.set_invaild(seq);
  562. goto end;
  563. }
  564. if((int)inner_mp.size()==data_num)
  565. about_to_fec=1;
  566. }
  567. else
  568. {
  569. if(mp[seq].data_num!=-1)
  570. {
  571. if((int)inner_mp.size()>mp[seq].data_num+1)
  572. {
  573. mylog(log_warn,"inner_mp.size()>data_num+1\n");
  574. anti_replay.set_invaild(seq);
  575. goto end;
  576. }
  577. if((int)inner_mp.size()>=mp[seq].data_num)
  578. {
  579. about_to_fec=1;
  580. }
  581. }
  582. }
  583. if(about_to_fec)
  584. {
  585. int group_data_num=mp[seq].data_num;
  586. int group_redundant_num=mp[seq].redundant_num;
  587. //mylog(log_error,"fec here!\n");
  588. if(type==0)
  589. {
  590. char *fec_tmp_arr[max_fec_packet_num+5]={0};
  591. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  592. {
  593. fec_tmp_arr[it->first]=fec_data[it->second].buf;
  594. }
  595. assert(rs_decode2(group_data_num,group_data_num+group_redundant_num,fec_tmp_arr,len)==0); //the input data has been modified in-place
  596. //this line should always succeed
  597. blob_decode.clear();
  598. for(int i=0;i<group_data_num;i++)
  599. {
  600. blob_decode.input(fec_tmp_arr[i],len);
  601. }
  602. if(blob_decode.output(output_n,output_s_arr,output_len_arr)!=0)
  603. {
  604. mylog(log_warn,"blob_decode failed\n");
  605. //ready_for_output=0;
  606. anti_replay.set_invaild(seq);
  607. goto end;
  608. }
  609. assert(ready_for_output==0);
  610. ready_for_output=1;
  611. anti_replay.set_invaild(seq);
  612. }
  613. else//type==1
  614. {
  615. int max_len=-1;
  616. int fec_result_ok=1;
  617. int data_check_ok=1;
  618. int debug_num=inner_mp.size();
  619. int missed_packet[max_fec_packet_num+5];
  620. int missed_packet_counter=0;
  621. //outupt_s_arr_buf[max_fec_packet_num+5]={0};
  622. //memset(output_s_arr_buf,0,sizeof(output_s_arr_buf));//in efficient
  623. for(int i=0;i<group_data_num+group_redundant_num;i++)
  624. {
  625. output_s_arr_buf[i]=0;
  626. }
  627. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  628. {
  629. output_s_arr_buf[it->first]=fec_data[it->second].buf;
  630. if(fec_data[it->second].len<(int)sizeof(u16_t))
  631. {
  632. mylog(log_warn,"fec_data[it->second].len<(int)sizeof(u16_t)");
  633. data_check_ok=0;
  634. }
  635. if(fec_data[it->second].len > max_len)
  636. max_len=fec_data[it->second].len;
  637. }
  638. if(max_len!=mp[seq].len)
  639. {
  640. data_check_ok=0;
  641. mylog(log_warn,"max_len!=mp[seq].len");
  642. }
  643. if(data_check_ok==0)
  644. {
  645. //ready_for_output=0;
  646. mylog(log_warn,"data_check_ok==0\n");
  647. anti_replay.set_invaild(seq);
  648. goto end;
  649. }
  650. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  651. {
  652. int tmp_idx=it->second;
  653. assert(max_len>=fec_data[tmp_idx].len);//guarenteed by data_check_ok
  654. memset(fec_data[tmp_idx].buf+fec_data[tmp_idx].len,0,max_len-fec_data[tmp_idx].len);
  655. }
  656. for(int i=0;i<group_data_num;i++)
  657. {
  658. if(output_s_arr_buf[i]==0 ||i==inner_index) //only missed packet +current packet
  659. {
  660. missed_packet[missed_packet_counter++]=i;
  661. }
  662. }
  663. mylog(log_trace,"fec done,%d %d,missed_packet_counter=%d\n",group_data_num,group_redundant_num,missed_packet_counter);
  664. assert(rs_decode2(group_data_num,group_data_num+group_redundant_num,output_s_arr_buf,max_len)==0);//this should always succeed
  665. for(int i=0;i<group_data_num;i++)
  666. {
  667. output_len_arr_buf[i]=read_u16(output_s_arr_buf[i]);
  668. output_s_arr_buf[i]+=sizeof(u16_t);
  669. if(output_len_arr_buf[i]>max_data_len)
  670. {
  671. mylog(log_warn,"invaild len %d,seq= %u,data_num= %d r_num= %d,i= %d\n",output_len_arr_buf[i],seq,group_data_num,group_redundant_num,i);
  672. fec_result_ok=0;
  673. for(int i=0;i<missed_packet_counter;i++)
  674. {
  675. log_bare(log_warn,"%d ",missed_packet[i]);
  676. }
  677. log_bare(log_warn,"\n");
  678. //break;
  679. }
  680. }
  681. if(fec_result_ok)
  682. {
  683. output_n=group_data_num;
  684. if(decode_fast_send)
  685. {
  686. output_n=missed_packet_counter;
  687. for(int i=0;i<missed_packet_counter;i++)
  688. {
  689. output_s_arr_buf[i]=output_s_arr_buf[missed_packet[i]];
  690. output_len_arr_buf[i]=output_len_arr_buf[missed_packet[i]];
  691. }
  692. }
  693. output_s_arr=output_s_arr_buf;
  694. output_len_arr=output_len_arr_buf;
  695. assert(ready_for_output==0);
  696. ready_for_output=1;
  697. }
  698. else
  699. {
  700. //fec_not_ok:
  701. ready_for_output=0;
  702. }
  703. anti_replay.set_invaild(seq);
  704. }// end of type==1
  705. }
  706. else //not about_to_fec
  707. {
  708. if(decode_fast_send)
  709. {
  710. if(type==1&&data_num==0)
  711. {
  712. assert(ready_for_output==0);
  713. output_n=1;
  714. int check_len=read_u16(fec_data[index].buf);
  715. output_s_arr_buf[0]=fec_data[index].buf+sizeof(u16_t);
  716. output_len_arr_buf[0]=fec_data[index].len-sizeof(u16_t);
  717. if(output_len_arr_buf[0]!=check_len)
  718. {
  719. mylog(log_warn,"len mismatch %d %d\n",output_len_arr_buf[0],check_len);
  720. }
  721. output_s_arr=output_s_arr_buf;
  722. output_len_arr=output_len_arr_buf;
  723. ready_for_output=1;
  724. }
  725. }
  726. }
  727. end:
  728. index++;
  729. if(index==int(fec_buff_num)) index=0;
  730. return 0;
  731. }
  732. int fec_decode_manager_t::output(int &n,char ** &s_arr,int* &len_arr)
  733. {
  734. if(!ready_for_output)
  735. {
  736. n=-1;
  737. s_arr=0;
  738. len_arr=0;
  739. }
  740. else
  741. {
  742. ready_for_output=0;
  743. n=output_n;
  744. s_arr=output_s_arr;
  745. len_arr=output_len_arr;
  746. }
  747. return 0;
  748. }