fec_manager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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_blob_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_blob_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<max_fec_packet_num);
  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. first_packet_time=get_current_time_us();
  151. my_time_t tmp_time=fec_pending_time+first_packet_time;
  152. its.it_value.tv_sec=tmp_time/1000000llu;
  153. its.it_value.tv_nsec=(tmp_time%1000000llu)*1000llu;
  154. timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
  155. }
  156. if(type==0)//for type 0 use blob
  157. {
  158. assert(blob_encode.input(s,len)==0);
  159. }
  160. else if(type==1)//for tpe 1 use input_buf and counter
  161. {
  162. mylog(log_trace,"counter=%d\n",counter);
  163. assert(len<=65535&&len>=0);
  164. assert(len<=fec_mtu);
  165. char * p=input_buf[counter]+sizeof(u32_t)+4*sizeof(char);//copy directly to final position,avoid unnecessary copy.
  166. //remember to change this,if protocol is modified
  167. write_u16(p,(u16_t)((u32_t)len)); //TODO omit this u16 for data packet while sending
  168. p+=sizeof(u16_t);
  169. memcpy(p,s,len);
  170. input_len[counter]=len+sizeof(u16_t);
  171. }
  172. else
  173. {
  174. assert(0==1);
  175. }
  176. counter++;
  177. return 0;
  178. }
  179. int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
  180. {
  181. int about_to_fec=0;
  182. int delayed_append=0;
  183. //int counter_back=counter;
  184. assert(type==0||type==1);
  185. if(type==0&& s!=0 &&counter==0&&blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu)
  186. {
  187. mylog(log_warn,"message too long len=%d,ignored\n",len);
  188. return -1;
  189. }
  190. if(type==1&&s!=0&&len>=fec_mtu)
  191. {
  192. mylog(log_warn,"message too long len=%d fec_mtu=%d,ignored\n",len,fec_mtu);
  193. return -1;
  194. }
  195. if(s==0&&counter==0)
  196. {
  197. mylog(log_warn,"unexpected s==0&&counter==0\n");
  198. return -1;
  199. }
  200. if(s==0) about_to_fec=1;//now
  201. 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
  202. if(type==0) assert(counter<fec_pending_num);//counter will never equal fec_pending_num,if that happens fec should already been done.
  203. if(type==1) assert(counter<fec_data_num);
  204. if(s!=0&&!delayed_append)
  205. {
  206. append(s,len);
  207. }
  208. if(type==0&& counter==fec_pending_num) about_to_fec=1;
  209. if(type==1&& counter==fec_data_num) about_to_fec=1;
  210. if(about_to_fec)
  211. {
  212. char ** blob_output=0;
  213. int fec_len=-1;
  214. mylog(log_trace,"counter=%d\n",counter);
  215. if(counter==0)
  216. {
  217. mylog(log_warn,"unexpected counter==0 here\n");
  218. return -1;
  219. }
  220. int actual_data_num;
  221. int actual_redundant_num;
  222. if(type==0)
  223. {
  224. actual_data_num=fec_data_num;
  225. actual_redundant_num=fec_redundant_num;
  226. if(short_packet_optimize)
  227. {
  228. u32_t best_len=(blob_encode.get_shard_len(fec_data_num,0)+header_overhead)*(fec_data_num+fec_redundant_num);
  229. int best_data_num=fec_data_num;
  230. for(int i=1;i<actual_data_num;i++)
  231. {
  232. u32_t new_len=(blob_encode.get_shard_len(i,0)+header_overhead)*(i+fec_redundant_num);
  233. if(new_len<best_len)
  234. {
  235. best_len=new_len;
  236. best_data_num=i;
  237. }
  238. }
  239. actual_data_num=best_data_num;
  240. actual_redundant_num=fec_redundant_num;
  241. mylog(log_trace,"actual_data_num=%d actual_redundant_num=%d\n",best_data_num,fec_redundant_num);
  242. }
  243. assert(blob_encode.output(actual_data_num,blob_output,fec_len)==0);
  244. }
  245. else
  246. {
  247. actual_data_num=counter;
  248. actual_redundant_num=fec_redundant_num;
  249. for(int i=0;i<counter;i++)
  250. {
  251. assert(input_len[i]>=0);
  252. if(input_len[i]>fec_len) fec_len=input_len[i];
  253. }
  254. }
  255. mylog(log_trace,"%d %d %d\n",actual_data_num,actual_redundant_num,fec_len);
  256. char *tmp_output_buf[max_fec_packet_num+5]={0};
  257. for(int i=0;i<actual_data_num+actual_redundant_num;i++)
  258. {
  259. int tmp_idx=0;
  260. write_u32(input_buf[i] + tmp_idx, seq);
  261. tmp_idx += sizeof(u32_t);
  262. input_buf[i][tmp_idx++] = (unsigned char) type;
  263. if (type == 1 && i < actual_data_num)
  264. {
  265. input_buf[i][tmp_idx++] = (unsigned char) 0;
  266. input_buf[i][tmp_idx++] = (unsigned char) 0;
  267. } else
  268. {
  269. input_buf[i][tmp_idx++] = (unsigned char) actual_data_num;
  270. input_buf[i][tmp_idx++] = (unsigned char) actual_redundant_num;
  271. }
  272. input_buf[i][tmp_idx++] = (unsigned char) i;
  273. tmp_output_buf[i]=input_buf[i]+tmp_idx; //////caution ,trick here.
  274. if(type==0)
  275. {
  276. output_len[i]=tmp_idx+fec_len;
  277. if(i<actual_data_num)
  278. {
  279. memcpy(input_buf[i]+tmp_idx,blob_output[i],fec_len);
  280. }
  281. }
  282. else
  283. {
  284. if(i<actual_data_num)
  285. {
  286. output_len[i]=tmp_idx+input_len[i];
  287. memset(tmp_output_buf[i]+input_len[i],0,fec_len-input_len[i]);
  288. }
  289. else
  290. output_len[i]=tmp_idx+fec_len;
  291. }
  292. output_buf[i]=input_buf[i];//output_buf points to same block of memory with different offset
  293. }
  294. if(0)
  295. {
  296. printf("seq=%u,fec_len=%d,%d %d,before fec\n",seq,fec_len,actual_data_num,actual_redundant_num);
  297. for(int i=0;i<actual_data_num;i++)
  298. {
  299. printf("{");
  300. for(int j=0;j<8+fec_len;j++)
  301. {
  302. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)input_buf[i][j]);
  303. }
  304. printf("},\n");
  305. //log_bare(log_warn,"")
  306. }
  307. }
  308. //output_len=blob_len+sizeof(u32_t)+4*sizeof(char);/////remember to change this 4,if modified the protocol
  309. rs_encode2(actual_data_num,actual_data_num+actual_redundant_num,tmp_output_buf,fec_len);
  310. if(0)
  311. {
  312. printf("seq=%u,fec_len=%d,%d %d,after fec\n",seq,fec_len,actual_data_num,actual_redundant_num);
  313. for(int i=0;i<actual_data_num+actual_redundant_num;i++)
  314. {
  315. printf("{");
  316. for(int j=0;j<8+fec_len;j++)
  317. {
  318. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)output_buf[i][j]);
  319. }
  320. printf("},\n");
  321. //log_bare(log_warn,"")
  322. }
  323. }
  324. //mylog(log_trace,"!!! s= %d\n");
  325. assert(ready_for_output==0);
  326. ready_for_output=1;
  327. first_packet_time_for_output=first_packet_time;
  328. seq++;
  329. counter=0;
  330. output_n=actual_data_num+actual_redundant_num;
  331. blob_encode.clear();
  332. itimerspec its;
  333. memset(&its,0,sizeof(its));
  334. timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
  335. if(encode_fast_send&&type==1)
  336. {
  337. int packet_to_send[max_fec_packet_num+5]={0};
  338. int packet_to_send_counter=0;
  339. //assert(counter!=0);
  340. if(s!=0)
  341. packet_to_send[packet_to_send_counter++]=actual_data_num-1;
  342. for(int i=actual_data_num;i<actual_data_num+actual_redundant_num;i++)
  343. {
  344. packet_to_send[packet_to_send_counter++]=i;
  345. }
  346. output_n=packet_to_send_counter;//re write
  347. for(int i=0;i<packet_to_send_counter;i++)
  348. {
  349. output_buf[i]=output_buf[packet_to_send[i]];
  350. output_len[i]=output_len[packet_to_send[i]];
  351. }
  352. }
  353. }
  354. else
  355. {
  356. if(encode_fast_send&&s!=0&&type==1)
  357. {
  358. assert(counter>=1);
  359. assert(counter<=255);
  360. int input_buf_idx=counter-1;
  361. assert(ready_for_output==0);
  362. ready_for_output=1;
  363. first_packet_time_for_output=0;
  364. output_n=1;
  365. int tmp_idx=0;
  366. write_u32(input_buf[input_buf_idx]+tmp_idx,seq);
  367. tmp_idx+=sizeof(u32_t);
  368. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)type;
  369. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
  370. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
  371. input_buf[input_buf_idx][tmp_idx++]=(unsigned char)((u32_t)input_buf_idx);
  372. output_len[0]=input_len[input_buf_idx]+tmp_idx;
  373. output_buf[0]=input_buf[input_buf_idx];
  374. if(0)
  375. {
  376. printf("seq=%u,buf_idx=%d\n",seq,input_buf_idx);
  377. for(int j=0;j<output_len[0];j++)
  378. {
  379. log_bare(log_warn,"0x%02x,",(u32_t)(unsigned char)output_buf[0][j]);
  380. }
  381. printf("\n");
  382. }
  383. }
  384. }
  385. if(s!=0&&delayed_append)
  386. {
  387. assert(type!=1);
  388. append(s,len);
  389. }
  390. return 0;
  391. }
  392. int fec_encode_manager_t::output(int &n,char ** &s_arr,int *&len)
  393. {
  394. if(!ready_for_output)
  395. {
  396. n=-1;
  397. len=0;
  398. s_arr=0;
  399. }
  400. else
  401. {
  402. n=output_n;
  403. len=output_len;
  404. s_arr=output_buf;
  405. ready_for_output=0;
  406. }
  407. return 0;
  408. }
  409. fec_decode_manager_t::fec_decode_manager_t()
  410. {
  411. re_init();
  412. }
  413. int fec_decode_manager_t::re_init()
  414. {
  415. for(int i=0;i<(int)fec_buff_num;i++)
  416. fec_data[i].used=0;
  417. ready_for_output=0;
  418. return 0;
  419. }
  420. int fec_decode_manager_t::input(char *s,int len)
  421. {
  422. assert(s!=0);
  423. assert(len+100<buf_len);//guarenteed by upper level
  424. int tmp_idx=0;
  425. int tmp_header_len=sizeof(u32_t)+sizeof(char)*4;
  426. if(len<tmp_header_len)
  427. {
  428. mylog(log_warn,"len =%d\n",len);
  429. return -1;
  430. }
  431. u32_t seq=read_u32(s+tmp_idx);
  432. tmp_idx+=sizeof(u32_t);
  433. int type=(unsigned char)s[tmp_idx++];
  434. int data_num=(unsigned char)s[tmp_idx++];
  435. int redundant_num=(unsigned char)s[tmp_idx++];
  436. int inner_index=(unsigned char)s[tmp_idx++];
  437. len=len-tmp_idx;
  438. //mylog(log_trace,"input\n");
  439. if(len<0)
  440. {
  441. mylog(log_warn,"len<0\n");
  442. return -1;
  443. }
  444. if(type==1)
  445. {
  446. if(len<(int)sizeof(u16_t))
  447. {
  448. mylog(log_warn,"type==1&&len<2\n");
  449. return -1;
  450. }
  451. if(data_num==0&&(int)( read_u16(s+tmp_idx)+sizeof(u16_t))!=len)
  452. {
  453. 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);
  454. return -1;
  455. }
  456. }
  457. if(type==0&&data_num==0)
  458. {
  459. mylog(log_warn,"unexpected type==0&&data_num==0\n");
  460. return -1;
  461. }
  462. if(data_num+redundant_num>=max_fec_packet_num)
  463. {
  464. mylog(log_warn,"data_num+redundant_num>=max_fec_packet_num\n");
  465. return -1;
  466. }
  467. if(!anti_replay.is_vaild(seq))
  468. {
  469. mylog(log_trace,"!anti_replay.is_vaild(seq) ,seq =%u\n",seq);
  470. return 0;
  471. }
  472. if(mp[seq].group_mp.find(inner_index)!=mp[seq].group_mp.end() )
  473. {
  474. mylog(log_debug,"dup fec index\n");//duplicate can happen on a normal network, so its just log_debug
  475. return -1;
  476. }
  477. if(mp[seq].type==-1)
  478. mp[seq].type=type;
  479. else
  480. {
  481. if(mp[seq].type!=type)
  482. {
  483. mylog(log_warn,"type mismatch\n");
  484. return -1;
  485. }
  486. }
  487. if(data_num!=0)
  488. {
  489. //mp[seq].data_counter++;
  490. if(mp[seq].data_num==-1)
  491. {
  492. mp[seq].data_num=data_num;
  493. mp[seq].redundant_num=redundant_num;
  494. mp[seq].len=len;
  495. }
  496. else
  497. {
  498. if(mp[seq].data_num!=data_num||mp[seq].redundant_num!=redundant_num||mp[seq].len!=len)
  499. {
  500. mylog(log_warn,"unexpected mp[seq].data_num!=data_num||mp[seq].redundant_num!=redundant_num||mp[seq].len!=len\n");
  501. return -1;
  502. }
  503. }
  504. }
  505. if(fec_data[index].used!=0)
  506. {
  507. u32_t tmp_seq=fec_data[index].seq;
  508. anti_replay.set_invaild(tmp_seq);
  509. if(mp.find(tmp_seq)!=mp.end())
  510. {
  511. mp.erase(tmp_seq);
  512. }
  513. if(tmp_seq==seq)
  514. {
  515. mylog(log_warn,"unexpected tmp_seq==seq ,seq=%d\n",seq);
  516. return -1;
  517. }
  518. }
  519. fec_data[index].used=1;
  520. fec_data[index].seq=seq;
  521. fec_data[index].type=type;
  522. fec_data[index].data_num=data_num;
  523. fec_data[index].redundant_num=redundant_num;
  524. fec_data[index].idx=inner_index;
  525. fec_data[index].len=len;
  526. assert(0<=index&&index<(int)fec_buff_num);
  527. assert(len+100<buf_len);
  528. memcpy(fec_data[index].buf,s+tmp_idx,len);
  529. mp[seq].group_mp[inner_index]=index;
  530. //index++ at end of function
  531. map<int,int> &inner_mp=mp[seq].group_mp;
  532. int about_to_fec=0;
  533. if(type==0)
  534. {
  535. //assert((int)inner_mp.size()<=data_num);
  536. if((int)inner_mp.size()>data_num)
  537. {
  538. mylog(log_warn,"inner_mp.size()>data_num\n");
  539. anti_replay.set_invaild(seq);
  540. goto end;
  541. }
  542. if((int)inner_mp.size()==data_num)
  543. about_to_fec=1;
  544. }
  545. else
  546. {
  547. if(mp[seq].data_num!=-1)
  548. {
  549. if((int)inner_mp.size()>data_num+1)
  550. {
  551. mylog(log_warn,"inner_mp.size()>data_num+1\n");
  552. anti_replay.set_invaild(seq);
  553. goto end;
  554. }
  555. if((int)inner_mp.size()>=mp[seq].data_num)
  556. {
  557. about_to_fec=1;
  558. }
  559. }
  560. }
  561. if(about_to_fec)
  562. {
  563. int group_data_num=mp[seq].data_num;
  564. int group_redundant_num=mp[seq].redundant_num;
  565. //mylog(log_error,"fec here!\n");
  566. if(type==0)
  567. {
  568. char *fec_tmp_arr[max_fec_packet_num+5]={0};
  569. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  570. {
  571. fec_tmp_arr[it->first]=fec_data[it->second].buf;
  572. }
  573. 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
  574. //this line should always succeed
  575. blob_decode.clear();
  576. for(int i=0;i<group_data_num;i++)
  577. {
  578. blob_decode.input(fec_tmp_arr[i],len);
  579. }
  580. if(blob_decode.output(output_n,output_s_arr,output_len_arr)==0)
  581. {
  582. mylog(log_warn,"blob_decode failed\n");
  583. //ready_for_output=0;
  584. anti_replay.set_invaild(seq);
  585. goto end;
  586. }
  587. assert(ready_for_output==0);
  588. ready_for_output=1;
  589. anti_replay.set_invaild(seq);
  590. }
  591. else//type==1
  592. {
  593. int max_len=-1;
  594. int fec_result_ok=1;
  595. int data_check_ok=1;
  596. int debug_num=inner_mp.size();
  597. int missed_packet[max_fec_packet_num+5];
  598. int missed_packet_counter=0;
  599. //outupt_s_arr_buf[max_fec_packet_num+5]={0};
  600. //memset(output_s_arr_buf,0,sizeof(output_s_arr_buf));//in efficient
  601. for(int i=0;i<group_data_num+group_redundant_num;i++)
  602. {
  603. output_s_arr_buf[i]=0;
  604. }
  605. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  606. {
  607. output_s_arr_buf[it->first]=fec_data[it->second].buf;
  608. if(fec_data[it->second].len<(int)sizeof(u16_t))
  609. {
  610. mylog(log_warn,"fec_data[it->second].len<(int)sizeof(u16_t)");
  611. data_check_ok=0;
  612. }
  613. if(fec_data[it->second].len > max_len)
  614. max_len=fec_data[it->second].len;
  615. }
  616. if(max_len!=mp[seq].len)
  617. {
  618. data_check_ok=0;
  619. mylog(log_warn,"max_len!=mp[seq].len");
  620. }
  621. if(data_check_ok==0)
  622. {
  623. //ready_for_output=0;
  624. mylog(log_warn,"data_check_ok==0\n");
  625. anti_replay.set_invaild(seq);
  626. goto end;
  627. }
  628. for(auto it=inner_mp.begin();it!=inner_mp.end();it++)
  629. {
  630. int tmp_idx=it->second;
  631. assert(max_len>=fec_data[tmp_idx].len);//guarenteed by data_check_ok
  632. memset(fec_data[tmp_idx].buf+fec_data[tmp_idx].len,0,max_len-fec_data[tmp_idx].len);
  633. }
  634. for(int i=0;i<group_data_num;i++)
  635. {
  636. if(output_s_arr_buf[i]==0 ||i==inner_index) //only missed packet +current packet
  637. {
  638. missed_packet[missed_packet_counter++]=i;
  639. }
  640. }
  641. mylog(log_trace,"fec done,%d %d,missed_packet_counter=%d\n",group_data_num,group_redundant_num,missed_packet_counter);
  642. assert(rs_decode2(group_data_num,group_data_num+group_redundant_num,output_s_arr_buf,max_len)==0);//this should always succeed
  643. for(int i=0;i<group_data_num;i++)
  644. {
  645. output_len_arr_buf[i]=read_u16(output_s_arr_buf[i]);
  646. output_s_arr_buf[i]+=sizeof(u16_t);
  647. if(output_len_arr_buf[i]>max_data_len)
  648. {
  649. 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);
  650. fec_result_ok=0;
  651. for(int i=0;i<missed_packet_counter;i++)
  652. {
  653. log_bare(log_warn,"%d ",missed_packet[i]);
  654. }
  655. log_bare(log_warn,"\n");
  656. //break;
  657. }
  658. }
  659. if(fec_result_ok)
  660. {
  661. output_n=group_data_num;
  662. if(decode_fast_send)
  663. {
  664. output_n=missed_packet_counter;
  665. for(int i=0;i<missed_packet_counter;i++)
  666. {
  667. output_s_arr_buf[i]=output_s_arr_buf[missed_packet[i]];
  668. output_len_arr_buf[i]=output_len_arr_buf[missed_packet[i]];
  669. }
  670. }
  671. output_s_arr=output_s_arr_buf;
  672. output_len_arr=output_len_arr_buf;
  673. assert(ready_for_output==0);
  674. ready_for_output=1;
  675. }
  676. else
  677. {
  678. //fec_not_ok:
  679. ready_for_output=0;
  680. }
  681. anti_replay.set_invaild(seq);
  682. }// end of type==1
  683. }
  684. else //not about_to_fec
  685. {
  686. if(decode_fast_send)
  687. {
  688. if(type==1&&data_num==0)
  689. {
  690. assert(ready_for_output==0);
  691. output_n=1;
  692. int check_len=read_u16(fec_data[index].buf);
  693. output_s_arr_buf[0]=fec_data[index].buf+sizeof(u16_t);
  694. output_len_arr_buf[0]=fec_data[index].len-sizeof(u16_t);
  695. if(output_len_arr_buf[0]!=check_len)
  696. {
  697. mylog(log_warn,"len mismatch %d %d\n",output_len_arr_buf[0],check_len);
  698. }
  699. output_s_arr=output_s_arr_buf;
  700. output_len_arr=output_len_arr_buf;
  701. ready_for_output=1;
  702. }
  703. }
  704. }
  705. end:
  706. index++;
  707. if(index==int(fec_buff_num)) index=0;
  708. return 0;
  709. }
  710. int fec_decode_manager_t::output(int &n,char ** &s_arr,int* &len_arr)
  711. {
  712. if(!ready_for_output)
  713. {
  714. n=-1;
  715. s_arr=0;
  716. len_arr=0;
  717. }
  718. else
  719. {
  720. ready_for_output=0;
  721. n=output_n;
  722. s_arr=output_s_arr;
  723. len_arr=output_len_arr;
  724. }
  725. return 0;
  726. }