@@ -4,20 +4,17 @@ use log::{error, trace, warn};
44
55pub use self :: defragmentation_error:: DefragmentationError ;
66pub use self :: defragmented_message:: DefragmentedMessage ;
7- use self :: transaction:: Transaction ;
7+ pub use self :: transaction:: Transaction ;
88use crate :: parameters:: messaging:: handler:: IncomingMessage ;
99
1010mod defragmentation_error;
1111mod defragmented_message;
1212mod transaction;
1313
14- /// Defragments potentially fragmented EZSP frames.
15- #[ derive( Clone , Debug , Default , Eq , PartialEq , Hash ) ]
16- pub struct Defragmenter < const BACKPRESSURE_THRESHOLD : usize > {
17- inner : BTreeMap < u8 , Transaction > ,
18- }
14+ const BACKPRESSURE_THRESHOLD : usize = 2 ;
1915
20- impl < const BACKPRESSURE_THRESHOLD : usize > Defragmenter < BACKPRESSURE_THRESHOLD > {
16+ /// Defragment potentially fragmented EZSP frames.
17+ pub trait Defragment {
2118 /// Attempt to defragment the given frame.
2219 ///
2320 /// # Returns
@@ -30,14 +27,21 @@ impl<const BACKPRESSURE_THRESHOLD: usize> Defragmenter<BACKPRESSURE_THRESHOLD> {
3027 ///
3128 /// - [`DefragmentationError::StrayFragment`] if a follow-up fragment is received without an initial fragment.
3229 /// - [`DefragmentationError::DuplicateFragment`] if a duplicate fragment is received.
33- pub fn defragment (
30+ fn defragment (
31+ & mut self ,
32+ incoming_message : IncomingMessage ,
33+ ) -> Result < Option < DefragmentedMessage > , DefragmentationError > ;
34+ }
35+
36+ impl Defragment for BTreeMap < u8 , Transaction > {
37+ fn defragment (
3438 & mut self ,
3539 incoming_message : IncomingMessage ,
3640 ) -> Result < Option < DefragmentedMessage > , DefragmentationError > {
37- if self . inner . len ( ) > BACKPRESSURE_THRESHOLD {
41+ if self . len ( ) > BACKPRESSURE_THRESHOLD {
3842 warn ! (
3943 "Defragmenter backpressure threshold exceeded: {} transactions in progress." ,
40- self . inner . len( )
44+ self . len( )
4145 ) ;
4246 }
4347
@@ -48,8 +52,7 @@ impl<const BACKPRESSURE_THRESHOLD: usize> Defragmenter<BACKPRESSURE_THRESHOLD> {
4852 trace ! ( "Received initial fragment." ) ;
4953
5054 if size > 1 {
51- self . inner
52- . insert ( seq, Transaction :: new ( size, index, incoming_message) ) ;
55+ self . insert ( seq, Transaction :: new ( size, index, incoming_message) ) ;
5356 return Ok ( None ) ;
5457 }
5558
@@ -59,13 +62,13 @@ impl<const BACKPRESSURE_THRESHOLD: usize> Defragmenter<BACKPRESSURE_THRESHOLD> {
5962 Some ( ( index, None ) ) => {
6063 trace ! ( "Received follow-up fragment." ) ;
6164
62- let Some ( transaction) = self . inner . get_mut ( & seq) else {
65+ let Some ( transaction) = self . get_mut ( & seq) else {
6366 return Err ( DefragmentationError :: StrayFragment { seq, index } ) ;
6467 } ;
6568
6669 if let Some ( message) = transaction. update ( index, incoming_message) {
6770 error ! ( "Duplicate fragment received. Previous was: {message:?}" ) ;
68- self . inner . remove ( & seq) ;
71+ self . remove ( & seq) ;
6972 return Err ( DefragmentationError :: DuplicateFragment { seq, index } ) ;
7073 }
7174
@@ -77,7 +80,7 @@ impl<const BACKPRESSURE_THRESHOLD: usize> Defragmenter<BACKPRESSURE_THRESHOLD> {
7780 return Ok ( None ) ;
7881 } ;
7982
80- self . inner . remove ( & seq) ;
83+ self . remove ( & seq) ;
8184 Ok ( Some ( defragmented_message) )
8285 }
8386 None => {
0 commit comments