11use crate :: error:: NatsError ;
2- use crate :: protocol:: { CommandError , Op } ;
2+ use crate :: protocol:: Op ;
33use bytes:: { BufMut , BytesMut } ;
44use tokio_codec:: { Decoder , Encoder } ;
55
@@ -41,14 +41,21 @@ impl Decoder for OpCodec {
4141 return Ok ( None ) ;
4242 }
4343
44- debug ! ( target: "nitox" , "codec buffer is {:?}" , buf) ;
44+ debug ! ( target: "nitox" , "next index: {}" , self . next_index) ;
45+
46+ //debug!(target: "nitox", "codec buffer is {:?}", buf);
4547 // Let's check if we find a blank space at the beginning
4648 if let Some ( command_offset) = buf[ self . next_index ..]
4749 . iter ( )
4850 . position ( |b| * b == b' ' || * b == b'\t' || * b == b'\r' )
4951 {
5052 let command_end = self . next_index + command_offset;
51- debug ! ( target: "nitox" , "codec detected command name {:?}" , & buf[ ..command_end] ) ;
53+ debug ! ( target: "nitox" , "command end: {}" , command_end) ;
54+ //debug!(target: "nitox", "codec detected command name {:?}", &buf[..command_end]);
55+ if !Op :: command_exists ( & buf[ ..command_end] ) {
56+ debug ! ( target: "nitox" , "command was incomplete" ) ;
57+ return Ok ( None ) ;
58+ }
5259
5360 if let Some ( command_body_offset) = buf[ command_end..] . windows ( 2 ) . position ( |w| w == b"\r \n " ) {
5461 let mut end_buf_pos = command_end + command_body_offset + 2 ;
@@ -60,29 +67,19 @@ impl Decoder for OpCodec {
6067 end_buf_pos += new_end + 2 ;
6168 } else {
6269 debug ! ( target: "nitox" , "command was incomplete" ) ;
63- self . next_index = buf. len ( ) ;
6470 return Ok ( None ) ;
6571 }
6672 }
6773
6874 debug ! ( target: "nitox" , "codec detected command body {:?}" , & buf[ ..end_buf_pos] ) ;
69- if !Op :: command_exists ( & buf[ ..command_end] ) {
70- debug ! ( target: "nitox" , "command was incomplete" ) ;
71- self . next_index = buf. len ( ) ;
72- return Ok ( None ) ;
73- }
7475
76+ let cmd_buf = buf. split_to ( end_buf_pos) ;
7577 debug ! ( target: "nitox" , "buffer now contains {:?}" , buf) ;
7678 self . next_index = 0 ;
77- match Op :: from_bytes ( buf[ ..command_end] . into ( ) , command_end) {
78- Err ( CommandError :: IncompleteCommandError ) => {
79- debug ! ( target: "nitox" , "command was incomplete" ) ;
80- self . next_index = buf. len ( ) ;
81- Ok ( None )
82- }
79+
80+ match Op :: from_bytes ( cmd_buf. freeze ( ) , command_end) {
8381 Ok ( op) => {
8482 debug ! ( target: "nitox" , "codec parsed command {:#?}" , op) ;
85- let _ = buf. split_to ( end_buf_pos) ;
8683 Ok ( Some ( op) )
8784 }
8885 Err ( e) => {
@@ -92,8 +89,7 @@ impl Decoder for OpCodec {
9289 }
9390 } else {
9491 debug ! ( target: "nitox" , "command was incomplete" ) ;
95- self . next_index = buf. len ( ) ;
96- return Ok ( None ) ;
92+ Ok ( None )
9793 }
9894 } else {
9995 // First blank not found yet, continuing
0 commit comments