-
Notifications
You must be signed in to change notification settings - Fork 5
/
fec_codec.go
339 lines (284 loc) · 7.23 KB
/
fec_codec.go
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
package gouxp
import (
"encoding/binary"
"errors"
"fmt"
"sync"
"github.com/shaoyuan1943/gokcp"
"github.com/klauspost/reedsolomon"
)
// fec packet format:
// | sn | cmd | len | payload |
// | 4byte | 2byte | 2byte | ... |
// sn: packet sequence id
// cmd: flag data or parity
// len: payload length
// define
const (
FECDataShards = 3
FECParityShards = 2
)
// cmd
const (
fecCmdData = 0x0F
fecCmdParity = 0x0E
)
const (
fecResultSize = 50
fecDataTimeout = 10000 // 10min
)
// format
const (
fecLengthOffset = 2
fecHeaderOffset = 6
fecHeaderSize = fecHeaderOffset + fecLengthOffset
)
var (
ErrUnknownFecCmd = errors.New("unknown fec cmd")
ErrFecDataTimeout = errors.New("fec data timeout")
ErrNoFecData = errors.New("no fec data")
)
var fecBufferPool sync.Pool
func bufferFromPool(size int) []byte {
if fecBufferPool.New == nil {
fecBufferPool.New = func() interface{} {
buffer := make([]byte, size)
return buffer
}
}
buffer := fecBufferPool.Get().([]byte)
buffer = buffer[:0]
return buffer
}
func bufferBackPool(buffer []byte) {
if buffer != nil {
fecBufferPool.Put(buffer)
}
}
func isFECFormat(data []byte) bool {
if data == nil || len(data) <= 0 {
return false
}
fecCmd := binary.LittleEndian.Uint16(data[4:])
if int(fecCmd) != fecCmdData && int(fecCmd) != fecCmdParity {
return false
}
return true
}
type FecCodecEncoder struct {
codec reedsolomon.Encoder
q [][]byte
insertIndex int
nextSN int32
shards int
dataShards int
parityShards int
maxRawDataLen int
zero []byte
codecData [][]byte
offset int
bufferSize int
lastInsertTime uint32
}
func NewFecEncoder(dataShards, parityShards, bufferSize int) *FecCodecEncoder {
fecEncoder := &FecCodecEncoder{}
fecEncoder.shards = dataShards + parityShards
fecEncoder.dataShards = dataShards
fecEncoder.parityShards = parityShards
encoder, err := reedsolomon.New(fecEncoder.dataShards, fecEncoder.parityShards)
if err != nil {
panic(fmt.Sprintf("init fec encoder: %v", err))
}
fecEncoder.codec = encoder
fecEncoder.codecData = make([][]byte, fecEncoder.shards)
fecEncoder.bufferSize = bufferSize
fecEncoder.zero = make([]byte, bufferSize)
fecEncoder.q = make([][]byte, fecEncoder.shards)
for i := 0; i < fecEncoder.shards; i++ {
fecEncoder.q[i] = make([]byte, bufferSize)
fecEncoder.q[i] = fecEncoder.q[i][:0]
}
return fecEncoder
}
func (f *FecCodecEncoder) Encode(rawData []byte) (fecData [][]byte, err error) {
if rawData == nil || len(rawData) == 0 || len(rawData) > f.bufferSize {
panic("raw data length invalid")
}
n := len(rawData)
f.q[f.insertIndex] = f.q[f.insertIndex][:fecHeaderSize+n]
copy(f.q[f.insertIndex][fecHeaderSize:], rawData)
binary.LittleEndian.PutUint16(f.q[f.insertIndex][fecHeaderOffset:], uint16(n))
f.lastInsertTime = gokcp.SetupFromNowMS()
if n > f.maxRawDataLen {
f.maxRawDataLen = n
}
if (f.insertIndex + 1) == f.dataShards {
maxLen := f.maxRawDataLen + fecHeaderSize
for i := 0; i < (f.dataShards + f.parityShards); i++ {
if i >= f.dataShards {
f.q[i] = f.q[i][:maxLen]
f.codecData[i] = f.q[i][fecHeaderOffset:maxLen]
} else {
orgLen := len(f.q[i])
if orgLen < maxLen {
f.q[i] = f.q[i][:maxLen]
copy(f.q[i][orgLen:maxLen], f.zero)
}
f.codecData[i] = f.q[i][fecHeaderOffset:maxLen]
}
}
err = f.codec.Encode(f.codecData)
if err != nil {
return
}
for i := 0; i < (f.dataShards + f.parityShards); i++ {
if i >= f.dataShards {
f.markParity(f.q[i])
} else {
f.markData(f.q[i])
}
}
f.insertIndex = 0
f.maxRawDataLen = 0
fecData = f.q
err = nil
return
}
f.insertIndex++
return
}
func (f *FecCodecEncoder) markData(data []byte) {
binary.LittleEndian.PutUint32(data[:4], uint32(f.nextSN))
binary.LittleEndian.PutUint16(data[4:fecHeaderOffset], uint16(fecCmdData))
f.nextSN++
}
func (f *FecCodecEncoder) markParity(data []byte) {
binary.LittleEndian.PutUint32(data[:4], uint32(f.nextSN))
binary.LittleEndian.PutUint16(data[4:fecHeaderOffset], uint16(fecCmdParity))
f.nextSN++
}
type DataShards struct {
q [][]byte
o [][]byte
lastInsert uint32
insertIndex int
decoded bool
maxRawDataLen int
shardsCount int
}
type FecCodecDecoder struct {
codec reedsolomon.Encoder
shards int
dataShards int
parityShards int
rawData map[int]DataShards
result [][]byte
offset int
bufferSize int
}
func NewFecDecoder(dataShards, parityShards, bufferSize int) *FecCodecDecoder {
fecDecoder := &FecCodecDecoder{}
fecDecoder.shards = dataShards + parityShards
fecDecoder.dataShards = dataShards
fecDecoder.parityShards = parityShards
decoder, err := reedsolomon.New(dataShards, parityShards)
if err != nil {
panic(fmt.Sprintf("init fec decoder err: %v", err))
}
fecDecoder.codec = decoder
fecDecoder.rawData = make(map[int]DataShards)
fecDecoder.result = make([][]byte, fecResultSize)
fecDecoder.bufferSize = bufferSize
return fecDecoder
}
func (f *FecCodecDecoder) Decode(fecData []byte, now uint32) (rawData [][]byte, err error) {
if fecData == nil || len(fecData) == 0 || len(fecData) > f.bufferSize {
panic("raw data length invalid")
}
if !isFECFormat(fecData) {
return nil, ErrUnknownFecCmd
}
sn := binary.LittleEndian.Uint32(fecData)
startRange := int(sn) - (int(sn) % f.shards)
endRange := startRange + f.shards + 1
sumIndex := 0
for i := startRange; i < endRange; i++ {
sumIndex += i
}
ds, ok := f.rawData[sumIndex]
if !ok {
ds = DataShards{}
ds.o = make([][]byte, f.shards)
ds.q = make([][]byte, f.shards)
for i := 0; i < len(ds.q); i++ {
ds.o[i] = bufferFromPool(f.bufferSize)
ds.q[i] = ds.o[i]
}
}
ds.q[int(sn)-startRange] = ds.q[int(sn)-startRange][:len(fecData)]
copy(ds.q[int(sn)-startRange], fecData)
ds.shardsCount++
if len(fecData) > ds.maxRawDataLen {
ds.maxRawDataLen = len(fecData)
}
ds.lastInsert = now
f.rawData[sumIndex] = ds
f.result = f.result[:0]
for k, v := range f.rawData {
if v.decoded {
f.delShards(k)
continue
}
if len(f.result) >= fecResultSize || len(f.result)+f.dataShards >= fecResultSize {
break
}
if v.shardsCount >= f.dataShards {
codec := v.q
for i := 0; i < len(v.q); i++ {
d := v.q[i]
if len(d) > 0 {
sn = binary.LittleEndian.Uint32(d)
startRange = int(sn) - (int(sn) % f.shards)
codec[(int(sn) - startRange)] = d[fecHeaderOffset:]
}
}
err = f.codec.ReconstructData(codec)
if err != nil {
return
}
reconstructed := codec[:f.dataShards]
for i := 0; i < len(reconstructed); i++ {
n := binary.LittleEndian.Uint16(reconstructed[i])
if n > 0 {
reconstructed[i] = reconstructed[i][fecLengthOffset:]
reconstructed[i] = reconstructed[i][:n]
}
}
v.decoded = true
f.result = append(f.result, reconstructed...)
f.rawData[k] = v
continue
}
// timeout
if (now-v.lastInsert) > fecDataTimeout && !v.decoded {
f.delShards(k)
}
}
rawData = f.result
err = nil
return
}
func (f *FecCodecDecoder) delShards(sumIndex int) {
ds, ok := f.rawData[sumIndex]
if !ok {
return
}
for i := 0; i < f.shards; i++ {
if ds.o[i] != nil {
bufferBackPool(ds.o[i])
}
}
ds.q = nil
ds.o = nil
delete(f.rawData, sumIndex)
}