@@ -18,21 +18,20 @@ const (
1818)
1919
2020var errInvalidMessage = & errProtocol {"invalid message" }
21- var errIncompletePacket = & errProtocol {"incomplete packet" }
2221
2322// ReadNextCommand reads the next command from the provided packet. It's
2423// possible that the packet contains multiple commands, or zero commands
2524// when the packet is incomplete.
26- // 'args' is an optional reusable buffer and it can be nil.
27- // 'argsout' are the output arguments for the command. 'kind' is the type of
28- // command that was read.
29- // 'stop' indicates that there are no more possible commands in the packet.
30- // 'err' is returned when a protocol error was encountered.
25+ // 'argsbuf' is an optional reusable buffer and it can be nil.
26+ // 'complete' indicates that a command was read. false means no more commands.
27+ // 'args' are the output arguments for the command.
28+ // 'kind' is the type of command that was read.
3129// 'leftover' is any remaining unused bytes which belong to the next command.
32- func ReadNextCommand (packet []byte , args [][]byte ) (
33- leftover []byte , argsout [][]byte , kind Kind , stop bool , err error ,
30+ // 'err' is returned when a protocol error was encountered.
31+ func ReadNextCommand (packet []byte , argsbuf [][]byte ) (
32+ complete bool , args [][]byte , kind Kind , leftover []byte , err error ,
3433) {
35- args = args [:0 ]
34+ args = argsbuf [:0 ]
3635 if len (packet ) > 0 {
3736 if packet [0 ] != '*' {
3837 if packet [0 ] == '$' {
@@ -44,47 +43,45 @@ func ReadNextCommand(packet []byte, args [][]byte) (
4443 for s , i := 1 , 1 ; i < len (packet ); i ++ {
4544 if packet [i ] == '\n' {
4645 if packet [i - 1 ] != '\r' {
47- println ("here" , i )
48- return packet , args [:0 ], Redis , true , errInvalidMultiBulkLength
46+ return false , args [:0 ], Redis , packet , errInvalidMultiBulkLength
4947 }
5048 count , ok := parseInt (packet [s : i - 1 ])
5149 if ! ok || count < 0 {
52- println ("there" , i , count , ok , "[" + string (packet [s :i - 1 ])+ "]" )
53- return packet , args [:0 ], Redis , true , errInvalidMultiBulkLength
50+ return false , args [:0 ], Redis , packet , errInvalidMultiBulkLength
5451 }
5552 i ++
5653 if count == 0 {
57- return packet [ i :] , args [:0 ], Redis , i == len ( packet ) , nil
54+ return true , args [:0 ], Redis , packet [ i :] , nil
5855 }
5956 nextArg:
6057 for j := 0 ; j < count ; j ++ {
6158 if i == len (packet ) {
6259 break
6360 }
6461 if packet [i ] != '$' {
65- return packet , args [:0 ], Redis , true ,
62+ return false , args [:0 ], Redis , packet ,
6663 & errProtocol {"expected '$', got '" +
6764 string (packet [i ]) + "'" }
6865 }
6966 for s := i + 1 ; i < len (packet ); i ++ {
7067 if packet [i ] == '\n' {
7168 if packet [i - 1 ] != '\r' {
72- return packet , args [:0 ], Redis , true , errInvalidBulkLength
69+ return false , args [:0 ], Redis , packet , errInvalidBulkLength
7370 }
7471 n , ok := parseInt (packet [s : i - 1 ])
7572 if ! ok || count <= 0 {
76- return packet , args [:0 ], Redis , true , errInvalidBulkLength
73+ return false , args [:0 ], Redis , packet , errInvalidBulkLength
7774 }
7875 i ++
7976 if len (packet )- i >= n + 2 {
8077 if packet [i + n ] != '\r' || packet [i + n + 1 ] != '\n' {
81- return packet , args [:0 ], Redis , true , errInvalidBulkLength
78+ return false , args [:0 ], Redis , packet , errInvalidBulkLength
8279 }
8380 args = append (args , packet [i :i + n ])
8481 i += n + 2
8582 if j == count - 1 {
8683 // done reading
87- return packet [ i :] , args , Redis , i == len ( packet ) , nil
84+ return true , args , Redis , packet [ i :] , nil
8885 }
8986 continue nextArg
9087 }
@@ -97,24 +94,24 @@ func ReadNextCommand(packet []byte, args [][]byte) (
9794 }
9895 }
9996 }
100- return packet , args [:0 ], Redis , true , errIncompletePacket
97+ return false , args [:0 ], Redis , packet , nil
10198}
10299
103- func readTile38Command (b []byte , argsbuf [][]byte ) (
104- leftover [] byte , args [][]byte , kind Kind , stop bool , err error ,
100+ func readTile38Command (packet []byte , argsbuf [][]byte ) (
101+ complete bool , args [][]byte , kind Kind , leftover [] byte , err error ,
105102) {
106- for i := 1 ; i < len (b ); i ++ {
107- if b [i ] == ' ' {
108- n , ok := parseInt (b [1 :i ])
103+ for i := 1 ; i < len (packet ); i ++ {
104+ if packet [i ] == ' ' {
105+ n , ok := parseInt (packet [1 :i ])
109106 if ! ok || n < 0 {
110- return b , args [:0 ], Tile38 , true , errInvalidMessage
107+ return false , args [:0 ], Tile38 , packet , errInvalidMessage
111108 }
112109 i ++
113- if len (b ) >= i + n + 2 {
114- if b [i + n ] != '\r' || b [i + n + 1 ] != '\n' {
115- return b , args [:0 ], Tile38 , true , errInvalidMessage
110+ if len (packet ) >= i + n + 2 {
111+ if packet [i + n ] != '\r' || packet [i + n + 1 ] != '\n' {
112+ return false , args [:0 ], Tile38 , packet , errInvalidMessage
116113 }
117- line := b [i : i + n ]
114+ line := packet [i : i + n ]
118115 reading:
119116 for len (line ) != 0 {
120117 if line [0 ] == '{' {
@@ -147,24 +144,24 @@ func readTile38Command(b []byte, argsbuf [][]byte) (
147144 args = append (args , line )
148145 break
149146 }
150- return b [ i + n + 2 :] , args , Tile38 , i == len ( b ) , nil
147+ return true , args , Tile38 , packet [ i + n + 2 :] , nil
151148 }
152149 break
153150 }
154151 }
155- return b , args [:0 ], Tile38 , true , errIncompletePacket
152+ return false , args [:0 ], Tile38 , packet , nil
156153}
157- func readTelnetCommand (b []byte , argsbuf [][]byte ) (
158- leftover [] byte , args [][]byte , kind Kind , stop bool , err error ,
154+ func readTelnetCommand (packet []byte , argsbuf [][]byte ) (
155+ complete bool , args [][]byte , kind Kind , leftover [] byte , err error ,
159156) {
160157 // just a plain text command
161- for i := 0 ; i < len (b ); i ++ {
162- if b [i ] == '\n' {
158+ for i := 0 ; i < len (packet ); i ++ {
159+ if packet [i ] == '\n' {
163160 var line []byte
164- if i > 0 && b [i - 1 ] == '\r' {
165- line = b [:i - 1 ]
161+ if i > 0 && packet [i - 1 ] == '\r' {
162+ line = packet [:i - 1 ]
166163 } else {
167- line = b [:i ]
164+ line = packet [:i ]
168165 }
169166 var quote bool
170167 var quotech byte
@@ -184,7 +181,7 @@ func readTelnetCommand(b []byte, argsbuf [][]byte) (
184181 }
185182 if c == '"' || c == '\'' {
186183 if i != 0 {
187- return b , args [:0 ], Telnet , true , errUnbalancedQuotes
184+ return false , args [:0 ], Telnet , packet , errUnbalancedQuotes
188185 }
189186 quotech = c
190187 quote = true
@@ -208,7 +205,7 @@ func readTelnetCommand(b []byte, argsbuf [][]byte) (
208205 args = append (args , nline )
209206 line = line [i + 1 :]
210207 if len (line ) > 0 && line [0 ] != ' ' {
211- return b , args [:0 ], Telnet , true , errUnbalancedQuotes
208+ return false , args [:0 ], Telnet , packet , errUnbalancedQuotes
212209 }
213210 continue outer
214211 } else if c == '\\' {
@@ -219,17 +216,17 @@ func readTelnetCommand(b []byte, argsbuf [][]byte) (
219216 nline = append (nline , c )
220217 }
221218 if quote {
222- return b , args [:0 ], Telnet , true , errUnbalancedQuotes
219+ return false , args [:0 ], Telnet , packet , errUnbalancedQuotes
223220 }
224221 if len (line ) > 0 {
225222 args = append (args , line )
226223 }
227224 break
228225 }
229- return b [ i + 1 :] , args , Telnet , i == len ( b ) , nil
226+ return true , args , Telnet , packet [ i + 1 :] , nil
230227 }
231228 }
232- return b , args [:0 ], Telnet , true , errIncompletePacket
229+ return false , args [:0 ], Telnet , packet , nil
233230}
234231
235232// AppendUint appends a Redis protocol uint64 to the input bytes.
0 commit comments