Prevent app list doodle from being pinch-to-zoomed.
[chromium-blink-merge.git] / net / quic / quic_packet_creator.h
blob190e5300dfe0b4ae35c820a359e3b9d61588609a
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 <string>
13 #include <utility>
14 #include <vector>
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "net/quic/quic_fec_group.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_protocol.h"
22 namespace net {
23 namespace test {
24 class QuicPacketCreatorPeer;
27 class QuicAckNotifier;
28 class QuicRandom;
29 class QuicRandomBoolSource;
31 class NET_EXPORT_PRIVATE QuicPacketCreator {
32 public:
33 // QuicRandom* required for packet entropy.
34 QuicPacketCreator(QuicConnectionId connection_id,
35 QuicFramer* framer,
36 QuicRandom* random_generator);
38 ~QuicPacketCreator();
40 // Turn on FEC protection for subsequently created packets. FEC should be
41 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
42 // protection to start.
43 void StartFecProtectingPackets();
45 // Turn off FEC protection for subsequently created packets. If the creator
46 // has any open FEC group, call will fail. It is the caller's responsibility
47 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
48 // that there is no open FEC group.
49 void StopFecProtectingPackets();
51 // Checks if it's time to send an FEC packet. |force_close| forces this to
52 // return true if an FEC group is open.
53 bool ShouldSendFec(bool force_close) const;
55 // Returns true if an FEC packet is under construction.
56 bool IsFecGroupOpen() const;
58 // Makes the framer not serialize the protocol version in sent packets.
59 void StopSendingVersion();
61 // Update the sequence number length to use in future packets as soon as it
62 // can be safely changed.
63 void UpdateSequenceNumberLength(
64 QuicPacketSequenceNumber least_packet_awaited_by_peer,
65 QuicPacketCount max_packets_in_flight);
67 // The overhead the framing will add for a packet with one frame.
68 static size_t StreamFramePacketOverhead(
69 QuicConnectionIdLength connection_id_length,
70 bool include_version,
71 QuicSequenceNumberLength sequence_number_length,
72 QuicStreamOffset offset,
73 InFecGroup is_in_fec_group);
75 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
77 // Converts a raw payload to a frame which fits into the currently open
78 // packet if there is one. Returns the number of bytes consumed from data.
79 // If data is empty and fin is true, the expected behavior is to consume the
80 // fin but return 0.
81 size_t CreateStreamFrame(QuicStreamId id,
82 const IOVector& data,
83 QuicStreamOffset offset,
84 bool fin,
85 QuicFrame* frame);
87 // Serializes all frames into a single packet. All frames must fit into a
88 // single packet. Also, sets the entropy hash of the serialized packet to a
89 // random bool and returns that value as a member of SerializedPacket.
90 // Never returns a RetransmittableFrames in SerializedPacket.
91 SerializedPacket SerializeAllFrames(const QuicFrames& frames,
92 char* buffer,
93 size_t buffer_len);
95 // Re-serializes frames with the original packet's sequence number length.
96 // Used for retransmitting packets to ensure they aren't too long.
97 // Caller must ensure that any open FEC group is closed before calling this
98 // method.
99 SerializedPacket ReserializeAllFrames(
100 const RetransmittableFrames& frames,
101 QuicSequenceNumberLength original_length,
102 char* buffer,
103 size_t buffer_len);
105 // Returns true if there are frames pending to be serialized.
106 bool HasPendingFrames() const;
108 // Returns true if there are retransmittable frames pending to be serialized.
109 bool HasPendingRetransmittableFrames() const;
111 // TODO(jri): Remove this method.
112 // Returns whether FEC protection is currently enabled. Note: Enabled does not
113 // mean that an FEC group is currently active; i.e., IsFecProtected() may
114 // still return false.
115 bool IsFecEnabled() const;
117 // Returns true if subsequent packets will be FEC protected. Note: True does
118 // not mean that an FEC packet is currently under construction; i.e.,
119 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
120 bool IsFecProtected() const;
122 // Returns the number of bytes which are available to be used by additional
123 // frames in the packet. Since stream frames are slightly smaller when they
124 // are the last frame in a packet, this method will return a different
125 // value than max_packet_size - PacketSize(), in this case.
126 size_t BytesFree() const;
128 // Returns the number of bytes that the packet will expand by if a new frame
129 // is added to the packet. If the last frame was a stream frame, it will
130 // expand slightly when a new frame is added, and this method returns the
131 // amount of expected expansion. If the packet is in an FEC group, no
132 // expansion happens and this method always returns zero.
133 size_t ExpansionOnNewFrame() const;
135 // Returns the number of bytes in the current packet, including the header,
136 // if serialized with the current frames. Adding a frame to the packet
137 // may change the serialized length of existing frames, as per the comment
138 // in BytesFree.
139 size_t PacketSize() const;
141 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
142 // if it is a stream frame, not other types of frames. Fix this API;
143 // add a AddNonSavedFrame method.
144 // Adds |frame| to the packet creator's list of frames to be serialized.
145 // Returns false if the frame doesn't fit into the current packet.
146 bool AddSavedFrame(const QuicFrame& frame);
148 // Serializes all frames which have been added and adds any which should be
149 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
150 // must fit into a single packet. Sets the entropy hash of the serialized
151 // packet to a random bool and returns that value as a member of
152 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
153 // the corresponding RetransmittableFrames if any frames are to be
154 // retransmitted.
155 // Fails if |buffer_len| isn't long enough for the encrypted packet.
156 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len);
158 // Packetize FEC data. All frames must fit into a single packet. Also, sets
159 // the entropy hash of the serialized packet to a random bool and returns
160 // that value as a member of SerializedPacket.
161 // Fails if |buffer_len| isn't long enough for the encrypted packet.
162 SerializedPacket SerializeFec(char* buffer, size_t buffer_len);
164 // Creates a version negotiation packet which supports |supported_versions|.
165 // Caller owns the created packet. Also, sets the entropy hash of the
166 // serialized packet to a random bool and returns that value as a member of
167 // SerializedPacket.
168 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
169 const QuicVersionVector& supported_versions);
171 // Returns a dummy packet that is valid but contains no useful information.
172 static SerializedPacket NoPacket();
174 // Sets the encryption level that will be applied to new packets.
175 void set_encryption_level(EncryptionLevel level) {
176 encryption_level_ = level;
179 // Sequence number of the last created packet, or 0 if no packets have been
180 // created.
181 QuicPacketSequenceNumber sequence_number() const {
182 return sequence_number_;
185 QuicConnectionIdLength connection_id_length() const {
186 return connection_id_length_;
189 void set_connection_id_length(QuicConnectionIdLength length) {
190 connection_id_length_ = length;
193 QuicByteCount max_packet_length() const {
194 return max_packet_length_;
197 // Sets the encrypter to use for the encryption level and updates the max
198 // plaintext size.
199 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
201 // Sets the maximum packet length.
202 void SetMaxPacketLength(QuicByteCount length);
204 // Returns current max number of packets covered by an FEC group.
205 size_t max_packets_per_fec_group() const {
206 return max_packets_per_fec_group_;
209 // Sets creator's max number of packets covered by an FEC group.
210 // Note: While there are no constraints on |max_packets_per_fec_group|,
211 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
212 // To turn off FEC protection, use StopFecProtectingPackets().
213 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group);
215 // Returns the currently open FEC group's number. If there isn't an open FEC
216 // group, returns the last closed FEC group number. Returns 0 when FEC is
217 // disabled or no FEC group has been created yet.
218 QuicFecGroupNumber fec_group_number() { return fec_group_number_; }
220 private:
221 friend class test::QuicPacketCreatorPeer;
223 static bool ShouldRetransmit(const QuicFrame& frame);
225 // Updates lengths and also starts an FEC group if FEC protection is on and
226 // there is not already an FEC group open.
227 InFecGroup MaybeUpdateLengthsAndStartFec();
229 // Called when a data packet is constructed that is part of an FEC group.
230 // |payload| is the non-encrypted FEC protected payload of the packet.
231 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
232 base::StringPiece payload);
234 void FillPacketHeader(QuicFecGroupNumber fec_group,
235 bool fec_flag,
236 QuicPacketHeader* header);
238 // Allows a frame to be added without creating retransmittable frames.
239 // Particularly useful for retransmits using SerializeAllFrames().
240 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
242 // Adds a padding frame to the current packet only if the current packet
243 // contains a handshake message, and there is sufficient room to fit a
244 // padding frame.
245 void MaybeAddPadding();
247 QuicConnectionId connection_id_;
248 EncryptionLevel encryption_level_;
249 QuicFramer* framer_;
250 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
251 QuicPacketSequenceNumber sequence_number_;
252 // If true, any created packets will be FEC protected.
253 bool should_fec_protect_;
254 QuicFecGroupNumber fec_group_number_;
255 scoped_ptr<QuicFecGroup> fec_group_;
256 // Controls whether protocol version should be included while serializing the
257 // packet.
258 bool send_version_in_packet_;
259 // Maximum length including headers and encryption (UDP payload length.)
260 QuicByteCount max_packet_length_;
261 // 0 indicates FEC is disabled.
262 size_t max_packets_per_fec_group_;
263 // Length of connection_id to send over the wire.
264 QuicConnectionIdLength connection_id_length_;
265 // Staging variable to hold next packet sequence number length. When sequence
266 // number length is to be changed, this variable holds the new length until
267 // a packet or FEC group boundary, when the creator's sequence_number_length_
268 // can be changed to this new value.
269 QuicSequenceNumberLength next_sequence_number_length_;
270 // Sequence number length for the current packet and for the current FEC group
271 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
272 // is empty.
273 mutable QuicSequenceNumberLength sequence_number_length_;
274 // packet_size_ is mutable because it's just a cache of the current size.
275 // packet_size should never be read directly, use PacketSize() instead.
276 mutable size_t packet_size_;
277 mutable size_t max_plaintext_size_;
278 QuicFrames queued_frames_;
279 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
281 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
284 } // namespace net
286 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_