fec_manager.cpp 27 KB

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