xref: /aosp_15_r20/external/webrtc/net/dcsctp/rx/reassembly_streams.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #ifndef NET_DCSCTP_RX_REASSEMBLY_STREAMS_H_
11 #define NET_DCSCTP_RX_REASSEMBLY_STREAMS_H_
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include <functional>
17 #include <vector>
18 
19 #include "absl/strings/string_view.h"
20 #include "api/array_view.h"
21 #include "net/dcsctp/common/sequence_numbers.h"
22 #include "net/dcsctp/packet/chunk/forward_tsn_common.h"
23 #include "net/dcsctp/packet/data.h"
24 #include "net/dcsctp/public/dcsctp_handover_state.h"
25 #include "net/dcsctp/public/dcsctp_message.h"
26 
27 namespace dcsctp {
28 
29 // Implementations of this interface will be called when data is received, when
30 // data should be skipped/forgotten or when sequence number should be reset.
31 //
32 // As a result of these operations - mainly when data is received - the
33 // implementations of this interface should notify when a message has been
34 // assembled, by calling the provided callback of type `OnAssembledMessage`. How
35 // it assembles messages will depend on e.g. if a message was sent on an ordered
36 // or unordered stream.
37 //
38 // Implementations will - for each operation - indicate how much additional
39 // memory that has been used as a result of performing the operation. This is
40 // used to limit the maximum amount of memory used, to prevent out-of-memory
41 // situations.
42 class ReassemblyStreams {
43  public:
44   // This callback will be provided as an argument to the constructor of the
45   // concrete class implementing this interface and should be called when a
46   // message has been assembled as well as indicating from which TSNs this
47   // message was assembled from.
48   using OnAssembledMessage =
49       std::function<void(rtc::ArrayView<const UnwrappedTSN> tsns,
50                          DcSctpMessage message)>;
51 
52   virtual ~ReassemblyStreams() = default;
53 
54   // Adds a data chunk to a stream as identified in `data`.
55   // If it was the last remaining chunk in a message, reassemble one (or
56   // several, in case of ordered chunks) messages.
57   //
58   // Returns the additional number of bytes added to the queue as a result of
59   // performing this operation. If this addition resulted in messages being
60   // assembled and delivered, this may be negative.
61   virtual int Add(UnwrappedTSN tsn, Data data) = 0;
62 
63   // Called for incoming FORWARD-TSN/I-FORWARD-TSN chunks - when the sender
64   // wishes the received to skip/forget about data up until the provided TSN.
65   // This is used to implement partial reliability, such as limiting the number
66   // of retransmissions or the an expiration duration. As a result of skipping
67   // data, this may result in the implementation being able to assemble messages
68   // in ordered streams.
69   //
70   // Returns the number of bytes removed from the queue as a result of
71   // this operation.
72   virtual size_t HandleForwardTsn(
73       UnwrappedTSN new_cumulative_ack_tsn,
74       rtc::ArrayView<const AnyForwardTsnChunk::SkippedStream>
75           skipped_streams) = 0;
76 
77   // Called for incoming (possibly deferred) RE_CONFIG chunks asking for
78   // either a few streams, or all streams (when the list is empty) to be
79   // reset - to have their next SSN or Message ID to be zero.
80   virtual void ResetStreams(rtc::ArrayView<const StreamID> stream_ids) = 0;
81 
82   virtual HandoverReadinessStatus GetHandoverReadiness() const = 0;
83   virtual void AddHandoverState(DcSctpSocketHandoverState& state) = 0;
84   virtual void RestoreFromState(const DcSctpSocketHandoverState& state) = 0;
85 };
86 
87 }  // namespace dcsctp
88 
89 #endif  // NET_DCSCTP_RX_REASSEMBLY_STREAMS_H_
90