@@ -64,6 +64,9 @@ enum State {
64
64
/// Compression is only applied to individual `ArrowMsg`s, instead of
65
65
/// the entire stream.
66
66
Message ( crate :: codec:: file:: MessageHeader ) ,
67
+
68
+ /// Stop reading
69
+ Aborted ,
67
70
}
68
71
69
72
impl StreamDecoder {
@@ -109,9 +112,31 @@ impl StreamDecoder {
109
112
fn try_read_impl ( & mut self ) -> Result < Option < LogMsg > , DecodeError > {
110
113
match self . state {
111
114
State :: StreamHeader => {
115
+ let is_first_header = self . chunks . num_read ( ) == 0 ;
112
116
if let Some ( header) = self . chunks . try_read ( FileHeader :: SIZE ) {
117
+ re_log:: trace!( ?header, "Decoding StreamHeader" ) ;
118
+
113
119
// header contains version and compression options
114
- let ( version, options) = options_from_bytes ( header) ?;
120
+ let ( version, options) = match options_from_bytes ( header) {
121
+ Ok ( ok) => ok,
122
+ Err ( err) => {
123
+ // We expected a header, but didn't find one!
124
+ if is_first_header {
125
+ return Err ( err) ;
126
+ } else {
127
+ re_log:: error!( "Trailing bytes in rrd stream: {header:?} ({err})" ) ;
128
+ self . state = State :: Aborted ;
129
+ return Ok ( None ) ;
130
+ }
131
+ }
132
+ } ;
133
+
134
+ re_log:: trace!(
135
+ version = version. to_string( ) ,
136
+ ?options,
137
+ "Found Stream Header"
138
+ ) ;
139
+
115
140
self . version = Some ( version) ;
116
141
self . options = options;
117
142
@@ -131,6 +156,9 @@ impl StreamDecoder {
131
156
. try_read ( crate :: codec:: file:: MessageHeader :: SIZE_BYTES )
132
157
{
133
158
let header = crate :: codec:: file:: MessageHeader :: from_bytes ( bytes) ?;
159
+
160
+ re_log:: trace!( ?header, "MessageHeader" ) ;
161
+
134
162
self . state = State :: Message ( header) ;
135
163
// we might have data left in the current chunk,
136
164
// immediately try to read the message content
@@ -139,23 +167,42 @@ impl StreamDecoder {
139
167
}
140
168
State :: Message ( header) => {
141
169
if let Some ( bytes) = self . chunks . try_read ( header. len as usize ) {
170
+ re_log:: trace!( ?header, "Read message" ) ;
171
+
142
172
let message = crate :: codec:: file:: decoder:: decode_bytes_to_app (
143
173
& mut self . app_id_cache ,
144
174
header. kind ,
145
175
bytes,
146
176
) ?;
177
+
147
178
if let Some ( mut message) = message {
179
+ re_log:: trace!(
180
+ "LogMsg::{}" ,
181
+ match message {
182
+ LogMsg :: SetStoreInfo { .. } => "SetStoreInfo" ,
183
+ LogMsg :: ArrowMsg { .. } => "ArrowMsg" ,
184
+ LogMsg :: BlueprintActivationCommand { .. } => {
185
+ "BlueprintActivationCommand"
186
+ }
187
+ }
188
+ ) ;
189
+
148
190
propagate_version ( & mut message, self . version ) ;
149
191
self . state = State :: MessageHeader ;
150
192
return Ok ( Some ( message) ) ;
151
193
} else {
194
+ re_log:: trace!( "End of stream - expecting a new Streamheader" ) ;
195
+
152
196
// `None` means end of stream, but there might be concatenated streams,
153
197
// so try to read another one.
154
198
self . state = State :: StreamHeader ;
155
199
return self . try_read ( ) ;
156
200
}
157
201
}
158
202
}
203
+ State :: Aborted => {
204
+ return Ok ( None ) ;
205
+ }
159
206
}
160
207
161
208
Ok ( None )
@@ -182,6 +229,9 @@ struct ChunkBuffer {
182
229
183
230
/// How many bytes of valid data are currently in `self.buffer`.
184
231
buffer_fill : usize ,
232
+
233
+ /// How many bytes have been read with [`Self::try_read`] so far?
234
+ num_read : usize ,
185
235
}
186
236
187
237
impl ChunkBuffer {
@@ -190,6 +240,7 @@ impl ChunkBuffer {
190
240
queue : VecDeque :: with_capacity ( 16 ) ,
191
241
buffer : Vec :: with_capacity ( 1024 ) ,
192
242
buffer_fill : 0 ,
243
+ num_read : 0 ,
193
244
}
194
245
}
195
246
@@ -200,6 +251,11 @@ impl ChunkBuffer {
200
251
self . queue . push_back ( Chunk :: new ( chunk) ) ;
201
252
}
202
253
254
+ /// How many bytes have been read with [`Self::try_read`] so far?
255
+ fn num_read ( & self ) -> usize {
256
+ self . num_read
257
+ }
258
+
203
259
/// Attempt to read exactly `n` bytes out of the queued chunks.
204
260
///
205
261
/// Returns `None` if there is not enough data to return a slice of `n` bytes.
@@ -239,7 +295,7 @@ impl ChunkBuffer {
239
295
// followed by another call to `try_read(N)` with the same `N`
240
296
// won't erroneously return the same bytes
241
297
self . buffer_fill = 0 ;
242
-
298
+ self . num_read += n ;
243
299
Some ( & self . buffer [ ..] )
244
300
} else {
245
301
None
0 commit comments