fec_manager.cpp 18 KB

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