break;
}
}
- //discard fragments received before the recently assembled frame
- begin_index = this_fragmentation->start_index;
- this_fragmentation->start_index = (end_index + 1) & MAXFRAGNUM_MASK;
- this_fragmentation->start_seq = (this_fragmentation->fragment[end_index].seq + 1) & (this_bundle->max_seq-1);
- //clear length and flags of the discarded fragments
- while (begin_index != this_fragmentation->start_index)
- {
- this_fragmentation->fragment[begin_index].flags = 0;
- this_fragmentation->fragment[begin_index].length = 0;
- begin_index = (begin_index + 1) & MAXFRAGNUM_MASK;
+
+discard_lost_frames:
+ //discard fragments numbererd below M and part of an unassembled frame,
+ //but not the one from the frame beginning just before M
+
+ // if we have something to discard
+ if (M_index != this_fragmentation->start_index)
+ {
+ // look for end of previous frame
+ // start at M-1, going backward
+ uint16_t index = M_index;
+ uint8_t end_or_hole_found = 0;
+
+ for (;;)
+ {
+ fragmentt *this_frag;
+
+ if (!end_or_hole_found)
+ {
+ fragmentt *front_frag = &this_fragmentation->frag[index];
+
+ // before a MP_BEGIN, there must be a MP_END
+ if (front_frag->length && (front_frag->flags & MP_BEGIN))
+ end_or_hole_found = 1;
+
+ index = (index + (MAXFRAGNUM-1)) & MAXFRAGNUM_MASK;
+ this_frag = &this_fragmentation->frag[index];
+
+ // we are a hole or a MP_END
+ if (!this_frag->length || (this_frag->flags & MP_END))
+ end_or_hole_found = 1;
+
+ if (end_or_hole_found)
+ end_index = index;
+ }
+
+ if (end_or_hole_found)
+ {
+ this_frag->flags = 0;
+ this_frag->length = 0;
+ }
+
+ if (index == this_fragmentation->start_index)
+ break;
+ }
+
+ this_fragmentation->start_index = (end_index + 1) & MAXFRAGNUM_MASK;
+ this_fragmentation->start_seq = (this_fragmentation->fragment[end_index].seq + 1) & (this_bundle->max_seq-1);
}
LOG(4, s, t, "MPPP after assembling: M index is =%d, start index is = %d, start seq=%d\n",M_index, this_fragmentation->start_index, this_fragmentation->start_seq);