-
Notifications
You must be signed in to change notification settings - Fork 9
/
decode.go
217 lines (195 loc) · 4.66 KB
/
decode.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
package protocol
import (
"bytes"
"encoding/binary"
"io"
"github.com/segmentio/encoding/json"
)
// CommandDecoder ...
type CommandDecoder interface {
Reset([]byte) error
Decode() (*Command, error)
}
// JSONCommandDecoder ...
type JSONCommandDecoder struct {
data []byte
messageCount int
prevNewLine int
numMessagesRead int
}
// NewJSONCommandDecoder ...
func NewJSONCommandDecoder(data []byte) *JSONCommandDecoder {
// Protocol message must be separated by exactly one `\n`.
messageCount := bytes.Count(data, []byte("\n")) + 1
if len(data) == 0 || data[len(data)-1] == '\n' {
// Protocol message must have zero or one `\n` at the end.
messageCount--
}
return &JSONCommandDecoder{
data: data,
messageCount: messageCount,
prevNewLine: 0,
numMessagesRead: 0,
}
}
// Reset ...
func (d *JSONCommandDecoder) Reset(data []byte) error {
// We have a strict contract that protocol messages should be separated by at most one `\n`.
messageCount := bytes.Count(data, []byte("\n")) + 1
if len(data) == 0 || data[len(data)-1] == '\n' {
// We have a strict contract that protocol message should use at most one `\n` at the end.
messageCount--
}
d.data = data
d.messageCount = messageCount
d.prevNewLine = 0
d.numMessagesRead = 0
return nil
}
// Decode ...
func (d *JSONCommandDecoder) Decode() (*Command, error) {
if d.messageCount == 0 {
return nil, io.ErrUnexpectedEOF
}
var c Command
if d.messageCount == 1 {
_, err := json.Parse(d.data, &c, json.ZeroCopy)
if err != nil {
return nil, err
}
return &c, io.EOF
}
var nextNewLine int
if d.numMessagesRead == d.messageCount-1 {
// Last message, no need to search for a new line.
nextNewLine = len(d.data[d.prevNewLine:])
} else if len(d.data) > d.prevNewLine {
nextNewLine = bytes.Index(d.data[d.prevNewLine:], []byte("\n"))
if nextNewLine < 0 {
return nil, io.ErrShortBuffer
}
} else {
return nil, io.ErrShortBuffer
}
if len(d.data) >= d.prevNewLine+nextNewLine {
_, err := json.Parse(d.data[d.prevNewLine:d.prevNewLine+nextNewLine], &c, json.ZeroCopy)
if err != nil {
return nil, err
}
d.numMessagesRead++
d.prevNewLine = d.prevNewLine + nextNewLine + 1
if d.numMessagesRead == d.messageCount {
return &c, io.EOF
}
return &c, nil
} else {
return nil, io.ErrShortBuffer
}
}
// ProtobufCommandDecoder ...
type ProtobufCommandDecoder struct {
data []byte
offset int
}
// NewProtobufCommandDecoder ...
func NewProtobufCommandDecoder(data []byte) *ProtobufCommandDecoder {
return &ProtobufCommandDecoder{
data: data,
}
}
// Reset ...
func (d *ProtobufCommandDecoder) Reset(data []byte) error {
d.data = data
d.offset = 0
return nil
}
// Decode ...
func (d *ProtobufCommandDecoder) Decode() (*Command, error) {
if d.offset < len(d.data) {
var c Command
l, n := binary.Uvarint(d.data[d.offset:])
if n <= 0 {
return nil, io.EOF
}
from := d.offset + n
to := d.offset + n + int(l)
if to > 0 && to <= len(d.data) {
cmdBytes := d.data[from:to]
err := c.UnmarshalVT(cmdBytes)
if err != nil {
return nil, err
}
d.offset = to
if d.offset == len(d.data) {
err = io.EOF
}
return &c, err
} else {
return nil, io.ErrShortBuffer
}
}
return nil, io.EOF
}
// ReplyDecoder ...
type ReplyDecoder interface {
Reset([]byte) error
Decode() (*Reply, error)
}
var _ ReplyDecoder = NewJSONReplyDecoder(nil)
// JSONReplyDecoder ...
type JSONReplyDecoder struct {
decoder *json.Decoder
}
// NewJSONReplyDecoder ...
func NewJSONReplyDecoder(data []byte) *JSONReplyDecoder {
return &JSONReplyDecoder{
decoder: json.NewDecoder(bytes.NewReader(data)),
}
}
// Reset ...
func (d *JSONReplyDecoder) Reset(data []byte) error {
d.decoder = json.NewDecoder(bytes.NewReader(data))
return nil
}
// Decode ...
func (d *JSONReplyDecoder) Decode() (*Reply, error) {
var c Reply
err := d.decoder.Decode(&c)
if err != nil {
return nil, err
}
return &c, nil
}
var _ ReplyDecoder = NewProtobufReplyDecoder(nil)
// ProtobufReplyDecoder ...
type ProtobufReplyDecoder struct {
data []byte
offset int
}
// NewProtobufReplyDecoder ...
func NewProtobufReplyDecoder(data []byte) *ProtobufReplyDecoder {
return &ProtobufReplyDecoder{
data: data,
}
}
// Reset ...
func (d *ProtobufReplyDecoder) Reset(data []byte) error {
d.data = data
d.offset = 0
return nil
}
// Decode ...
func (d *ProtobufReplyDecoder) Decode() (*Reply, error) {
if d.offset < len(d.data) {
var c Reply
l, n := binary.Uvarint(d.data[d.offset:])
replyBytes := d.data[d.offset+n : d.offset+n+int(l)]
err := c.UnmarshalVT(replyBytes)
if err != nil {
return nil, err
}
d.offset = d.offset + n + int(l)
return &c, nil
}
return nil, io.EOF
}