[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / quic / quic_packet_creator.h
bloba43d027610c35a3531ca732f6da1d61b3cfafcb8
1 // Copyright (c) 2012 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 // Accumulates frames for the next packet until more frames no longer fit or
6 // it's time to create a packet from them. Also provides packet creation of
7 // FEC packets based on previously created packets.
9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_
12 #include <utility>
13 #include <vector>
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/quic/quic_fec_group.h"
18 #include "net/quic/quic_framer.h"
19 #include "net/quic/quic_protocol.h"
21 namespace net {
22 namespace test {
23 class QuicPacketCreatorPeer;
26 class QuicAckNotifier;
27 class QuicRandom;
28 class QuicRandomBoolSource;
30 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
31 public:
32 // QuicRandom* required for packet entropy.
33 QuicPacketCreator(QuicConnectionId connection_id,
34 QuicFramer* framer,
35 QuicRandom* random_generator);
37 ~QuicPacketCreator() override;
39 // QuicFecBuilderInterface
40 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
41 base::StringPiece payload) override;
43 // Turn on FEC protection for subsequently created packets. FEC should be
44 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
45 // protection to start.
46 void StartFecProtectingPackets();
48 // Turn off FEC protection for subsequently created packets. If the creator
49 // has any open FEC group, call will fail. It is the caller's responsibility
50 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
51 // that there is no open FEC group.
52 void StopFecProtectingPackets();
54 // Checks if it's time to send an FEC packet. |force_close| forces this to
55 // return true if an FEC group is open.
56 bool ShouldSendFec(bool force_close) const;
58 // Returns true if an FEC packet is under construction.
59 bool IsFecGroupOpen() const;
61 // Makes the framer not serialize the protocol version in sent packets.
62 void StopSendingVersion();
64 // Update the sequence number length to use in future packets as soon as it
65 // can be safely changed.
66 void UpdateSequenceNumberLength(
67 QuicPacketSequenceNumber least_packet_awaited_by_peer,
68 QuicPacketCount max_packets_in_flight);
70 // The overhead the framing will add for a packet with one frame.
71 static size_t StreamFramePacketOverhead(
72 QuicConnectionIdLength connection_id_length,
73 bool include_version,
74 QuicSequenceNumberLength sequence_number_length,
75 QuicStreamOffset offset,
76 InFecGroup is_in_fec_group);
78 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
80 // Converts a raw payload to a frame which fits into the currently open
81 // packet if there is one. Returns the number of bytes consumed from data.
82 // If data is empty and fin is true, the expected behavior is to consume the
83 // fin but return 0.
84 size_t CreateStreamFrame(QuicStreamId id,
85 const IOVector& data,
86 QuicStreamOffset offset,
87 bool fin,
88 QuicFrame* frame);
90 // Serializes all frames into a single packet. All frames must fit into a
91 // single packet. Also, sets the entropy hash of the serialized packet to a
92 // random bool and returns that value as a member of SerializedPacket.
93 // Never returns a RetransmittableFrames in SerializedPacket.
94 SerializedPacket SerializeAllFrames(const QuicFrames& frames);
96 // Re-serializes frames with the original packet's sequence number length.
97 // Used for retransmitting packets to ensure they aren't too long.
98 // Caller must ensure that any open FEC group is closed before calling this
99 // method.
100 SerializedPacket ReserializeAllFrames(
101 const RetransmittableFrames& frames,
102 QuicSequenceNumberLength original_length);
104 // Returns true if there are frames pending to be serialized.
105 bool HasPendingFrames() const;
107 // Returns true if there are retransmittable frames pending to be serialized.
108 bool HasPendingRetransmittableFrames() const;
110 // TODO(jri): Remove this method.
111 // Returns whether FEC protection is currently enabled. Note: Enabled does not
112 // mean that an FEC group is currently active; i.e., IsFecProtected() may
113 // still return false.
114 bool IsFecEnabled() const;
116 // Returns true if subsequent packets will be FEC protected. Note: True does
117 // not mean that an FEC packet is currently under construction; i.e.,
118 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
119 bool IsFecProtected() const;
121 // Returns the number of bytes which are available to be used by additional
122 // frames in the packet. Since stream frames are slightly smaller when they
123 // are the last frame in a packet, this method will return a different
124 // value than max_packet_size - PacketSize(), in this case.
125 size_t BytesFree() const;
127 // Returns the number of bytes that the packet will expand by if a new frame
128 // is added to the packet. If the last frame was a stream frame, it will
129 // expand slightly when a new frame is added, and this method returns the
130 // amount of expected expansion. If the packet is in an FEC group, no
131 // expansion happens and this method always returns zero.
132 size_t ExpansionOnNewFrame() const;
134 // Returns the number of bytes in the current packet, including the header,
135 // if serialized with the current frames. Adding a frame to the packet
136 // may change the serialized length of existing frames, as per the comment
137 // in BytesFree.
138 size_t PacketSize() const;
140 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
141 // if it is a stream frame, not other types of frames. Fix this API;
142 // add a AddNonSavedFrame method.
143 // Adds |frame| to the packet creator's list of frames to be serialized.
144 // Returns false if the frame doesn't fit into the current packet.
145 bool AddSavedFrame(const QuicFrame& frame);
147 // Serializes all frames which have been added and adds any which should be
148 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
149 // must fit into a single packet. Sets the entropy hash of the serialized
150 // packet to a random bool and returns that value as a member of
151 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
152 // the corresponding RetransmittableFrames if any frames are to be
153 // retransmitted.
154 SerializedPacket SerializePacket();
156 // Packetize FEC data. All frames must fit into a single packet. Also, sets
157 // the entropy hash of the serialized packet to a random bool and returns
158 // that value as a member of SerializedPacket.
159 SerializedPacket SerializeFec();
161 // Creates a version negotiation packet which supports |supported_versions|.
162 // Caller owns the created packet. Also, sets the entropy hash of the
163 // serialized packet to a random bool and returns that value as a member of
164 // SerializedPacket.
165 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
166 const QuicVersionVector& supported_versions);
168 // Sets the encryption level that will be applied to new packets.
169 void set_encryption_level(EncryptionLevel level) {
170 encryption_level_ = level;
173 // Sequence number of the last created packet, or 0 if no packets have been
174 // created.
175 QuicPacketSequenceNumber sequence_number() const {
176 return sequence_number_;
179 QuicConnectionIdLength connection_id_length() const {
180 return connection_id_length_;
183 void set_connection_id_length(QuicConnectionIdLength length) {
184 connection_id_length_ = length;
187 QuicByteCount max_packet_length() const {
188 return max_packet_length_;
191 void set_max_packet_length(QuicByteCount length) {
192 // |max_packet_length_| should not be changed mid-packet or mid-FEC group.
193 DCHECK(fec_group_.get() == nullptr && queued_frames_.empty());
194 max_packet_length_ = length;
197 // Returns current max number of packets covered by an FEC group.
198 size_t max_packets_per_fec_group() const {
199 return max_packets_per_fec_group_;
202 // Sets creator's max number of packets covered by an FEC group.
203 // Note: While there are no constraints on |max_packets_per_fec_group|,
204 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
205 // To turn off FEC protection, use StopFecProtectingPackets().
206 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group);
208 // Returns the currently open FEC group's number. If there isn't an open FEC
209 // group, returns the last closed FEC group number. Returns 0 when FEC is
210 // disabled or no FEC group has been created yet.
211 QuicFecGroupNumber fec_group_number() { return fec_group_number_; }
213 private:
214 friend class test::QuicPacketCreatorPeer;
216 static bool ShouldRetransmit(const QuicFrame& frame);
218 // Updates lengths and also starts an FEC group if FEC protection is on and
219 // there is not already an FEC group open.
220 InFecGroup MaybeUpdateLengthsAndStartFec();
222 void FillPacketHeader(QuicFecGroupNumber fec_group,
223 bool fec_flag,
224 QuicPacketHeader* header);
226 // Allows a frame to be added without creating retransmittable frames.
227 // Particularly useful for retransmits using SerializeAllFrames().
228 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
230 // Adds a padding frame to the current packet only if the current packet
231 // contains a handshake message, and there is sufficient room to fit a
232 // padding frame.
233 void MaybeAddPadding();
235 QuicConnectionId connection_id_;
236 EncryptionLevel encryption_level_;
237 QuicFramer* framer_;
238 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
239 QuicPacketSequenceNumber sequence_number_;
240 // If true, any created packets will be FEC protected.
241 bool should_fec_protect_;
242 QuicFecGroupNumber fec_group_number_;
243 scoped_ptr<QuicFecGroup> fec_group_;
244 // Controls whether protocol version should be included while serializing the
245 // packet.
246 bool send_version_in_packet_;
247 // Maximum length including headers and encryption (UDP payload length.)
248 QuicByteCount max_packet_length_;
249 // 0 indicates FEC is disabled.
250 size_t max_packets_per_fec_group_;
251 // Length of connection_id to send over the wire.
252 QuicConnectionIdLength connection_id_length_;
253 // Staging variable to hold next packet sequence number length. When sequence
254 // number length is to be changed, this variable holds the new length until
255 // a packet or FEC group boundary, when the creator's sequence_number_length_
256 // can be changed to this new value.
257 QuicSequenceNumberLength next_sequence_number_length_;
258 // Sequence number length for the current packet and for the current FEC group
259 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
260 // is empty.
261 mutable QuicSequenceNumberLength sequence_number_length_;
262 // packet_size_ is mutable because it's just a cache of the current size.
263 // packet_size should never be read directly, use PacketSize() instead.
264 mutable size_t packet_size_;
265 QuicFrames queued_frames_;
266 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
268 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
271 } // namespace net
273 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_