-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrap.go
98 lines (72 loc) · 1.65 KB
/
wrap.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
package sharpshooter
import (
"errors"
"github.com/soyum2222/sharpshooter/protocol"
"sync/atomic"
)
func (s *Sniper) wrapfec() {
loop:
remain := s.winSize - int32(len(s.ammoBag))
if remain <= 0 {
return
}
l := len(s.sendBuffer)
if l == 0 {
return
}
// anchor is mark sendCache current op index
var anchor int64
if int64(l) < int64(s.fece.dataShards)*s.packageSize {
anchor = int64(l)
} else {
anchor = int64(s.fece.dataShards) * s.packageSize
}
body := s.sendBuffer[:anchor]
// body will be copied
shard, err := s.fece.encode(body)
if err != nil {
s.errorContainer.Store(err)
select {
case <-s.errorSign:
default:
s.errorContainer.Store(errors.New(err.Error()))
s.chanCloser.closeChan(s.errorSign)
}
return
}
s.sendBuffer = removeByte(s.sendBuffer, int(anchor))
for _, v := range shard {
ammo := protocol.Ammo{
Id: atomic.AddUint32(&s.sendId, 1) - 1,
Kind: protocol.NORMAL,
Body: v,
}
s.addEffectivePacket(1)
s.ammoBag = append(s.ammoBag, &ammo)
}
goto loop
}
func (s *Sniper) wrapnoml() {
remain := s.winSize - int32(len(s.ammoBag))
for i := 0; i < int(remain); i++ {
l := len(s.sendBuffer)
if l == 0 {
return
}
// anchor is mark sendCache current op index
var anchor int64
if int64(l) < s.packageSize {
anchor = int64(l)
} else {
anchor = s.packageSize
}
id := atomic.AddUint32(&s.sendId, 1)
ammo := s.ammoPool.Get().(*protocol.Ammo)
ammo.Id = id - 1
ammo.Kind = protocol.NORMAL
ammo.Body = append(ammo.Body, s.sendBuffer[:anchor]...)
s.sendBuffer = removeByte(s.sendBuffer, int(anchor))
s.addEffectivePacket(1)
s.ammoBag = append(s.ammoBag, ammo)
}
}