@@ -15,14 +15,22 @@ parity packet.
15
15
#include "sendif.h"
16
16
#include "structs.h"
17
17
#include <time.h>
18
+ #include "fec.h"
18
19
19
- //Emit a parity packet every FEC_M packets
20
- #define FEC_M 3
20
+ extern FecGenerator fecGenParity ;
21
+
22
+ static FecGenerator * gens []= {
23
+ & fecGenParity ,
24
+ NULL
25
+ };
26
+
27
+ static FecGenerator * currGen = NULL ;
28
+
29
+ static int currK , currN ;
21
30
22
31
static int sendMaxPktLen ;
23
32
static SendCb * sendCb ;
24
33
static int serial = 0 ;
25
- static uint8_t * parPacket ;
26
34
27
35
static time_t tsLastSaved ;
28
36
@@ -31,50 +39,52 @@ static time_t tsLastSaved;
31
39
void fecInit (SendCb * cb , int maxlen ) {
32
40
sendCb = cb ;
33
41
sendMaxPktLen = maxlen ;
34
- parPacket = malloc (maxlen );
35
- memset (parPacket , 0 , maxlen );
36
42
char buff [128 ];
37
43
FILE * f = fopen (TSFILE , "r" );
38
44
if (f != NULL ) {
39
45
fgets (buff , 127 , f );
40
46
serial = atoi (buff );
41
47
fclose (f );
42
48
}
49
+ if (serial == 0 ) serial = 1 ; //because serial==0 is special
43
50
tsLastSaved = time (NULL );
51
+ currGen = gens [0 ];
52
+ currK = 3 ;
53
+ currN = 4 ;
54
+ currGen -> init (currK , currN , maxlen );
44
55
}
45
56
46
-
47
- void fecSend (uint8_t * packet , size_t len ) {
48
- int fecMaxPacketLen = (sendMaxPktLen - sizeof (FecPacket )); //max data in a fec packet
49
- FecPacket * p = malloc (sizeof (FecPacket )+ fecMaxPacketLen );
57
+ void fecSendFecced (uint8_t * packet , size_t len ) {
58
+ FecPacket * p = malloc (sizeof (FecPacket )+ len );
50
59
p -> serial = htonl (serial );
51
60
memcpy (p -> data , packet , len );
52
- //Clear rest of packet to not leak stuff
53
- memset (p -> data + len , 0 , fecMaxPacketLen - len );
54
- //Add packet to parity
55
- for (int x = 0 ; x < fecMaxPacketLen ; x ++ ) parPacket [x ]^=p -> data [x ];
56
- sendCb ((uint8_t * )p , sendMaxPktLen );
61
+ sendCb ((uint8_t * )p , sizeof (FecPacket )+ len );
57
62
serial ++ ;
58
- //See if it's time to send the parity packet already.
59
- if ((serial % (FEC_M + 1 )) == FEC_M ) {
60
- //Yes it is! Re-use p to send parity data.
61
- p -> serial = htonl (serial );
62
- memcpy (p -> data , parPacket , fecMaxPacketLen );
63
- sendCb ((uint8_t * )p , sendMaxPktLen );
64
- serial ++ ;
65
- //Zero out parity array for next set of packets.
66
- memset (parPacket , 0 , fecMaxPacketLen );
67
- }
68
- //Save timestamp in case of crash/quit every 10 secs
63
+ free (p );
64
+ }
65
+
66
+
67
+ void fecSend (uint8_t * packet , size_t len ) {
68
+ currGen -> send (packet , len , serial , fecSendFecced );
69
+ //Save timestamp every 10 secs in case of crash/quit
69
70
if (time (NULL )- tsLastSaved > 10 ) {
70
71
FILE * f ;
71
72
f = fopen (TSFILE ".tmp" , "w" );
72
73
fprintf (f , "%d" , (int )serial );
73
74
fclose (f );
74
75
rename (TSFILE ".tmp" , TSFILE );
75
76
tsLastSaved = time (NULL );
77
+
78
+ //Semi-hack: We use the same timer to send out the FEC parameters
79
+ FecPacket * p = malloc (sizeof (FecPacket )+ sizeof (FecDesc ));
80
+ p -> serial = htonl (0 );
81
+ FecDesc * dsc = (FecDesc * )p -> data ;
82
+ dsc -> k = htons (currK );
83
+ dsc -> n = htons (currN );
84
+ dsc -> fecAlgoId = currGen -> genId ;
85
+ sendCb (p , sizeof (FecPacket )+ sizeof (FecDesc ));
86
+ free (p );
76
87
}
77
- free (p );
78
88
}
79
89
80
90
0 commit comments