-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
251 lines (226 loc) 路 5.38 KB
/
Copy pathprotocol.go
File metadata and controls
251 lines (226 loc) 路 5.38 KB
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
package main
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
)
const (
opPush byte = 1
opPop byte = 2
statusOK byte = 1
statusError byte = 2
protocolMaxChannels = 1024
protocolMaxErrorSize = 64 << 10
)
var protocolByteOrder = binary.BigEndian
func writeRequest(w *bufio.Writer, req request) error {
var op byte
switch req.Op {
case "push":
op = opPush
case "pop":
op = opPop
default:
return fmt.Errorf("unknown request op %q", req.Op)
}
if len(req.Channels) > protocolMaxChannels {
return fmt.Errorf("too many channels: %d", len(req.Channels))
}
if len(req.Payload) > defaultMaxBytes {
return fmt.Errorf("payload exceeds maximum size of %d bytes", defaultMaxBytes)
}
if err := w.WriteByte(op); err != nil {
return err
}
if err := binary.Write(w, protocolByteOrder, req.TimeoutMS); err != nil {
return err
}
if err := binary.Write(w, protocolByteOrder, uint16(len(req.Channels))); err != nil {
return err
}
for _, channel := range req.Channels {
if len(channel) > 0xffff {
return fmt.Errorf("channel too long: %d", len(channel))
}
if err := binary.Write(w, protocolByteOrder, uint16(len(channel))); err != nil {
return err
}
if _, err := io.WriteString(w, channel); err != nil {
return err
}
}
if err := binary.Write(w, protocolByteOrder, uint32(len(req.Payload))); err != nil {
return err
}
if _, err := w.Write(req.Payload); err != nil {
return err
}
return w.Flush()
}
func readRequest(r *bufio.Reader) (request, error) {
op, err := r.ReadByte()
if err != nil {
return request{}, err
}
var timeoutMS int64
if err := binary.Read(r, protocolByteOrder, &timeoutMS); err != nil {
return request{}, err
}
var channelCount uint16
if err := binary.Read(r, protocolByteOrder, &channelCount); err != nil {
return request{}, err
}
if int(channelCount) > protocolMaxChannels {
return request{}, fmt.Errorf("too many channels: %d", channelCount)
}
channels := make([]string, 0, channelCount)
for i := 0; i < int(channelCount); i++ {
channel, err := readString16(r)
if err != nil {
return request{}, err
}
channels = append(channels, channel)
}
payload, err := readBytes32(r, defaultMaxBytes)
if err != nil {
return request{}, err
}
req := request{
Channels: channels,
Payload: payload,
TimeoutMS: timeoutMS,
}
switch op {
case opPush:
req.Op = "push"
case opPop:
req.Op = "pop"
default:
return request{}, fmt.Errorf("unknown request op %d", op)
}
return req, nil
}
func writeResponse(w *bufio.Writer, resp response) error {
status := statusOK
if !resp.OK {
status = statusError
}
if len(resp.Payload) > defaultMaxBytes {
return fmt.Errorf("payload exceeds maximum size of %d bytes", defaultMaxBytes)
}
if len(resp.Channel) > 0xffff {
return fmt.Errorf("channel too long: %d", len(resp.Channel))
}
if len(resp.Error) > protocolMaxErrorSize {
return fmt.Errorf("error message too long: %d", len(resp.Error))
}
if err := w.WriteByte(status); err != nil {
return err
}
if err := writeString16(w, resp.Channel); err != nil {
return err
}
if err := writeBytes32(w, resp.Payload); err != nil {
return err
}
if err := writeString32(w, resp.Error); err != nil {
return err
}
return w.Flush()
}
func readResponse(r *bufio.Reader) (response, error) {
status, err := r.ReadByte()
if err != nil {
return response{}, err
}
channel, err := readString16(r)
if err != nil {
return response{}, err
}
payload, err := readBytes32(r, defaultMaxBytes)
if err != nil {
return response{}, err
}
errorText, err := readString32(r, protocolMaxErrorSize)
if err != nil {
return response{}, err
}
resp := response{
Channel: channel,
Payload: payload,
Error: errorText,
}
switch status {
case statusOK:
resp.OK = true
case statusError:
resp.OK = false
default:
return response{}, fmt.Errorf("unknown response status %d", status)
}
return resp, nil
}
func writeString16(w io.Writer, value string) error {
if len(value) > 0xffff {
return fmt.Errorf("string too long: %d", len(value))
}
if err := binary.Write(w, protocolByteOrder, uint16(len(value))); err != nil {
return err
}
_, err := io.WriteString(w, value)
return err
}
func writeString32(w io.Writer, value string) error {
if err := binary.Write(w, protocolByteOrder, uint32(len(value))); err != nil {
return err
}
_, err := io.WriteString(w, value)
return err
}
func writeBytes32(w io.Writer, value []byte) error {
if err := binary.Write(w, protocolByteOrder, uint32(len(value))); err != nil {
return err
}
_, err := w.Write(value)
return err
}
func readString16(r io.Reader) (string, error) {
var n uint16
if err := binary.Read(r, protocolByteOrder, &n); err != nil {
return "", err
}
buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return "", err
}
return string(buf), nil
}
func readString32(r io.Reader, max int) (string, error) {
buf, err := readBytes32(r, max)
if err != nil {
return "", err
}
return string(buf), nil
}
func readBytes32(r io.Reader, max int) ([]byte, error) {
var n uint32
if err := binary.Read(r, protocolByteOrder, &n); err != nil {
return nil, err
}
if int64(n) > int64(max) {
return nil, fmt.Errorf("frame too large: %d > %d", n, max)
}
if n == 0 {
return nil, nil
}
buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
if errors.Is(err, io.EOF) {
return nil, io.ErrUnexpectedEOF
}
return nil, err
}
return buf, nil
}