• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 QUIC_EXPORT_PRIVATE 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 
31   // Clears this coalesced packet.
32   void Clear();
33 
34   // Clears all state associated with initial_packet_.
35   void NeuterInitialPacket();
36 
37   // Copies encrypted_buffers_ to |buffer| and sets |length_copied| to the
38   // copied amount. Returns false if copy fails (i.e., |buffer_len| is not
39   // enough).
40   bool CopyEncryptedBuffers(char* buffer, size_t buffer_len,
41                             size_t* length_copied) const;
42 
43   std::string ToString(size_t serialized_length) const;
44 
45   // Returns true if this coalesced packet contains packet of |level|.
46   bool ContainsPacketOfEncryptionLevel(EncryptionLevel level) const;
47 
48   // Returns transmission type of packet of |level|. This should only be called
49   // when this coalesced packet contains packet of |level|.
50   TransmissionType TransmissionTypeOfPacket(EncryptionLevel level) const;
51 
52   // Returns number of packets contained in this coalesced packet.
53   size_t NumberOfPackets() const;
54 
initial_packet()55   const SerializedPacket* initial_packet() const {
56     return initial_packet_.get();
57   }
58 
self_address()59   const QuicSocketAddress& self_address() const { return self_address_; }
60 
peer_address()61   const QuicSocketAddress& peer_address() const { return peer_address_; }
62 
length()63   QuicPacketLength length() const { return length_; }
64 
max_packet_length()65   QuicPacketLength max_packet_length() const { return max_packet_length_; }
66 
67   std::vector<size_t> packet_lengths() const;
68 
69  private:
70   friend class test::QuicCoalescedPacketPeer;
71 
72   // self/peer addresses are set when trying to coalesce the first packet.
73   // Packets with different self/peer addresses cannot be coalesced.
74   QuicSocketAddress self_address_;
75   QuicSocketAddress peer_address_;
76   // Length of this coalesced packet.
77   QuicPacketLength length_;
78   // Max packet length. Do not try to coalesce packet when max packet length
79   // changes (e.g., with MTU discovery).
80   QuicPacketLength max_packet_length_;
81   // Copies of packets' encrypted buffers according to different encryption
82   // levels.
83   std::string encrypted_buffers_[NUM_ENCRYPTION_LEVELS];
84   // Recorded transmission type according to different encryption levels.
85   TransmissionType transmission_types_[NUM_ENCRYPTION_LEVELS];
86 
87   // A copy of ENCRYPTION_INITIAL packet if this coalesced packet contains one.
88   // Null otherwise. Please note, the encrypted_buffer field is not copied. The
89   // frames are copied to allow it be re-serialized when this coalesced packet
90   // gets sent.
91   std::unique_ptr<SerializedPacket> initial_packet_;
92 };
93 
94 }  // namespace quic
95 
96 #endif  // QUICHE_QUIC_CORE_QUIC_COALESCED_PACKET_H_
97