Skip to main content

rtci_sctp/association/
undelivered.rs

1//! The user messages an association that ended did not deliver: RFC 9260 section 8.1 ("the
2//! endpoint SHOULD report the failure to the upper layer and optionally report back all
3//! outstanding user data remaining in its outbound queue"), the SEND FAILURE notification
4//! (11.2.2) and the Receive Unsent / Receive Unacknowledged Message primitives (11.1.14,
5//! 11.1.15).
6
7use crate::chunk::chunk_payload_data::{ChunkPayloadData, MessageId, PayloadProtocolIdentifier};
8use bytes::{Bytes, BytesMut};
9use rustc_hash::FxHashMap;
10
11/// Whether any of an undelivered message left this side (RFC 9260 11.2.2, "mode").
12#[derive(Debug, Copy, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum UndeliveredMode {
15    /// No part of the message was ever sent (RECEIVE_UNSENT, RFC 9260 11.1.14).
16    Unsent,
17    /// At least part of the message was sent and it is not completely acknowledged
18    /// (RECEIVE_UNACKED, RFC 9260 11.1.15).
19    Unacknowledged,
20}
21
22/// A user message an association did not deliver, handed back by
23/// [`Association::take_undelivered_messages`](crate::Association::take_undelivered_messages)
24/// once the association ended.
25#[derive(Debug, Clone, PartialEq)]
26#[non_exhaustive]
27pub struct UndeliveredMessage {
28    /// The stream the message was written to.
29    pub stream_identifier: u16,
30    /// The Stream Sequence Number the message had (0 when it was never numbered).
31    pub stream_sequence_number: u16,
32    /// The Payload Protocol Identifier it was written with.
33    pub payload_type: PayloadProtocolIdentifier,
34    /// It was written unordered.
35    pub unordered: bool,
36    /// Unsent, or sent and not acknowledged.
37    pub mode: UndeliveredMode,
38    /// `data` is not the whole message: the peer acknowledged some of it, or some of it was
39    /// sent before (RFC 9260 11.1.14 / 11.1.15 "partial flag").
40    pub partial: bool,
41    /// The bytes of the message that were not acknowledged, in order.
42    pub data: Bytes,
43}
44
45impl UndeliveredMessage {
46    /// Joins the fragments of one message that remain (in TSN or queue order). `acked`
47    /// fragments (a Gap Ack Block covered them) leave a gap: the message is partial then.
48    pub(crate) fn from_fragments(
49        fragments: &[ChunkPayloadData],
50        mode: UndeliveredMode,
51    ) -> Option<Self> {
52        let first = fragments.first()?;
53        let last = fragments.last()?;
54        let mut data = BytesMut::new();
55        let mut partial = !first.beginning_fragment || !last.ending_fragment;
56        for c in fragments {
57            if c.acknowledged {
58                partial = true;
59            } else {
60                data.extend_from_slice(&c.user_data);
61            }
62        }
63        Some(UndeliveredMessage {
64            stream_identifier: first.stream_identifier,
65            stream_sequence_number: first.stream_sequence_number,
66            payload_type: first.payload_type,
67            unordered: first.unordered,
68            mode,
69            partial,
70            data: data.freeze(),
71        })
72    }
73}
74
75/// Turns the chunks an ended association holds into messages: `sent` chunks (in TSN order)
76/// first, then `unsent` messages (in the order written). The fragments of one message (its
77/// local id) go together wherever they are, so the unsent tail of a part-sent message joins
78/// its sent fragments; a message with a sent fragment is `Unacknowledged`. Abandoned chunks
79/// (reported with `StreamEvent::MessageAbandoned` already) are left out.
80pub(crate) fn undelivered_messages(
81    sent: Vec<ChunkPayloadData>,
82    unsent: Vec<Vec<ChunkPayloadData>>,
83) -> Vec<UndeliveredMessage> {
84    let mut groups: Vec<(Vec<ChunkPayloadData>, bool)> = Vec::new();
85    let mut by_id: FxHashMap<MessageId, usize> = FxHashMap::default();
86    let chunks = sent
87        .into_iter()
88        .map(|c| (c, true))
89        .chain(unsent.into_iter().flatten().map(|c| (c, false)));
90    for (c, was_sent) in chunks {
91        if c.abandoned() {
92            continue;
93        }
94        let index = match c.message_id {
95            Some(id) => *by_id.entry(id).or_insert_with(|| {
96                groups.push((Vec::new(), false));
97                groups.len() - 1
98            }),
99            None => {
100                groups.push((Vec::new(), false));
101                groups.len() - 1
102            }
103        };
104        groups[index].1 |= was_sent;
105        groups[index].0.push(c);
106    }
107    groups
108        .into_iter()
109        .filter_map(|(fragments, was_sent)| {
110            let mode = if was_sent {
111                UndeliveredMode::Unacknowledged
112            } else {
113                UndeliveredMode::Unsent
114            };
115            UndeliveredMessage::from_fragments(&fragments, mode)
116        })
117        .collect()
118}