-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
155 lines (127 loc) · 2.93 KB
/
reader.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
package memcache
type responseReader struct {
data []byte
begin uint64
end uint64
dataMask uint64
lastPos uint64
waitForEnd uint64
lastErr error
}
func newResponseReader(sizeLog int) *responseReader {
return &responseReader{
data: make([]byte, 1<<sizeLog),
begin: 0,
end: 0,
dataMask: 1<<sizeLog - 1,
lastPos: 0,
}
}
func (r *responseReader) getCap() uint64 {
return uint64(len(r.data))
}
func (r *responseReader) recv(data []byte) {
first := r.getIndex(r.end)
copy(r.data[first:], data)
max := r.getCap()
if first+uint64(len(data)) > max {
firstPart := max - first
copy(r.data, data[firstPart:])
}
r.end += uint64(len(data))
}
func (r *responseReader) getIndex(pos uint64) uint64 {
return pos & r.dataMask
}
func (r *responseReader) findFirstNumberPos(start uint64, end uint64) (uint64, error) {
for pos := start; pos < end; pos++ {
i := r.getIndex(pos)
if r.data[i] >= '0' && r.data[i] <= '9' {
return pos, nil
}
}
return end, ErrBrokenPipe{reason: "not a number after VA"}
}
func (r *responseReader) parseIntFrom(start uint64, end uint64) (uint64, error) {
result := uint64(0)
start, err := r.findFirstNumberPos(start, end)
if err != nil {
return 0, err
}
for pos := start; pos < end; pos++ {
i := r.getIndex(pos)
if r.data[i] < '0' || r.data[i] > '9' {
break
}
num := uint64(r.data[i] - '0')
result *= 10
result += num
}
return result, nil
}
func (r *responseReader) isCRLF(pos uint64) bool {
return r.data[r.getIndex(pos)] == '\r' && r.data[r.getIndex(pos+1)] == '\n'
}
func (r *responseReader) isVA(pos uint64) bool {
return r.data[r.getIndex(pos)] == 'V' && r.data[r.getIndex(pos+1)] == 'A'
}
func (r *responseReader) returnIfHasFullResponseData() bool {
if r.waitForEnd <= r.end {
r.lastPos = r.waitForEnd
r.waitForEnd = 0
return true
}
return false
}
func (r *responseReader) hasNext() bool {
if r.waitForEnd > 0 {
return r.returnIfHasFullResponseData()
}
for pos := r.lastPos; pos+1 < r.end; pos++ {
if !r.isCRLF(pos) {
continue
}
r.lastPos = pos + 2
if r.isVA(r.begin) {
dataLen, err := r.parseIntFrom(r.begin+2, pos)
if err != nil {
r.lastErr = err
return false
}
r.waitForEnd = r.lastPos + dataLen + 2
return r.returnIfHasFullResponseData()
}
return true
}
if r.end >= r.begin+2 {
r.lastPos = r.end - 2
}
return false
}
func (r *responseReader) readData(data []byte) []byte {
n := r.lastPos - r.begin
first := r.getIndex(r.begin)
max := r.getCap()
firstEnd := first + n
if firstEnd > max {
firstEnd = max
}
data = append(data, r.data[first:firstEnd]...)
if firstEnd == max {
firstPart := max - first
secondPart := n - firstPart
data = append(data, r.data[:secondPart]...)
}
r.begin = r.lastPos
return data
}
func (r *responseReader) hasError() error {
return r.lastErr
}
func (r *responseReader) reset() {
r.begin = 0
r.end = 0
r.lastPos = 0
r.waitForEnd = 0
r.lastErr = nil
}