forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
209 lines (174 loc) · 4.54 KB
/
request.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
package resp
import (
"io"
)
// RequestReader is used by servers to wrap a client connection and convert
// requests into commands.
type RequestReader struct {
r *bufioR
}
// NewRequestReader wraps any reader interface
func NewRequestReader(rd io.Reader) *RequestReader {
r := new(bufioR)
r.reset(mkStdBuffer(), rd)
return &RequestReader{r: r}
}
// Buffered returns the number of unread bytes.
func (r *RequestReader) Buffered() int {
return r.r.Buffered()
}
// Reset resets the reader to a new reader and recycles internal buffers.
func (r *RequestReader) Reset(rd io.Reader) {
r.r.Reset(rd)
}
// PeekCmd peeks the next command name.
func (r *RequestReader) PeekCmd() (string, error) {
return r.peekCmd(0)
}
// ReadCmd reads the next command. It optionally recycles the cmd passed.
func (r *RequestReader) ReadCmd(cmd *Command) (*Command, error) {
if cmd == nil {
cmd = new(Command)
} else {
cmd.Reset()
}
return cmd, readCommand(cmd, r.r)
}
// StreamCmd reads the next command as a stream.
func (r *RequestReader) StreamCmd(cmd *CommandStream) (*CommandStream, error) {
if cmd == nil {
cmd = new(CommandStream)
} else {
cmd.Reset()
}
return cmd, readCommand(cmd, r.r)
}
// SkipCmd skips the next command.
func (r *RequestReader) SkipCmd() error {
c, err := r.r.PeekByte()
if err != nil {
return err
}
if c != '*' {
_, err = r.r.ReadLine()
return err
}
n, err := r.r.ReadArrayLen()
if err != nil {
return err
}
if n < 1 {
return r.SkipCmd()
}
for i := 0; i < n; i++ {
if err := r.r.SkipBulk(); err != nil {
return err
}
}
return nil
}
// Close release resource
func (r *RequestReader) Close() error {
r.r.Close()
return nil
}
func (r *RequestReader) peekCmd(offset int) (string, error) {
line, err := r.r.PeekLine(offset)
if err != nil {
return "", err
}
offset += len(line)
if len(line) == 0 {
return "", nil
} else if line[0] != '*' {
return line.FirstWord(), nil
}
n, err := line.ParseSize('*', errInvalidMultiBulkLength)
if err != nil {
return "", err
}
if n < 1 {
return r.peekCmd(offset)
}
line, err = r.r.PeekLine(offset)
if err != nil {
return "", err
}
offset += len(line)
n, err = line.ParseSize('$', errInvalidBulkLength)
if err != nil {
return "", err
}
data, err := r.r.PeekN(offset, int(n))
return string(data), err
}
// --------------------------------------------------------------------
// RequestWriter is used by clients to send commands to servers.
type RequestWriter struct {
w *bufioW
}
// NewRequestWriter wraps any Writer interface
func NewRequestWriter(wr io.Writer) *RequestWriter {
w := new(bufioW)
w.reset(mkStdBuffer(), wr)
return &RequestWriter{w: w}
}
// Reset resets the writer with an new interface
func (w *RequestWriter) Reset(wr io.Writer) {
w.w.Reset(wr)
}
// Buffered returns the number of buffered bytes
func (w *RequestWriter) Buffered() int {
return w.w.Buffered()
}
// Flush flushes the output buffer. Call this after you have completed your pipeline
func (w *RequestWriter) Flush() error {
return w.w.Flush()
}
// WriteCmd writes a full command as part of a pipeline. To execute the pipeline,
// you must call Flush.
func (w *RequestWriter) WriteCmd(cmd string, args ...[]byte) {
w.w.AppendArrayLen(len(args) + 1)
w.w.AppendBulkString(cmd)
for _, arg := range args {
w.w.AppendBulk(arg)
}
}
// WriteCmdString writes a full command as part of a pipeline. To execute the pipeline,
// you must call Flush.
func (w *RequestWriter) WriteCmdString(cmd string, args ...string) {
w.w.AppendArrayLen(len(args) + 1)
w.w.AppendBulkString(cmd)
for _, arg := range args {
w.w.AppendBulkString(arg)
}
}
// WriteMultiBulkSize is a low-level method to write a multibulk size.
// For normal operation, use WriteCmd or WriteCmdString.
func (w *RequestWriter) WriteMultiBulkSize(n int) error {
if n < 0 {
return errInvalidMultiBulkLength
}
w.w.AppendArrayLen(n)
return nil
}
// WriteBulk is a low-level method to write a bulk.
// For normal operation, use WriteCmd or WriteCmdString.
func (w *RequestWriter) WriteBulk(b []byte) {
w.w.AppendBulk(b)
}
// WriteBulkString is a low-level method to write a bulk.
// For normal operation, use WriteCmd or WriteCmdString.
func (w *RequestWriter) WriteBulkString(s string) {
w.w.AppendBulkString(s)
}
// CopyBulk is a low-level method to copy a large bulk of data directly to the writer.
// For normal operation, use WriteCmd or WriteCmdString.
func (w *RequestWriter) CopyBulk(r io.Reader, n int64) error {
return w.w.CopyBulk(r, n)
}
// Close release resource
func (w *RequestWriter) Close() error {
w.w.Close()
return nil
}