-
Notifications
You must be signed in to change notification settings - Fork 6
/
ncrx.cpp
757 lines (723 loc) · 40.5 KB
/
ncrx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#include "ncrx.h"
extern sockaddr_in addr(unsigned int ip, unsigned short int port);
#define UNROLL_MATRIX_MULTIPLICATION_2(session, output, position, row_index, accumulator) \
accumulator = FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[0], session->_buffer[0].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[1], session->_buffer[1].pkt.buffer[position]),\
output[position] = accumulator
#define UNROLL_MATRIX_MULTIPLICATION_4(session, output, position, row_index, accumulator) \
accumulator = FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[0], session->_buffer[0].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[1], session->_buffer[1].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[2], session->_buffer[2].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[3], session->_buffer[3].pkt.buffer[position]),\
output[position] = accumulator
#define UNROLL_MATRIX_MULTIPLICATION_8(session, output, position, row_index, accumulator) \
accumulator = FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[0], session->_buffer[0].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[1], session->_buffer[1].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[2], session->_buffer[2].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[3], session->_buffer[3].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[4], session->_buffer[4].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[5], session->_buffer[5].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[6], session->_buffer[6].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[7], session->_buffer[7].pkt.buffer[position]),\
output[position] = accumulator
#define UNROLL_MATRIX_MULTIPLICATION_16(session, output, position, row_index, accumulator) \
accumulator = FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[0], session->_buffer[0].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[1], session->_buffer[1].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[2], session->_buffer[2].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[3], session->_buffer[3].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[4], session->_buffer[4].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[5], session->_buffer[5].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[6], session->_buffer[6].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[7], session->_buffer[7].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[8], session->_buffer[8].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[9], session->_buffer[9].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[10], session->_buffer[10].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[11], session->_buffer[11].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[12], session->_buffer[12].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[13], session->_buffer[13].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[14], session->_buffer[14].pkt.buffer[position]),\
accumulator ^= FiniteField::instance()->mul(session->_decoding_matrix[row_index].decode[15], session->_buffer[15].pkt.buffer[position]),\
output[position] = accumulator
rx_session_info::rx_session_info(sockaddr_in addr, unsigned char blk_size): _ADDR(addr),_MAX_BLOCK_SIZE(blk_size)
{
_state = rx_session_info::STATE::INIT_FAILURE;
_rank = 0;
try
{
_buffer = new PktBuffer[_MAX_BLOCK_SIZE];
for(unsigned char i = 0 ; i < _MAX_BLOCK_SIZE ; i++)
{
_buffer[i].delivered = false;
memset(_buffer[i].pkt.buffer, 0x0, sizeof(NetworkCodingPktBuffer));
}
}
catch(std::exception ex)
{
return;
}
_blk_seq = 0;
try
{
_decoding_matrix = new DecodingBuffer[_MAX_BLOCK_SIZE];
for(unsigned char index = 0 ; index < _MAX_BLOCK_SIZE ; index++)
{
_decoding_matrix[index].empty = true;
_decoding_matrix[index].encode = nullptr;
_decoding_matrix[index].decode = nullptr;
}
}
catch(std::exception &ex)
{
delete [] _buffer;
_buffer = nullptr;
return;
}
try
{
for(unsigned char index = 0 ; index < _MAX_BLOCK_SIZE ; index++)
{
_decoding_matrix[index].encode = new unsigned char[_MAX_BLOCK_SIZE];
memset(_decoding_matrix[index].encode, 0x0, _MAX_BLOCK_SIZE);
_decoding_matrix[index].decode = new unsigned char[_MAX_BLOCK_SIZE];
memset(_decoding_matrix[index].decode, 0x0, _MAX_BLOCK_SIZE);
}
}
catch(std::exception ex)
{
for(unsigned char index = 0 ; index < _MAX_BLOCK_SIZE ; index++)
{
if(_decoding_matrix[index].encode != nullptr)
{
delete [] _decoding_matrix[index].encode;
}
if(_decoding_matrix[index].decode != nullptr)
{
delete [] _decoding_matrix[index].decode;
}
}
delete [] _decoding_matrix;
_decoding_matrix = nullptr;
delete [] _buffer;
_buffer = nullptr;
}
_losses = 0;
_state = rx_session_info::STATE::INIT_SUCCESS;
}
rx_session_info::~rx_session_info()
{
if(_state == rx_session_info::STATE::INIT_FAILURE)
{
return;
}
for(unsigned char index = 0 ; index < _MAX_BLOCK_SIZE ; index++)
{
if(_decoding_matrix[index].encode != nullptr)
{
delete [] _decoding_matrix[index].encode;
}
if(_decoding_matrix[index].decode != nullptr)
{
delete [] _decoding_matrix[index].decode;
}
}
delete [] _decoding_matrix;
_decoding_matrix = nullptr;
delete [] _buffer;
_buffer = nullptr;
}
bool ncrx::_handle_original_packet(rx_session_info* const session_info, const unsigned char * const pkt, int size)
{
// Copy original packet to the relavant position in _buffer.
// For original pkt, GET_OUTER_BLK_SIZE(pkt) is _tx_cnt.
const unsigned short int index = GET_OUTER_BLK_SIZE(pkt);
if(session_info->_buffer[index].delivered == true)
{
if(index != 0)
{
std::cout<<"Something is wrong!!!!["<<0+index<<"]\n";
exit(-1);
}
return (session_info->_rank == GET_OUTER_BLK_SIZE(pkt)) && (GET_OUTER_FLAGS(session_info->_buffer[index].pkt.buffer) & OuterHeader::FLAGS_END_OF_BLK);
}
session_info->_buffer[index].delivered = false;
memcpy(session_info->_buffer[index].pkt.buffer, pkt, GET_OUTER_SIZE(pkt));
session_info->_decoding_matrix[index].empty = false;
memcpy(session_info->_decoding_matrix[index].encode, GET_INNER_CODE(pkt), session_info->_MAX_BLOCK_SIZE);
memcpy(session_info->_decoding_matrix[index].decode, GET_INNER_CODE(pkt), session_info->_MAX_BLOCK_SIZE);
bool send_ack = false;
if(session_info->_rank == GET_OUTER_BLK_SIZE(pkt))
{
if(_receive_callback != nullptr)
{
_receive_callback(GET_INNER_PAYLOAD(session_info->_buffer[index].pkt.buffer, session_info->_MAX_BLOCK_SIZE), GET_INNER_SIZE(session_info->_buffer[index].pkt.buffer), session_info->_ADDR);
session_info->_buffer[index].delivered = true; // The packet is delivered.
}
// All original packets in the block are received without losses.
// Send the ack.
if(GET_OUTER_FLAGS(session_info->_buffer[index].pkt.buffer) & OuterHeader::FLAGS_END_OF_BLK)
{
send_ack = true;
}
}
session_info->_rank++;
return send_ack;
}
int ncrx::_innovative(rx_session_info * const session_info, unsigned char* pkt)
{
// 1. find an empty slot.
int innovative_index = 0;
for( ; innovative_index < session_info->_MAX_BLOCK_SIZE ; innovative_index++)
{
if(session_info->_decoding_matrix[innovative_index].empty == true)
{
session_info->_decoding_matrix[innovative_index].empty = false;
memcpy(session_info->_decoding_matrix[innovative_index].encode, GET_INNER_CODE(pkt), session_info->_MAX_BLOCK_SIZE);
memset(session_info->_decoding_matrix[innovative_index].decode, 0x0, session_info->_MAX_BLOCK_SIZE);
session_info->_decoding_matrix[innovative_index].decode[innovative_index] = 1;
break;
}
}
if(innovative_index >= session_info->_MAX_BLOCK_SIZE)
{
return -1;
}
// 2. eliminate redundunt information of pkt
for(unsigned char buffer_index = 0 ; buffer_index < session_info->_MAX_BLOCK_SIZE ; buffer_index++)
{
if(session_info->_decoding_matrix[buffer_index].empty == true)
{
continue;
}
if(session_info->_decoding_matrix[innovative_index].encode[buffer_index] == 0)
{
continue;
}
if(buffer_index == innovative_index)
{
continue;
}
const unsigned char mul = session_info->_decoding_matrix[innovative_index].encode[buffer_index];
for(unsigned char code_index = 0 ; code_index < session_info->_MAX_BLOCK_SIZE ; code_index++)
{
session_info->_decoding_matrix[innovative_index].encode[code_index] = FiniteField::instance()->mul(session_info->_decoding_matrix[buffer_index].encode[code_index], mul) ^ session_info->_decoding_matrix[innovative_index].encode[code_index];
session_info->_decoding_matrix[innovative_index].decode[code_index] = FiniteField::instance()->mul(session_info->_decoding_matrix[buffer_index].decode[code_index], mul) ^ session_info->_decoding_matrix[innovative_index].decode[code_index];
}
}
// 3. check innovativity
if(session_info->_decoding_matrix[innovative_index].encode[innovative_index] == 0)
{
session_info->_decoding_matrix[innovative_index].empty = true;
memset(session_info->_decoding_matrix[innovative_index].encode, 0x0, session_info->_MAX_BLOCK_SIZE);
memset(session_info->_decoding_matrix[innovative_index].decode, 0x0, session_info->_MAX_BLOCK_SIZE);
return -1;
}
// 4. Make _decoding_matrix[innovative_index].encode[innovative_index] to be 1.
if(session_info->_decoding_matrix[innovative_index].encode[innovative_index] != 1)
{
const unsigned char mul = FiniteField::instance()->inv(session_info->_decoding_matrix[innovative_index].encode[innovative_index]);
for(unsigned char code_index = 0 ; code_index < session_info->_MAX_BLOCK_SIZE ; code_index++)
{
session_info->_decoding_matrix[innovative_index].encode[code_index] = FiniteField::instance()->mul(session_info->_decoding_matrix[innovative_index].encode[code_index], mul);
session_info->_decoding_matrix[innovative_index].decode[code_index] = FiniteField::instance()->mul(session_info->_decoding_matrix[innovative_index].decode[code_index], mul);
}
}
// 5. remove redundunt information from the buffer
for(unsigned char buffer_index = 0 ; buffer_index < session_info->_MAX_BLOCK_SIZE ; buffer_index++)
{
if(session_info->_decoding_matrix[buffer_index].empty == true)
{
continue;
}
if(session_info->_decoding_matrix[buffer_index].encode[innovative_index] == 0)
{
continue;
}
if(buffer_index == innovative_index)
{
continue;
}
const unsigned char mul = session_info->_decoding_matrix[buffer_index].encode[innovative_index];
for(unsigned char code_index = 0 ; code_index < session_info->_MAX_BLOCK_SIZE ; code_index++)
{
session_info->_decoding_matrix[buffer_index].encode[code_index] = FiniteField::instance()->mul( session_info->_decoding_matrix[innovative_index].encode[code_index], mul ) ^ \
session_info->_decoding_matrix[buffer_index].encode[code_index];
session_info->_decoding_matrix[buffer_index].decode[code_index] = FiniteField::instance()->mul( session_info->_decoding_matrix[innovative_index].decode[code_index], mul ) ^ \
session_info->_decoding_matrix[buffer_index].decode[code_index];
}
}
return innovative_index;
}
void ncrx::_decode(rx_session_info * const session_info, unsigned char* pkt, int size)
{
int innovative_index = _innovative(session_info, pkt);
if(innovative_index == -1)
{
return;
}
session_info->_buffer[innovative_index].delivered = false;
memcpy(session_info->_buffer[innovative_index].pkt.buffer, pkt, GET_OUTER_SIZE(pkt));
session_info->_rank++;
session_info->_losses++;
}
bool ncrx::_handle_remedy_packet(rx_session_info* const session_info, unsigned char *pkt, int size)
{
if(session_info->_rank == GET_OUTER_BLK_SIZE(pkt)+1)
{
return true;
}
_decode(session_info, pkt, size);
if(session_info->_rank == GET_OUTER_BLK_SIZE(pkt)+1)
{
for(unsigned char i = 0 ; i < session_info->_rank ; i++)
{
if(session_info->_buffer[i].delivered == false)
{
if((GET_OUTER_FLAGS(session_info->_buffer[i].pkt.buffer) & OuterHeader::FLAGS_ORIGINAL) == 0)
{
memcpy(session_info->_decoding_buffer.buffer, session_info->_buffer[i].pkt.buffer, OUTER_HEADER_SIZE);
// decoding
for(unsigned int decoding_position = OUTER_HEADER_SIZE ; decoding_position < GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) ; )
{
if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 1023)
{
_unroll_decode_1024(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=1024;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 511)
{
_unroll_decode_512(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=512;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 255)
{
_unroll_decode_256(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=256;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 127)
{
_unroll_decode_128(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=128;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 63)
{
_unroll_decode_64(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=64;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 31)
{
_unroll_decode_32(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=32;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 15)
{
_unroll_decode_16(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=16;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 7)
{
_unroll_decode_8(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=8;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 3)
{
_unroll_decode_4(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=4;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 1)
{
_unroll_decode_2(session_info, session_info->_decoding_buffer.buffer, decoding_position, i);
decoding_position+=2;
}
else if(GET_OUTER_SIZE(session_info->_buffer[i].pkt.buffer) - decoding_position > 0)
{
unsigned char accumulator;
switch(session_info->_MAX_BLOCK_SIZE)
{
case 2:
UNROLL_MATRIX_MULTIPLICATION_2(session_info, session_info->_decoding_buffer.buffer, decoding_position, i, accumulator);
break;
case 4:
UNROLL_MATRIX_MULTIPLICATION_4(session_info, session_info->_decoding_buffer.buffer, decoding_position, i, accumulator);
break;
case 8:
UNROLL_MATRIX_MULTIPLICATION_8(session_info, session_info->_decoding_buffer.buffer, decoding_position, i, accumulator);
break;
case 16:
UNROLL_MATRIX_MULTIPLICATION_16(session_info, session_info->_decoding_buffer.buffer, decoding_position, i, accumulator);
break;
}
decoding_position+=1;
}
}
if(_receive_callback != nullptr)
{
_receive_callback(GET_INNER_PAYLOAD(session_info->_decoding_buffer.buffer, session_info->_MAX_BLOCK_SIZE), GET_INNER_SIZE(session_info->_decoding_buffer.buffer), session_info->_ADDR);
session_info->_buffer[i].delivered = true; // The packet is delivered.
}
}
else
{
if(_receive_callback != nullptr)
{
_receive_callback(GET_INNER_PAYLOAD(session_info->_buffer[i].pkt.buffer, session_info->_MAX_BLOCK_SIZE), GET_INNER_SIZE(session_info->_buffer[i].pkt.buffer), session_info->_ADDR);
session_info->_buffer[i].delivered = true; // The packet is delivered.
}
}
}
}
return true;
}
return false;
}
void ncrx::_unroll_decode_2(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
unsigned char accumulator;
switch(session_info->_MAX_BLOCK_SIZE)
{
case 2:
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+1, row_index, accumulator);
break;
case 4:
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+1, row_index, accumulator);
break;
case 8:
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+1, row_index, accumulator);
break;
case 16:
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+1, row_index, accumulator);
break;
}
}
void ncrx::_unroll_decode_4(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
unsigned char accumulator;
switch(session_info->_MAX_BLOCK_SIZE)
{
case 2:
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+3, row_index, accumulator);
break;
case 4:
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+3, row_index, accumulator);
break;
case 8:
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+3, row_index, accumulator);
break;
case 16:
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+3, row_index, accumulator);
break;
}
}
void ncrx::_unroll_decode_8(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
unsigned char accumulator;
switch(session_info->_MAX_BLOCK_SIZE)
{
case 2:
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+7, row_index, accumulator);
break;
case 4:
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+7, row_index, accumulator);
break;
case 8:
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+7, row_index, accumulator);
break;
case 16:
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+7, row_index, accumulator);
break;
}
}
void ncrx::_unroll_decode_16(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
unsigned char accumulator;
switch(session_info->_MAX_BLOCK_SIZE)
{
case 2:
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+7, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+8, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+9, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+10, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+11, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+12, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+13, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+14, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_2(session_info, output, position+15, row_index, accumulator);
break;
case 4:
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+7, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+8, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+9, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+10, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+11, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+12, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+13, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+14, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_4(session_info, output, position+15, row_index, accumulator);
break;
case 8:
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+7, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+8, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+9, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+10, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+11, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+12, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+13, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+14, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_8(session_info, output, position+15, row_index, accumulator);
break;
case 16:
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+1, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+2, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+3, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+4, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+5, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+6, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+7, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+8, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+9, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+10, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+11, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+12, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+13, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+14, row_index, accumulator);
UNROLL_MATRIX_MULTIPLICATION_16(session_info, output, position+15, row_index, accumulator);
break;
}
}
void ncrx::_unroll_decode_32(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_16(session_info, output, position, row_index);
_unroll_decode_16(session_info, output, position+16, row_index);
}
void ncrx::_unroll_decode_64(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_16(session_info, output, position, row_index);
_unroll_decode_16(session_info, output, position+16, row_index);
_unroll_decode_16(session_info, output, position+32, row_index);
_unroll_decode_16(session_info, output, position+48, row_index);
}
void ncrx::_unroll_decode_128(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_16(session_info, output, position, row_index);
_unroll_decode_16(session_info, output, position+16, row_index);
_unroll_decode_16(session_info, output, position+32, row_index);
_unroll_decode_16(session_info, output, position+48, row_index);
_unroll_decode_16(session_info, output, position+64, row_index);
_unroll_decode_16(session_info, output, position+80, row_index);
_unroll_decode_16(session_info, output, position+96, row_index);
_unroll_decode_16(session_info, output, position+112, row_index);
}
void ncrx::_unroll_decode_256(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_128(session_info, output, position, row_index);
_unroll_decode_128(session_info, output, position+128, row_index);
}
void ncrx::_unroll_decode_512(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_128(session_info, output, position, row_index);
_unroll_decode_128(session_info, output, position+128, row_index);
_unroll_decode_128(session_info, output, position+256, row_index);
_unroll_decode_128(session_info, output, position+384, row_index);
}
void ncrx::_unroll_decode_1024(rx_session_info * const session_info, unsigned char *output, unsigned short position, unsigned char row_index)
{
_unroll_decode_512(session_info, output, position, row_index);
_unroll_decode_512(session_info, output, position+512, row_index);
}
ncrx::ncrx(int socket, std::function<void (unsigned char *, unsigned int, sockaddr_in addr)> rx):_SOCKET(socket), _receive_callback(rx){}
ncrx::~ncrx()
{
_server_session_info.perform_for_all_data([](rx_session_info* session){
delete session;
session = nullptr;
});
_server_session_info.clear();
}
void ncrx::_rx_handler(unsigned char* buffer, unsigned int size, sockaddr_in* sender_addr, unsigned int sender_addr_len)
{
if(buffer[0] == NC_PKT_TYPE::DATA_TYPE)
{
if(rand()%8 == 0)
{
// For test, introduce random packet loss of 10%.
return;
}
unsigned char* const _rx_buffer = buffer;
const ip_port_key key = {sender_addr->sin_addr.s_addr, sender_addr->sin_port};
rx_session_info** const lookup_result = _server_session_info.find(key);
rx_session_info* session_info = nullptr;
if(lookup_result == nullptr)
{
try
{
session_info = new rx_session_info((*sender_addr), GET_OUTER_MAX_BLK_SIZE(_rx_buffer));
}
catch(std::exception ex)
{
return;
}
if(session_info->_state == rx_session_info::STATE::INIT_FAILURE)
{
delete session_info;
return;
}
_server_session_info.insert(key, session_info);
}
else
{
if((*lookup_result)->_MAX_BLOCK_SIZE != GET_OUTER_MAX_BLK_SIZE(_rx_buffer))
{
rx_session_info* new_session_info = nullptr;
try
{
new_session_info = new rx_session_info((*sender_addr), GET_OUTER_MAX_BLK_SIZE(_rx_buffer));
}
catch(std::exception ex)
{
return;
}
if(session_info->_state == rx_session_info::STATE::INIT_FAILURE)
{
delete new_session_info;
return;
}
delete (*lookup_result);
(*lookup_result) = new_session_info;
}
session_info = (*lookup_result);
}
if(session_info->_state == rx_session_info::STATE::INIT_FAILURE){
return;
}
const unsigned short int blk_seq = GET_OUTER_BLK_SEQ(_rx_buffer);
/*
* A change on a block sequence number indicates start of new block.
* We need to flush rx buffers.
*/
if(session_info->_blk_seq != blk_seq)
{
for(int i = 0 ; i < session_info->_MAX_BLOCK_SIZE ; i++)
{
if(session_info->_buffer[i].delivered == false &&
session_info->_decoding_matrix[i].empty == false &&
(GET_OUTER_FLAGS(session_info->_buffer[i].pkt.buffer) & OuterHeader::FLAGS_ORIGINAL) > 0)
{
if(_receive_callback != nullptr)
{
_receive_callback(GET_INNER_PAYLOAD(session_info->_buffer[i].pkt.buffer, session_info->_MAX_BLOCK_SIZE), GET_INNER_SIZE(session_info->_buffer[i].pkt.buffer), session_info->_ADDR);
session_info->_buffer[i].delivered = true;
}
}
}
session_info->_rank = 0;
session_info->_blk_seq = blk_seq;
session_info->_losses = 0;
for(int i = 0 ; i < session_info->_MAX_BLOCK_SIZE ; i++)
{
session_info->_buffer[i].delivered = false;
memset(session_info->_buffer[i].pkt.buffer, 0x0, TOTAL_HEADER_SIZE(session_info->_MAX_BLOCK_SIZE)+MAX_PAYLOAD_SIZE(session_info->_MAX_BLOCK_SIZE));
session_info->_decoding_matrix[i].empty = true;
memset(session_info->_decoding_matrix[i].encode, 0x0, session_info->_MAX_BLOCK_SIZE);
memset(session_info->_decoding_matrix[i].decode, 0x0, session_info->_MAX_BLOCK_SIZE);
}
}
bool send_ack = false;
if(GET_OUTER_FLAGS(_rx_buffer) & OuterHeader::FLAGS_ORIGINAL)
{
send_ack = _handle_original_packet(session_info, _rx_buffer, size);
}
else
{
send_ack = _handle_remedy_packet(session_info, _rx_buffer, size);
}
if(send_ack)
{
Ack ack_pkt;
ack_pkt.type = NC_PKT_TYPE::ACK_TYPE;
ack_pkt.blk_seq = session_info->_blk_seq;
ack_pkt.losses = session_info->_losses;
if(sizeof(Ack) != sendto(_SOCKET, (void*)&ack_pkt, sizeof(Ack), 0, (sockaddr*)sender_addr, sender_addr_len))
{
std::cout<<"Could not send ack\n";
}
else
{
//printf("Send Ack %hu\n", ack_pkt.blk_seq);
}
}
}
else if(buffer[0] == NC_PKT_TYPE::REQ_CONNECT_TYPE)
{
Connect connect;
connect.type = NC_PKT_TYPE::REP_CONNECT_TYPE;
if(sizeof(Connect) != sendto(_SOCKET, (void*)&connect, sizeof(Connect), 0, (sockaddr*)sender_addr, sender_addr_len))
{
std::cout<<"Could not send connection reply\n";
}
}
}