Snap pinch zoom gestures near the screen edge.
[chromium-blink-merge.git] / net / quic / quic_packet_creator.h
blob493a47b976627407513aa1b5def44bc57694cbb0
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 // Resets (closes) the FEC group. This method should only be called on a
56 // packet boundary.
57 void ResetFecGroup();
59 // Returns true if an FEC packet is under construction.
60 bool IsFecGroupOpen() const;
62 // Makes the framer not serialize the protocol version in sent packets.
63 void StopSendingVersion();
65 // Update the sequence number length to use in future packets as soon as it
66 // can be safely changed.
67 void UpdateSequenceNumberLength(
68 QuicPacketSequenceNumber least_packet_awaited_by_peer,
69 QuicPacketCount max_packets_in_flight);
71 // The overhead the framing will add for a packet with one frame.
72 static size_t StreamFramePacketOverhead(
73 QuicConnectionIdLength connection_id_length,
74 bool include_version,
75 QuicSequenceNumberLength sequence_number_length,
76 QuicStreamOffset offset,
77 InFecGroup is_in_fec_group);
79 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
81 // Converts a raw payload to a frame which fits into the currently open
82 // packet. The payload begins at |iov_offset| into the |iov|.
83 // Returns the number of bytes consumed from data.
84 // If data is empty and fin is true, the expected behavior is to consume the
85 // fin but return 0. If any data is consumed, it will be copied into a
86 // new buffer that |frame| will point to and will be stored in |buffer|.
87 size_t CreateStreamFrame(QuicStreamId id,
88 const QuicIOVector& iov,
89 size_t iov_offset,
90 QuicStreamOffset offset,
91 bool fin,
92 QuicFrame* frame,
93 scoped_ptr<char[]>* buffer);
95 // Serializes all frames into a single packet. All frames must fit into a
96 // single packet. Also, sets the entropy hash of the serialized packet to a
97 // random bool and returns that value as a member of SerializedPacket.
98 // Never returns a RetransmittableFrames in SerializedPacket.
99 SerializedPacket SerializeAllFrames(const QuicFrames& frames,
100 char* buffer,
101 size_t buffer_len);
103 // Re-serializes frames with the original packet's sequence number length.
104 // Used for retransmitting packets to ensure they aren't too long.
105 // Caller must ensure that any open FEC group is closed before calling this
106 // method.
107 SerializedPacket ReserializeAllFrames(
108 const RetransmittableFrames& frames,
109 QuicSequenceNumberLength original_length,
110 char* buffer,
111 size_t buffer_len);
113 // Returns true if there are frames pending to be serialized.
114 bool HasPendingFrames() const;
116 // Returns true if there are retransmittable frames pending to be serialized.
117 bool HasPendingRetransmittableFrames() const;
119 // TODO(jri): Remove this method.
120 // Returns whether FEC protection is currently enabled. Note: Enabled does not
121 // mean that an FEC group is currently active; i.e., IsFecProtected() may
122 // still return false.
123 bool IsFecEnabled() const;
125 // Returns true if subsequent packets will be FEC protected. Note: True does
126 // not mean that an FEC packet is currently under construction; i.e.,
127 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
128 bool IsFecProtected() const;
130 // Returns the number of bytes which are available to be used by additional
131 // frames in the packet. Since stream frames are slightly smaller when they
132 // are the last frame in a packet, this method will return a different
133 // value than max_packet_size - PacketSize(), in this case.
134 size_t BytesFree() const;
136 // Returns the number of bytes that the packet will expand by if a new frame
137 // is added to the packet. If the last frame was a stream frame, it will
138 // expand slightly when a new frame is added, and this method returns the
139 // amount of expected expansion. If the packet is in an FEC group, no
140 // expansion happens and this method always returns zero.
141 size_t ExpansionOnNewFrame() const;
143 // Returns the number of bytes in the current packet, including the header,
144 // if serialized with the current frames. Adding a frame to the packet
145 // may change the serialized length of existing frames, as per the comment
146 // in BytesFree.
147 size_t PacketSize() const;
149 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
150 // if it is a stream frame, not other types of frames. Fix this API;
151 // add a AddNonSavedFrame method.
152 // Adds |frame| to the packet creator's list of frames to be serialized.
153 // Returns false if the frame doesn't fit into the current packet.
154 bool AddSavedFrame(const QuicFrame& frame);
156 // Identical to AddSavedFrame, but takes ownership of the buffer if it returns
157 // true.
158 bool AddSavedFrame(const QuicFrame& frame, char* buffer);
160 // Identical to AddSavedFrame, but takes ownership of the buffer if it returns
161 // true, and allows to cause the packet to be padded.
162 bool AddPaddedSavedFrame(const QuicFrame& frame, char* buffer);
164 // Serializes all frames which have been added and adds any which should be
165 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
166 // must fit into a single packet. Sets the entropy hash of the serialized
167 // packet to a random bool and returns that value as a member of
168 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
169 // the corresponding RetransmittableFrames if any frames are to be
170 // retransmitted.
171 // Fails if |buffer_len| isn't long enough for the encrypted packet.
172 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len);
174 // Packetize FEC data. All frames must fit into a single packet. Also, sets
175 // the entropy hash of the serialized packet to a random bool and returns
176 // that value as a member of SerializedPacket.
177 // Fails if |buffer_len| isn't long enough for the encrypted packet.
178 SerializedPacket SerializeFec(char* buffer, size_t buffer_len);
180 // Creates a version negotiation packet which supports |supported_versions|.
181 // Caller owns the created packet. Also, sets the entropy hash of the
182 // serialized packet to a random bool and returns that value as a member of
183 // SerializedPacket.
184 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
185 const QuicVersionVector& supported_versions);
187 // Returns a dummy packet that is valid but contains no useful information.
188 static SerializedPacket NoPacket();
190 // Sets the encryption level that will be applied to new packets.
191 void set_encryption_level(EncryptionLevel level) {
192 encryption_level_ = level;
195 // Sequence number of the last created packet, or 0 if no packets have been
196 // created.
197 QuicPacketSequenceNumber sequence_number() const {
198 return sequence_number_;
201 QuicConnectionIdLength connection_id_length() const {
202 return connection_id_length_;
205 void set_connection_id_length(QuicConnectionIdLength length) {
206 connection_id_length_ = length;
209 QuicByteCount max_packet_length() const {
210 return max_packet_length_;
213 // Sets the encrypter to use for the encryption level and updates the max
214 // plaintext size.
215 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
217 // Indicates whether the packet creator is in a state where it can change
218 // current maximum packet length.
219 bool CanSetMaxPacketLength() const;
221 // Sets the maximum packet length.
222 void SetMaxPacketLength(QuicByteCount length);
224 // Returns current max number of packets covered by an FEC group.
225 size_t max_packets_per_fec_group() const {
226 return max_packets_per_fec_group_;
229 // Sets creator's max number of packets covered by an FEC group.
230 // Note: While there are no constraints on |max_packets_per_fec_group|,
231 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
232 // To turn off FEC protection, use StopFecProtectingPackets().
233 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group);
235 // Returns the currently open FEC group's number. If there isn't an open FEC
236 // group, returns the last closed FEC group number. Returns 0 when FEC is
237 // disabled or no FEC group has been created yet.
238 QuicFecGroupNumber fec_group_number() { return fec_group_number_; }
240 private:
241 friend class test::QuicPacketCreatorPeer;
243 static bool ShouldRetransmit(const QuicFrame& frame);
245 // Copies |length| bytes from iov starting at offset |iov_offset| into buffer.
246 // |iov| must be at least iov_offset+length total length and buffer must be
247 // at least |length| long.
248 static void CopyToBuffer(const QuicIOVector& iov,
249 size_t iov_offset,
250 size_t length,
251 char* buffer);
253 // Updates lengths and also starts an FEC group if FEC protection is on and
254 // there is not already an FEC group open.
255 InFecGroup MaybeUpdateLengthsAndStartFec();
257 // Called when a data packet is constructed that is part of an FEC group.
258 // |payload| is the non-encrypted FEC protected payload of the packet.
259 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
260 base::StringPiece payload);
262 void FillPacketHeader(QuicFecGroupNumber fec_group,
263 bool fec_flag,
264 QuicPacketHeader* header);
266 // Allows a frame to be added without creating retransmittable frames.
267 // Particularly useful for retransmits using SerializeAllFrames().
268 bool AddFrame(const QuicFrame& frame,
269 bool save_retransmittable_frames,
270 bool needs_padding,
271 char* buffer);
273 // Adds a padding frame to the current packet only if the current packet
274 // contains a handshake message, and there is sufficient room to fit a
275 // padding frame.
276 void MaybeAddPadding();
278 QuicConnectionId connection_id_;
279 EncryptionLevel encryption_level_;
280 QuicFramer* framer_;
281 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
282 QuicPacketSequenceNumber sequence_number_;
283 // If true, any created packets will be FEC protected.
284 bool should_fec_protect_;
285 QuicFecGroupNumber fec_group_number_;
286 scoped_ptr<QuicFecGroup> fec_group_;
287 // Controls whether protocol version should be included while serializing the
288 // packet.
289 bool send_version_in_packet_;
290 // Maximum length including headers and encryption (UDP payload length.)
291 QuicByteCount max_packet_length_;
292 // 0 indicates FEC is disabled.
293 size_t max_packets_per_fec_group_;
294 // Length of connection_id to send over the wire.
295 QuicConnectionIdLength connection_id_length_;
296 // Staging variable to hold next packet sequence number length. When sequence
297 // number length is to be changed, this variable holds the new length until
298 // a packet or FEC group boundary, when the creator's sequence_number_length_
299 // can be changed to this new value.
300 QuicSequenceNumberLength next_sequence_number_length_;
301 // Sequence number length for the current packet and for the current FEC group
302 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
303 // is empty.
304 mutable QuicSequenceNumberLength sequence_number_length_;
305 // packet_size_ is mutable because it's just a cache of the current size.
306 // packet_size should never be read directly, use PacketSize() instead.
307 mutable size_t packet_size_;
308 mutable size_t max_plaintext_size_;
309 QuicFrames queued_frames_;
310 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
311 // If true, the packet will be padded up to |max_packet_length_|.
312 bool needs_padding_;
314 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
317 } // namespace net
319 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_