@@ -65,11 +65,29 @@ impl DecodeBuffer {
6565 // If the readable portion of the buffer would have less than `encode_buf_min_free_space`
6666 // Or if we would return less than 8 readable bytes we don't allow further writing into
6767 // the current buffer, caller must swap.
68- // TODO: Define what is a sensible amount of minimum bytes to be read at any given moment.
69- if self . writeable_len ( ) < 8 {
70- return None ;
68+ if self . is_writeable ( ) {
69+ unsafe { Some ( self . buffer . get_slice ( self . write_offset , self . buffer . len ( ) ) ) }
70+ } else {
71+ None
7172 }
72- unsafe { Some ( self . buffer . get_slice ( self . write_offset , self . buffer . len ( ) ) ) }
73+ }
74+
75+ /// True if there is sufficient amount of writeable bytes
76+ pub ( crate ) fn is_writeable ( & mut self ) -> bool {
77+ self . writeable_len ( ) > 8
78+ }
79+
80+ /// Returns true if there is data to be decoded, else false
81+ pub ( crate ) fn has_frame ( & mut self ) -> io:: Result < bool > {
82+ if self . decode_frame_head ( ) . is_err ( ) {
83+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "framing error" ) ) ;
84+ }
85+ if let Some ( head) = & self . next_frame_head {
86+ if self . readable_len ( ) >= head. content_length ( ) {
87+ return Ok ( true ) ;
88+ }
89+ }
90+ Ok ( false )
7391 }
7492
7593 /// Swaps the underlying buffer in place with other
@@ -86,7 +104,10 @@ impl DecodeBuffer {
86104 if let Some ( mut overflow_chunk) = overflow {
87105 // TODO: change the config parameter to a separate value?
88106 let overflow_len = overflow_chunk. remaining ( ) ;
89- if self . writeable_len ( ) - overflow_len > self . buffer_config . encode_buf_min_free_space {
107+ if overflow_len < self . writeable_len ( )
108+ && self . writeable_len ( ) - overflow_len
109+ > self . buffer_config . encode_buf_min_free_space
110+ {
90111 // Just copy the overflow_chunk bytes, no need to chain.
91112 // the overflow must not exceed the new buffers capacity
92113 unsafe {
@@ -145,47 +166,41 @@ impl DecodeBuffer {
145166
146167 /// Tries to decode one frame from the readable part of the buffer
147168 pub fn get_frame ( & mut self ) -> Result < Frame , FramingError > {
169+ self . decode_frame_head ( ) ?;
148170 if let Some ( head) = & self . next_frame_head {
149171 if self . readable_len ( ) >= head. content_length ( ) {
150172 let head = self . next_frame_head . take ( ) . unwrap ( ) ;
151- let chunk_lease = self . read_chunk_lease ( head. content_length ( ) ) ;
152- match head. frame_type ( ) {
173+ return match head. frame_type ( ) {
153174 // Frames with empty bodies should be handled in frame-head decoding below.
154175 FrameType :: Data => {
155- Data :: decode_from ( chunk_lease) . map_err ( |_| FramingError :: InvalidFrame )
176+ Data :: decode_from ( self . read_chunk_lease ( head. content_length ( ) ) )
177+ . map_err ( |_| FramingError :: InvalidFrame )
156178 }
157- FrameType :: StreamRequest => StreamRequest :: decode_from ( chunk_lease)
158- . map_err ( |_| FramingError :: InvalidFrame ) ,
159179 FrameType :: Hello => {
160- Hello :: decode_from ( chunk_lease) . map_err ( |_| FramingError :: InvalidFrame )
180+ Hello :: decode_from ( self . read_chunk_lease ( head. content_length ( ) ) )
181+ . map_err ( |_| FramingError :: InvalidFrame )
161182 }
162183 FrameType :: Start => {
163- Start :: decode_from ( chunk_lease) . map_err ( |_| FramingError :: InvalidFrame )
164- }
165- FrameType :: Ack => {
166- Ack :: decode_from ( chunk_lease) . map_err ( |_| FramingError :: InvalidFrame )
184+ Start :: decode_from ( self . read_chunk_lease ( head. content_length ( ) ) )
185+ . map_err ( |_| FramingError :: InvalidFrame )
167186 }
187+ // Frames without content match here for expediency, Decoder doesn't allow 0 length.
188+ FrameType :: Bye => Ok ( Frame :: Bye ( ) ) ,
189+ FrameType :: Ack => Ok ( Frame :: Ack ( ) ) ,
168190 _ => Err ( FramingError :: UnsupportedFrameType ) ,
169- }
170- } else {
171- Err ( FramingError :: NoData )
191+ } ;
172192 }
173- } else if self . readable_len ( ) >= FRAME_HEAD_LEN as usize {
193+ }
194+ Err ( FramingError :: NoData )
195+ }
196+
197+ fn decode_frame_head ( & mut self ) -> Result < ( ) , FramingError > {
198+ if self . next_frame_head . is_none ( ) && self . readable_len ( ) >= FRAME_HEAD_LEN as usize {
174199 let mut chunk_lease = self . read_chunk_lease ( FRAME_HEAD_LEN as usize ) ;
175200 let head = FrameHead :: decode_from ( & mut chunk_lease) ?;
176- if head. content_length ( ) == 0 {
177- match head. frame_type ( ) {
178- // Frames without content match here for expediency, Decoder doesn't allow 0 length.
179- FrameType :: Bye => Ok ( Frame :: Bye ( ) ) ,
180- _ => Err ( FramingError :: NoData ) ,
181- }
182- } else {
183- self . next_frame_head = Some ( head) ;
184- self . get_frame ( )
185- }
186- } else {
187- Err ( FramingError :: NoData )
201+ self . next_frame_head = Some ( head) ;
188202 }
203+ Ok ( ( ) )
189204 }
190205
191206 /// Extracts the readable portion (if any) from the active buffer as a ChunkLease
@@ -198,11 +213,6 @@ impl DecodeBuffer {
198213 }
199214 None
200215 }
201-
202- /// Destroys the DecodeBuffer and returns the BufferChunk
203- pub ( crate ) fn destroy ( self ) -> BufferChunk {
204- self . buffer
205- }
206216}
207217
208218#[ cfg( test) ]
0 commit comments