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_COMMON_H_ 11 #define NET_DCSCTP_PACKET_CHUNK_DATA_COMMON_H_ 12 #include <stdint.h> 13 14 #include <utility> 15 #include <vector> 16 17 #include "api/array_view.h" 18 #include "net/dcsctp/packet/chunk/chunk.h" 19 #include "net/dcsctp/packet/data.h" 20 21 namespace dcsctp { 22 23 // Base class for DataChunk and IDataChunk 24 class AnyDataChunk : public Chunk { 25 public: 26 // Represents the "immediate ack" flag on DATA/I-DATA, from RFC7053. 27 using ImmediateAckFlag = webrtc::StrongAlias<class ImmediateAckFlagTag, bool>; 28 29 // Data chunk options. 30 // See https://tools.ietf.org/html/rfc4960#section-3.3.1 31 struct Options { 32 Data::IsEnd is_end = Data::IsEnd(false); 33 Data::IsBeginning is_beginning = Data::IsBeginning(false); 34 IsUnordered is_unordered = IsUnordered(false); 35 ImmediateAckFlag immediate_ack = ImmediateAckFlag(false); 36 }; 37 tsn()38 TSN tsn() const { return tsn_; } 39 options()40 Options options() const { 41 Options options; 42 options.is_end = data_.is_end; 43 options.is_beginning = data_.is_beginning; 44 options.is_unordered = data_.is_unordered; 45 options.immediate_ack = immediate_ack_; 46 return options; 47 } 48 stream_id()49 StreamID stream_id() const { return data_.stream_id; } ssn()50 SSN ssn() const { return data_.ssn; } message_id()51 MID message_id() const { return data_.message_id; } fsn()52 FSN fsn() const { return data_.fsn; } ppid()53 PPID ppid() const { return data_.ppid; } payload()54 rtc::ArrayView<const uint8_t> payload() const { return data_.payload; } 55 56 // Extracts the Data from the chunk, as a destructive action. extract()57 Data extract() && { return std::move(data_); } 58 AnyDataChunk(TSN tsn,StreamID stream_id,SSN ssn,MID message_id,FSN fsn,PPID ppid,std::vector<uint8_t> payload,const Options & options)59 AnyDataChunk(TSN tsn, 60 StreamID stream_id, 61 SSN ssn, 62 MID message_id, 63 FSN fsn, 64 PPID ppid, 65 std::vector<uint8_t> payload, 66 const Options& options) 67 : tsn_(tsn), 68 data_(stream_id, 69 ssn, 70 message_id, 71 fsn, 72 ppid, 73 std::move(payload), 74 options.is_beginning, 75 options.is_end, 76 options.is_unordered), 77 immediate_ack_(options.immediate_ack) {} 78 AnyDataChunk(TSN tsn,Data data,bool immediate_ack)79 AnyDataChunk(TSN tsn, Data data, bool immediate_ack) 80 : tsn_(tsn), data_(std::move(data)), immediate_ack_(immediate_ack) {} 81 82 protected: 83 // Bits in `flags` header field. 84 static constexpr int kFlagsBitEnd = 0; 85 static constexpr int kFlagsBitBeginning = 1; 86 static constexpr int kFlagsBitUnordered = 2; 87 static constexpr int kFlagsBitImmediateAck = 3; 88 89 private: 90 TSN tsn_; 91 Data data_; 92 ImmediateAckFlag immediate_ack_; 93 }; 94 95 } // namespace dcsctp 96 97 #endif // NET_DCSCTP_PACKET_CHUNK_DATA_COMMON_H_ 98