xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_coalesced_packet.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_CORE_QUIC_COALESCED_PACKET_H_
6 #define QUICHE_QUIC_CORE_QUIC_COALESCED_PACKET_H_
7 
8 #include "quiche/quic/core/quic_packets.h"
9 
10 namespace quic {
11 
12 namespace test {
13 class QuicCoalescedPacketPeer;
14 }
15 
16 // QuicCoalescedPacket is used to buffer multiple packets which can be coalesced
17 // into the same UDP datagram.
18 class QUICHE_EXPORT QuicCoalescedPacket {
19  public:
20   QuicCoalescedPacket();
21   ~QuicCoalescedPacket();
22 
23   // Returns true if |packet| is successfully coalesced with existing packets.
24   // Returns false otherwise.
25   bool MaybeCoalescePacket(const SerializedPacket& packet,
26                            const QuicSocketAddress& self_address,
27                            const QuicSocketAddress& peer_address,
28                            quiche::QuicheBufferAllocator* allocator,
29                            QuicPacketLength current_max_packet_length,
30                            QuicEcnCodepoint ecn_codepoint);
31 
32   // Clears this coalesced packet.
33   void Clear();
34 
35   // Clears all state associated with initial_packet_.
36   void NeuterInitialPacket();
37 
38   // Copies encrypted_buffers_ to |buffer| and sets |length_copied| to the
39   // copied amount. Returns false if copy fails (i.e., |buffer_len| is not
40   // enough).
41   bool CopyEncryptedBuffers(char* buffer, size_t buffer_len,
42                             size_t* length_copied) const;
43 
44   std::string ToString(size_t serialized_length) const;
45 
46   // Returns true if this coalesced packet contains packet of |level|.
47   bool ContainsPacketOfEncryptionLevel(EncryptionLevel level) const;
48 
49   // Returns transmission type of packet of |level|. This should only be called
50   // when this coalesced packet contains packet of |level|.
51   TransmissionType TransmissionTypeOfPacket(EncryptionLevel level) const;
52 
53   // Returns number of packets contained in this coalesced packet.
54   size_t NumberOfPackets() const;
55 
initial_packet()56   const SerializedPacket* initial_packet() const {
57     return initial_packet_.get();
58   }
59 
self_address()60   const QuicSocketAddress& self_address() const { return self_address_; }
61 
peer_address()62   const QuicSocketAddress& peer_address() const { return peer_address_; }
63 
length()64   QuicPacketLength length() const { return length_; }
65 
max_packet_length()66   QuicPacketLength max_packet_length() const { return max_packet_length_; }
67 
68   std::vector<size_t> packet_lengths() const;
69 
ecn_codepoint()70   QuicEcnCodepoint ecn_codepoint() const { return ecn_codepoint_; }
71 
72  private:
73   friend class test::QuicCoalescedPacketPeer;
74 
75   // self/peer addresses are set when trying to coalesce the first packet.
76   // Packets with different self/peer addresses cannot be coalesced.
77   QuicSocketAddress self_address_;
78   QuicSocketAddress peer_address_;
79   // Length of this coalesced packet.
80   QuicPacketLength length_;
81   // Max packet length. Do not try to coalesce packet when max packet length
82   // changes (e.g., with MTU discovery).
83   QuicPacketLength max_packet_length_;
84   // Copies of packets' encrypted buffers according to different encryption
85   // levels.
86   std::string encrypted_buffers_[NUM_ENCRYPTION_LEVELS];
87   // Recorded transmission type according to different encryption levels.
88   TransmissionType transmission_types_[NUM_ENCRYPTION_LEVELS];
89 
90   // A copy of ENCRYPTION_INITIAL packet if this coalesced packet contains one.
91   // Null otherwise. Please note, the encrypted_buffer field is not copied. The
92   // frames are copied to allow it be re-serialized when this coalesced packet
93   // gets sent.
94   std::unique_ptr<SerializedPacket> initial_packet_;
95 
96   // A coalesced packet shares an ECN codepoint.
97   QuicEcnCodepoint ecn_codepoint_;
98 };
99 
100 }  // namespace quic
101 
102 #endif  // QUICHE_QUIC_CORE_QUIC_COALESCED_PACKET_H_
103