xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/data_chunk.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_PACKET_CHUNK_DATA_CHUNK_H_
11 #define NET_DCSCTP_PACKET_CHUNK_DATA_CHUNK_H_
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #include <cstdint>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/strings/string_view.h"
21 #include "api/array_view.h"
22 #include "net/dcsctp/packet/chunk/chunk.h"
23 #include "net/dcsctp/packet/chunk/data_common.h"
24 #include "net/dcsctp/packet/data.h"
25 #include "net/dcsctp/packet/tlv_trait.h"
26 
27 namespace dcsctp {
28 
29 // https://tools.ietf.org/html/rfc4960#section-3.3.1
30 struct DataChunkConfig : ChunkConfig {
31   static constexpr int kType = 0;
32   static constexpr size_t kHeaderSize = 16;
33   static constexpr size_t kVariableLengthAlignment = 1;
34 };
35 
36 class DataChunk : public AnyDataChunk, public TLVTrait<DataChunkConfig> {
37  public:
38   static constexpr int kType = DataChunkConfig::kType;
39 
40   // Exposed to allow the retransmission queue to make room for the correct
41   // header size.
42   static constexpr size_t kHeaderSize = DataChunkConfig::kHeaderSize;
43 
DataChunk(TSN tsn,StreamID stream_id,SSN ssn,PPID ppid,std::vector<uint8_t> payload,const Options & options)44   DataChunk(TSN tsn,
45             StreamID stream_id,
46             SSN ssn,
47             PPID ppid,
48             std::vector<uint8_t> payload,
49             const Options& options)
50       : AnyDataChunk(tsn,
51                      stream_id,
52                      ssn,
53                      MID(0),
54                      FSN(0),
55                      ppid,
56                      std::move(payload),
57                      options) {}
58 
DataChunk(TSN tsn,Data && data,bool immediate_ack)59   DataChunk(TSN tsn, Data&& data, bool immediate_ack)
60       : AnyDataChunk(tsn, std::move(data), immediate_ack) {}
61 
62   static absl::optional<DataChunk> Parse(rtc::ArrayView<const uint8_t> data);
63 
64   void SerializeTo(std::vector<uint8_t>& out) const override;
65   std::string ToString() const override;
66 };
67 
68 }  // namespace dcsctp
69 
70 #endif  // NET_DCSCTP_PACKET_CHUNK_DATA_CHUNK_H_
71